Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Renaming certain files after upload.

    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

    Code:
    <?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.';
    	}
    }
    ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    No that doesn't really make any sense. What is the difference between the front and back end when it comes to this code? The user can upload any file they want, you can rename it to anything you want.
    To rename, simply modify $targetFile to change what you need. The problem I see overall is on conflict even with a conflict detection and correction, how will you determine which of the conflicted files is the same as this file just uploaded at a later time?
    You'd be better off using a database to associate random generated file names to the user uploading them (or whatever you plan on using for an association). Then you can display whatever you want within the database and the file names themselves have no impact on the display.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •