Using this upload script in a template (have tried PHP on both input / output):
<?
include($_SERVER['DOCUMENT_ROOT'] . '/upload/class.upload_27.php');
$dir_dest = $_SERVER['DOCUMENT_ROOT'] . "/otheruploads/";
$files = array();
foreach ($_FILES['my_field'] as $k => $l) {
foreach ($l as $i => $v) {
if (!array_key_exists($i, $files))
$files[$i] = array();
$files[$i][$k] = $v;
}
}
foreach ($files as $file) {
// we instanciate the class for each element of $file
$handle = new Upload($file);
// then we check if the file has been uploaded properly
// in its *temporary* location in the server (often, it is /tmp)
if ($handle->uploaded) {
// now, we start the upload 'process'. That is, to copy the uploaded file
// from its temporary location to the wanted location
// It could be something like $handle->Process('/home/www/my_uploads/');
$handle->Process($dir_dest);
// we check if everything went OK
if ($handle->processed) {
// everything was fine !
echo '<fieldset>';
echo ' <legend>file uploaded with success</legend>';
echo ' ' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB';
echo ' link to the file just uploaded: <a >file_dst_name . '">' . $handle->file_dst_name . '</a>';
echo '</fieldset>';
} else {
// one error occured
echo '<fieldset>';
echo ' <legend>file not uploaded to the wanted location</legend>';
echo ' Error: ' . $handle->error . '';
echo '</fieldset>';
}
} else {
// if we're here, the upload file failed for some reasons
// i.e. the server didn't receive the file
echo '<fieldset>';
echo ' <legend>file not uploaded on the server</legend>';
echo ' Error: ' . $handle->error . '';
echo '</fieldset>';
}
}
?>
It works fine if I store it as a native PHP file, but as soon as I try accessing it via a template, I get the following errors:
Notice: Undefined index: my_field in /home/mydir/system/core/core.functions.php(637) : eval()'d code on line 7
Warning: Invalid argument supplied for foreach() in /home/mydir/system/core/core.functions.php(637) : eval()'d code on line 7
FYI, here’s the HTML form:
<form name="form3" enctype="multipart/form-data" method="post" action="/actions/doupload">
<input type="file" size="32" name="my_field[]" value="" /></p>
<input type="file" size="32" name="my_field[]" value="" /></p>
<input type="file" size="32" name="my_field[]" value="" /></p>
<input type="submit" name="Submit" value="upload" /></p>
</form>
DEA:
I honestly would not be doing this in a template. Look at making a module to accomplish it. $FNS->form_declaration() is a very helpful tool that provides you with a more secure form.
That being said, I would first do a var_dump($_FILES); exit; to see if there is anything in the array.
I’m going to move this thread down to the modules forums, as it’s a better fit & I’d like to encourage you to make this into a module. 😉
-greg
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.