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
https://forums.phpfreaks.com/topic/256693-change-the-name-of-uploaded-file/
Share on other sites

What is the main problem? To create a new filename in an easy name?

You may use pathinfo() function http://ru.php.net/manual/en/function.pathinfo.php in order to get all filename parts. Then you may change something and create a new filename.

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?

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.

instead of doing all that exploding, you can do this:

 

$info = pathinfo("/path/to/file.jpg");
$new_name = md5(time());
$filename = $info['dirname']."/".$new_name.".".$info['extension'];
move_uploaded_file($_FILES['userfile']['tmp_name'], $filename);

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.