note 51569 deleted from features.file-upload by felipe
| From: | felipe@php.net | Date: | Sun, 30 Dec 2007 17:50:44 +0000 |
| Subject: | note 51569 deleted from features.file-upload by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134314@lists.php.net to get a copy of this message | ||
Note Submitter: bryan dot fillmer at beasleyallen dot com
----
I looked high and low for a simple way to rename files as they are uploaded to prevent users from
overwriting each others files. Below is the script I came up with.
<?php
foreach( $_FILES["screenshots"]["error"] as $key => $error ) {
if( $error == UPLOAD_ERR_OK ) {
$tmp_name = $_FILES["screenshots"]["tmp_name"][$key];
$original_name = $_FILES["screenshots"]["name"][$key];
$tmp_array = preg_split( "/\\\/",$tmp_name );
$file_name = $tmp_array[count($array)-1];
$extension = preg_replace( "/(.*)\.([a-z]+)$/","$2",$original_name );
$new_name = preg_replace( "/(.*)\.([a-z]+)$/","$1.$extension",$file_name );
move_uploaded_file( $tmp_name,"./screenshots/$new_name" );
}
}
?>
I started with the generic multiple file upload script in the documentation above and worked from
there.