Log in

View Full Version : File Upload - Setting a Name



Medyman
01-14-2008, 05:41 PM
Hey guys...

How do I set a filename using the code below? I'd like for the image to be called header.jpg and overwrite the existing file of the same name.


if (!isset($HTTP_POST_FILES['banner'])) exit;
if (is_uploaded_file($HTTP_POST_FILES['banner']['tmp_name'])) {
if ($HTTP_POST_FILES['banner']['size']>$max_size) { echo "The file is too big"; exit; }
if (($HTTP_POST_FILES['banner']['type']=="image/jpeg")) {
$res = copy($HTTP_POST_FILES['banner']['tmp_name'], $path .
$HTTP_POST_FILES['banner']['name']);

if (!$res) { $msg2 = "Upload Error"; exit; }
else { $msg = "Update Processed"; }

}
else { echo "Wrong file type"; exit; }
}
$my_file = $HTTP_POST_FILES['banner']['name'];

djr33
01-14-2008, 11:59 PM
Looks like it's $HTTP_POST_FILES['banner']['name']);

Really, you can just copy the code using move_uploaded_file() from php.net here:
http://www.php.net/manual/en/features.file-upload.php

alexjewell
01-15-2008, 11:26 AM
I'd also use image/jpg in there just to be safe:



if (($HTTP_POST_FILES['banner']['type']=="image/jpeg") || ($HTTP_POST_FILES['banner']['type']=="image/jpg")) {


The other thing is that you can use $_FILES instead of $HTTP_POST_FILES...which, I think is a lot easier. But your server and/or PHP version may not support that.

Anyway, just throwing those things out there.

Medyman
01-15-2008, 02:06 PM
Thanks guys...