Skip to main content
Inspiring
June 25, 2010
Question

change file name on upload in PHP

  • June 25, 2010
  • 1 reply
  • 4811 views

HI Folks,

I am trying to rename a file uploaded in a form using this.

But nothing happens. I can't figure out why but I'm sure you guys will see the problem straight away.

if(isset($_FILES['logo']['name']))

{$_FILES['logo']['name']=("".$row_rs_member['id']."jpg");}

Any ideas?

Dave

This topic has been closed for replies.

1 reply

June 25, 2010

Take a look at move_uploaded_file function:

http://php.net/manual/en/function.move-uploaded-file.php

In your case, something like this should work:

$filename = $row_rs_member['id'] . ".jpg";

$filepath = "/home/username/html/files/"; // CHANGE THIS WITH YOUR OWN PATH...

move_uploaded_file($_FILES['logo']['tmp_name'], $filepath . $filename);

davecheetAuthor
Inspiring
June 25, 2010

Thanks but I'm confused.

I don't want to move the file.

It's aldready being uploaded to the right folder, just with the existing name. `so if I can just change the filename before it's uploaded everything will work perfectly.

Isn't there just a straightforward way of renaming the file?

Dave

June 25, 2010

Yes there is:

http://www.php.net/manual/en/function.rename.php