Jump to content

rename on upload


gurechan

Recommended Posts

Hi, I am trying to set up an upload script in php using:

$updir = "../files/";
$file = $_FILES['file']['name'];
if(!(copy($_FILES['file']['tmp_name'], $updir . $_FILES['file']['name']))) die("Cannot upload files.");

What I want to do though is rename the file on upload. For examle:

From: abc.some_file_extension
To: 123.some_file_extension

Can anyone point me in the right direction?
Link to comment
https://forums.phpfreaks.com/topic/13951-rename-on-upload/
Share on other sites

When I upload a file I tend to do rename it with the move_uploaded_file() function.

[code]
<?php
if(move_uploaded_file($_FILES['file']['tmp_name'], $updir . $newFilename)) {
  // file was moved and renamed
} else {
  // There was an error moving the file
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/13951-rename-on-upload/#findComment-54405
Share on other sites

$updir = "../files/";
$file = explode('.',$_FILES['file']['name']);
$extention=count($file);
$newfile='new_file_name'.'.'.$file[$extention];
if(!(copy($_FILES['file']['tmp_name'], $updir . $newfile))) die("Cannot upload files.");

That should do the job not sure if the quickest way and not tested..

The count($file) is incase the file has any .'s in the name so it will always use the last text after the .

sorry wrote this and SharkBait replyed.. just thought i would post anyway as this will make it so you keep the extention of the file..

Regards
Liam
Link to comment
https://forums.phpfreaks.com/topic/13951-rename-on-upload/#findComment-54408
Share on other sites

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.