Forum Moderators: coopster

Message Too Old, No Replies

upload file script

want to change file name on upload

         

mooger35

6:32 pm on Aug 30, 2006 (gmt 0)

10+ Year Member



This is what I have and it uploads files just fine:
$path = "path/to/folder/";
$max_size = 2000000;

if (!isset($HTTP_POST_FILES['userfile'])) exit;

if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {

if ($HTTP_POST_FILES['userfile']['size']>$max_size) { echo "The file is too big<br>\n"; exit; }
if (($HTTP_POST_FILES['userfile']['type']=="image/gif") ¦¦ ($HTTP_POST_FILES['userfile']['type']=="image/pjpeg") ¦¦ ($HTTP_POST_FILES['userfile']['type']=="image/jpeg") ¦¦ ($HTTP_POST_FILES['userfile']['type']=="image/jpg")) {

if (file_exists($path . $HTTP_POST_FILES['userfile']['name'])) { echo "The file already exists<br>\n"; exit; }

$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
$HTTP_POST_FILES['userfile']['name']);
if (!$res) { echo "upload failed!<br>\n"; exit;
} else {
echo "upload sucessful<br>\n";

echo "File Name: ".$HTTP_POST_FILES['userfile']['name']."<br>\n";
echo "File Size: ".$HTTP_POST_FILES['userfile']['size']." bytes<br>\n";
echo "File Type: ".$HTTP_POST_FILES['userfile']['type']."<br>\n";
}
else { echo "Wrong file type<br>\n"; exit; }
}

What I would like is to change the file name to something like this: 0001-1156962703.jpg (or whatever image ext)

0001 = $_GET['playerid']
1156962703 = time()

Can someone point me to the correct spot to insert this change in the code and what change needs to be made?

Thank you

jatar_k

7:03 pm on Aug 30, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



this line

$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
$HTTP_POST_FILES['userfile']['name']);

replace $HTTP_POST_FILES['userfile']['name'] with the name you want, so maybe

$newfilename = $_GET['playerid'] . '-' . time() . '.jpg';

$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
$newfilename);

something like that

mooger35

8:58 pm on Aug 30, 2006 (gmt 0)

10+ Year Member



thanks... I just needed to make sure I put the correct type of file (JPG or GIF) and it worked perfectly.