View Full Version : change file name upon upload

02-07-2003, 12:49 AM
I'd like to alter this code so that the photo uploaded is renamed with the string value of $f_userkey so it comes out like myusername.gif (or.jpg) I'd also like to load a string indicating the file type (.gif or .jpg) into mysql as $picsuffix so i can call it later as
$photoname = /path/dir/.$f_userkey.$picsuffix

Is that retarded? If not... how do i do it?

if (is_uploaded_file($HTTP_POST_FILES['f_photo']['tmp_name'])) {
if ($HTTP_POST_FILES['f_photo']['size']>$max_size) {
error("The file is too big");
if (($HTTP_POST_FILES['f_photo']['type']=="image/gif") || ($HTTP_POST_FILES['f_photo']['type']=="image/pjpeg") || ($HTTP_POST_FILES['f_photo']['type']=="image/jpeg")) {

$res = copy($HTTP_POST_FILES['f_photo']['tmp_name'], $imagepath .

if (!$res) {
error("upload failed!");

else {
error("Wrong file type. Please use JPG or GIF");


Dylan Leblanc
02-07-2003, 06:54 AM
Use something like this:

move_uploaded_file($HTTP_POST_FILES['f_photo']['tmp_name'], '/path/dir/' . $f_userkey . $picsuffix)

That will do all the moving and renaming for you.

(I dont know why the forum put s line break in "tmp", it shouldn't be there).