note 22631 modified in features.file-upload.multiple by vrana
| From: | vrana@php.net | Date: | Tue, 17 Aug 2004 13:55:49 +0000 |
| Subject: | note 22631 modified in features.file-upload.multiple by vrana | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-80047@lists.php.net to get a copy of this message | ||
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.";
}
}
?>
--was--
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.
$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.";
}
}
http://php.net/manual/en/features.file-upload.multiple.php