note 51569 deleted from features.file-upload by felipe

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
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.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.