Results 1 to 2 of 2

Thread: Rename file during upload

  1. #1
    Join Date
    Oct 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Rename file during upload

    Could someone please help me with the code below, I want the file that is being uploaded to be renamed with a random number in addition to the unix timestamp to prevent a filename from ever being duplicated.

    I also want to check that files being uploaded are .jpg, .jpeg, .gif, or .png

    I've been trying for ages done a lot of googling but can't seem to get it to work for me.

    Code:
    echo 'Upload result:<br>'; 
    $uploaddir = dirname($_SERVER['SCRIPT_FILENAME'])."/UploadedFiles/";
    
    $target_encoding = "ISO-8859-1";
    echo '<pre>';
    if(count($_FILES) > 0)
    {
    	$arrfile = pos($_FILES);
    	$uploadfile = $uploaddir . iconv("UTF-8", $target_encoding,basename($arrfile['name']));
    
    	if (move_uploaded_file($arrfile['tmp_name'], $uploadfile))
    	   echo "File is valid, and was successfully uploaded.\n";
    }
    else
    	echo 'No files sent. Script is OK!'; 
    echo 'Here is some more debugging info:';
    print_r($_FILES);
    
    echo "</pre>";
    
    ?>

  2. #2
    Join Date
    Oct 2008
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Gm -

    Try this for the Required Filetypes:

    Code:
    $allowed_filetypes = array('.jpg','.gif','.jpeg','.png');  
    
    // Get the name of the file (including file extension).
    $filename = $_FILES['userfile']['name'];
    
    // Get the extension from the filename.
     $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
    
     // Check if the filetype is allowed, if not DIE and inform the user.
       if(!in_array($ext,$allowed_filetypes))
          die('The file you attempted to upload is not allowed.');

Bookmarks

Posting Permissions

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