Page 1 of 1

Help!! - File rename problem

Posted: Fri Dec 16, 2005 3:44 am
by Vintage83
Hi,
I use the script below to rename the uploaded image files. THe script works fine but I would like to use as filename the product_id taken from my recordset.

When I try to use $row_prod['prod_id'] which is the value that returns my id in the $rename variable , i get a renamed file without name but just an extension ( .jpg)


The script:

Code: Select all

$rename = "Anything";
$old_name = $_FILES['Image']['name'];
$read_extension = explode(".", $old_name);
$ext = $read_extension[1];
$new_name = $rename.".".$ext;
rename("images/prod/".$old_name, "images/prod/".$new_name);
Any ideas??? :(

Burrito: Please use

Code: Select all

tags when [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting PHP Code In The Forums[/url][/size]

Posted: Fri Dec 16, 2005 5:01 am
by onion2k
If you're getting ".jpg" then clearly $rename is empty. You say that you've set it to the contents of $row_prod['prod_id'] .. that would imply that $row_prod['prod_id'] is empty too.

You should also be aware that the extension of a file is meaningless. Just because it ends in .jpg doesn't mean it actually is a .jpg file. Furthermore, the way your code is written, if I were to upload a file called virus.jpg.exe then you would save it in your website as "Anything.jpg" .. and users would download it .. I would recommend looking at the $_FILES['Image']['type'] upload data .. that gives you the MIME type of a file that's been uploaded. It's not completely bulletproof as it can still be faked, but it's much, much more reliable than the extension.

Posted: Fri Dec 16, 2005 11:21 am
by Vintage83
Thanks for the reply onion2k,

The $row_prod['prod_id'] is not empty. It gets the value from a URL parameter. When I try to put it in a table with an echo I can see its value, so i guess its ok but when i use it in the &rename variable I get a blank filename.
Whats wrong ??? :-(

There is no security problem because the file will be used only by the admin of the page. He will only upload images ;-)

Posted: Fri Dec 16, 2005 12:37 pm
by Burrito
echo out the old name/path and the new name/path to see what values they contain, then try your rename().