Jump to content

renaming a uploaded file


barrowvian

Recommended Posts

so far i have;

// Configuration - Your Options
$allowed_filetypes = array('.jpg','.gif','.bmp','.png'); // These will be the types of file that will pass the validation.
      
$max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).
$upload_path = $_COOKIE['tmp']; // upload files to. called from cookie tmp from newuser.php
$filename = $_FILES['userfile1']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

   // 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.');
  
   // Check if we can upload to the specified path, if not DIE and inform the user.
   if(!is_writable($upload_path))
      die('You cannot upload to the specified directory, please CHMOD it to 777.');

// Now check the filesize, if it is too large then DIE and inform the user.
if(filesize($_FILES['userfile1']['tmp_name']) > $max_filesize)
die('The file you attempted to upload is too large.');

// Upload the file to your specified path.
if(move_uploaded_file($_FILES['userfile1']['tmp_name'],$upload_path . $filename))
echo 'Upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a><br />';
else
echo 'There was an error during the file upload.  Please try again.';

 

Ive just been playing around with uploading images etc and have the basics working. Now I'd like to rename this file 'profile' and use its original extension. I know the rename function will be used here as Ive been looking around online for what Im looking for but I havent been able to successfully implement it yet. Please could someone help me out, thanks

 

Link to comment
https://forums.phpfreaks.com/topic/196820-renaming-a-uploaded-file/
Share on other sites

move_uploaded_file() is basically a renaming function (but it does a few other things too), so instead of

 

move_uploaded_file($_FILES['userfile1']['tmp_name'],$upload_path . $filename)

 

You could do

 

$pathinfo = pathinfo($_FILES['userfile1']['name']);
move_uploaded_file($_FILES['userfile1']['tmp_name'],$upload_path . 'profile.' . $pathinfo['extension'])

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.