Click here to Skip to main content
15,908,775 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all, i've got a script that i've part edited to accept only certain file types to be uploaded, eg; JPG, PNG, JPEG etc. the script is originally part of the "uploadify script", im new to php so im finding it hard to work out how i can add a function to rename files on the backend so i dont get duplicates or errors on upload, but keep original names for the frontend. if this all makes sense, thanks in advance - Cody

PHP
if (!empty($_FILES)) {
	$tempFile = $_FILES['Filedata']['tmp_name'];
	$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
	$targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
	
	
	$fileTypes = array('jpg','jpeg','gif','png'); // File extensions
	$fileParts = pathinfo($_FILES['Filedata']['name']);
	
	if (in_array($fileParts['extension'],$fileTypes)) {
		move_uploaded_file($tempFile,$targetFile);
		echo '1';
	} else {
		echo 'Invalid file type.';
	}
}
?>


[edit]Tags only - OriginalGriff[/edit]
Posted
Updated 7-Oct-11 21:09pm
v2
Comments
Bala Selvanayagam 8-Oct-11 9:55am    
consider prefixing the file name with a unique identifier,

for example 12223434_myphoto.jpg and then substring the filename for "_" and display in the front end ?

1 solution

Hi cjason406
If you want to rename only filename, it's very easy.
you can change value of $targetFile variable to new values.
for example:
PHP
      $targetFile =  str_replace('//','/',$targetPath) . 'yourname'.'jpg';

or
you can rewrite move_uploaded_file syntax
        $path="../images/profile/";
        $tagetName=$username;
        $extention=$fileParts['extension']; // so you can enter ext eg jpg
        move_uploaded_file($tempFile,$paht.$tagetName.$extention);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900