Jump to content

Change the name of uploaded file


frobak

Recommended Posts

Hi

 

I need to change the name of a file being uploaded by a user. The reason i need this is because there is a strong possibility that duplicate filenames would be logged.

 

This is the code i have currently:

 

   $upload_path = 'cv/'; // The place the files will be uploaded to (currently a 'files' directory).

   $filename = $_FILES['userfile']['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 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.');

   // Upload the file to your specified path.
   if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename));

 

This code works fine to upload the file in the current name.

 

I assume i need to seperate the filename from the file extension, and i can then assign a new variable to the filename. Easier said than done though as Ive tried many combinations of things.

 

Is there a simple way using this script? or will i need to start from scratch?

 

Cheers

Link to comment
Share on other sites

Hi

 

Yeah the problem is that i need to change the filename of the uploaded file, not the extension, just the filename. or even just append a user id to the beggining of the filename.

 

CV's are being uploaded by users, so i would need to chnage the fielname or I could have many duplicate filenames, people uploading cv.doc for example.

 

pathinfo() doesnt split the extension?

Link to comment
Share on other sites

Something like this will work.

 

<?PHP

  //### The place the files will be uploaded to (currently a 'files' directory).
  $upload_path = 'cv/';

  //### Get the name of the file (including file extension).
$filename = $_FILES['userfile']['name'];

//### Explode the periods
  $explode = explode('.',$filename);
  //### Get extension of the filename
  $ext = $explode[(count($explode)-1)];
  
  //### Create a new filename - Do what you like here
  $newFilename = md5(microtime(true));
  //### Merge the file name, with the file extension
  $filename = $newFilename.'.'.$ext;

//### 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.');
}

//### Upload the file to your specified path.
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)) {
  echo 'File was moved';
} else {
  echo 'File was not moved.';
}

?>

 

Regards, PaulRyan.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.