Help!! - File rename problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Vintage83
Forum Newbie
Posts: 2
Joined: Fri Dec 16, 2005 3:41 am

Help!! - File rename problem

Post 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]
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
Vintage83
Forum Newbie
Posts: 2
Joined: Fri Dec 16, 2005 3:41 am

Post 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 ;-)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

echo out the old name/path and the new name/path to see what values they contain, then try your rename().
Post Reply