Jump to content


Rename of uploaded file

  • Please log in to reply
2 replies to this topic

#1 brown2005

  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 12 March 2006 - 10:08 PM

I have a script that uploads a file from someones computer to my server.

say the file i have is 3.JPG

when i upload it, i want to change the name of the file to 1.JPG or 2.JPG to conincide with the id of the person uuploading the image.

any ideas please

toplay: Edited title to be more meaningful.

#2 hitman6003

  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 12 March 2006 - 10:15 PM

Post the code that you use to determine the userid and the code that you use to save the uploaded file...please don't post your entire script...we only need the relevant parts.

#3 k.soule

  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts
  • LocationIllinois

Posted 13 March 2006 - 02:36 AM

$upload = move_uploaded_file($_FILES['picture']['tmp_name'], "/home/www/htdocs/$id.jpg");

$_FILES['picture']['tmp_name'] is the temporary name on the server, change picture to the name of the form element that you are uploading with.

/home/www/htdocs/$id.jpg is the location, change /home/www/htdocs/ to match your file system structure, $id would be the variable holding the identification of the person and, of course, .jpg is the extension. $upload will be 1, if the upload was successful.

EDIT -> Let me clarify the above, ['tmp_name'] should not be changed, it stands for the temporary name that the server is using to store the uploaded file, uploaded files are always moved to a temporary file and then moved to a permanent location. It doesn't pass the filename the user put in, if you wanted that name, it would just be ['name']...but that is useless unless you want to preserve the same file name; the only element of that code that will need changed is ['picture'] and the location and variable of the final location, as underlined above.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users