note 22631 deleted from features.file-upload.multiple by aidan

From: Date: Sun, 19 Sep 2004 03:28:42 +0000
Subject: note 22631 deleted from features.file-upload.multiple by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-76965@lists.php.net to get a copy of this message
Note Submitter: quinten+spam at andrew dot cmu dot edu ---- Here's working file upload code snippet that works for any number of files "out of the box." It assumes that each input of type file is named 'files[]', although this could be modified so that it just iterated through the array $HTTP_POST_FILES. It will not overwrite existing files. It could be improved with more checks (ie, mime types) but this does what I need quite admirably. Let me know if you find an inefficiency. <?php $path_to_file = '/var/www-uploads/'; $files = $HTTP_POST_FILES['files']; if (!ereg("/$", $path_to_file)) $path_to_file = $path_to_file."/"; foreach ($files['name'] as $key=>$name) { if ($files['size'][$key]) { // clean up file name $name = ereg_replace("[^a-z0-9._]", "", str_replace(" ", "_", str_replace("%20", "_", strtolower($name) ) ) ); $location = $path_to_file.$name; while (file_exists($location)) $location .= ".copy"; copy($files['tmp_name'][$key],$location); unlink($files['tmp_name'][$key]); echo "\n<br>Successfully uploaded file: $name."; } } ?>

« previous php.notes (#76965) next »