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