php - uploading multiple files, renaming with form data -
ok first off, i'm new here, if i'm not posting correctly, please let me know. did search extensively on site, did not find issue.
i have php form , uploader should upload multiple files , rename files (add name & date original file name) according response in name field of form, , date of upload.
everything works except: 1 file uploads, not multiple files. while writing this, did work, i've messed recently.
can tell me went wrong? thanks.
here code form:
<?php // make note of current working directory relative root. $directory_self = str_replace(basename($_server['php_self']), '', $_server['php_self']); // location of upload handler $uploadhandler = 'http://' . $_server['http_host'] . $directory_self . 'upload.processor.php'; // max file bytes (2mb) $max_file_size = 2097152; // echo ?><!doctype html public "-//w3c//dtd html 4.01//en" "http://www.w3.org/tr/html4/strict.dtd"> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <link rel="stylesheet" type="text/css" href="stylesheet.css"> <title>upload awesome images!</title> </head> <body> <form id="upload" action="<?php echo $uploadhandler ?>" enctype="multipart/form-data" method="post"> <span>step #2</span> <h1> upload images! </h1> <div style="border:#999 1px solid;padding:5px"> <p> <input type="hidden" name="max_file_size" value="<?php echo $max_file_size ?>"> </p> <p> photographer's name: <input type="text" name="photogname" value="<?php if(isset($_post['contactname'])) echo $_post['contactname'];?>"> </p> <p> <label for="file">file upload:</label> <input id="file" type="file" name="file[]" class="tag" multiple/> </p> <p> <label for="submit">press to...</label> <input id="submit" type="submit" name="submit" value="upload me!"> </p> </div> <p>max file size= 2mb</p> <p>max number of files uploadable = 20</p> <p>*important note: when submitting photos, please sure owner of images, or have explicit permission owner use them. copyrights verified before winners announced. </p> </form> </body> </html>
and here processing:
<?php //current local $directory_self = str_replace(basename($_server['php_self']), '', $_server['php_self']); //locations/dirs $uploadsdirectory = $_server['document_root'] . $directory_self . '../images/photocontest/'; $uploadform = 'http://' . $_server['http_host'] . $directory_self . 'upload.form.php'; $uploadsuccess = 'http://' . $_server['http_host'] . $directory_self . 'upload.success.php'; $fieldname = 'file'; // upload // possible upload errors (more?) $errors = array(1 => 'php.ini max file size exceeded', 2 => 'html form max file size exceeded', 3 => 'file upload partial', 4 => 'no file attached'); // check upload form used. no fancy injection stuff isset($_post['submit']) or error('woah. can\'t upload images without form', $uploadform); // check standard uploading errors ($_files[$fieldname]['error'] == 0) or error($errors[$_files[$fieldname]['error']], $uploadform); // check file http upload, not sneaky pete @is_uploaded_file($_files[$fieldname]['tmp_name']) or error('not http upload', $uploadform); // validation... // should run check make sure upload image @getimagesize($_files[$fieldname]['tmp_name']) or error('only image uploads allowed', $uploadform); //name file. if not, name time (remove time function when name works right) $now = date('m-d-y'); $photogname = ($_post['photogname']); while(file_exists($uploadfilename = $uploadsdirectory.$photogname.$now.'-'.$_files[$fieldname]['name'])) { $now++; } // move file final dir (& check perm) @move_uploaded_file($_files[$fieldname]['tmp_name'], $uploadfilename) or error('receiving directory insuffiecient permission', $uploadform); // sucess header('location: ' . $uploadsuccess); // error handler function error($error, $location, $seconds = 5) { header("refresh: $seconds; url=\"$location\""); echo '<!doctype html public "-//w3c//dtd html 4.01//en"'."\n". '"http://www.w3.org/tr/html4/strict.dtd">'."\n\n". '<html lang="en">'."\n". ' <head>'."\n". ' <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."\n\n". ' <link rel="stylesheet" type="text/css" href="stylesheet.css">'."\n\n". ' <title>upload error</title>'."\n\n". ' </head>'."\n\n". ' <body>'."\n\n". ' <div id="upload">'."\n\n". ' <h1>upload failure</h1>'."\n\n". ' <p>an error has occured: '."\n\n". ' <span class="red">' . $error . '...</span>'."\n\n". ' upload form reloading</p>'."\n\n". ' </div>'."\n\n". '</html>'; exit; } // end error handler ?>
there multiple
word @ end of file input
tag. don't know what's , it's defiantly not html 5 or because document type version 4.
check code:
html:
<input type="file" name="file[]" class="tag"/> <input type="file" name="file[]" class="tag"/> <input type="file" name="file[]" class="tag"/>
php:
foreach($_files $file) { // check standard uploading errors ($file['error'] == 0) or error($errors[$file['error']], $uploadform); // check file http upload, not sneaky pete @is_uploaded_file($file['tmp_name']) or error('not http upload', $uploadform); // validation... // should run check make sure upload image @getimagesize($file['tmp_name']) or error('only image uploads allowed', $uploadform); //name file. if not, name time (remove time function when name works right) $now = date('m-d-y'); $photogname = ($_post['photogname']); while(file_exists($uploadfilename = $uploadsdirectory.$photogname.$now.'-'.$file['name'])) { $now++; } // move file final dir (& check perm) @move_uploaded_file($file['tmp_name'], $uploadfilename) or error('receiving directory insuffiecient permission', $uploadform); }
Comments
Post a Comment