Forum Moderators: coopster

Message Too Old, No Replies

Changing File Upload Names For Security

Changing File Upload Names For Security

         

dc_duo

7:18 pm on Feb 14, 2005 (gmt 0)

10+ Year Member



Hi all wonder if anyone can help me. I am currently writing a site however I have one small problem for the life of me I do not know how to change a files name when it is being uploaded. I use the following bit of code for uploading files.

if (is_uploaded_file($_FILES['image1']['tmp_name'])){

if (move_uploaded_file($_FILES['image1']['tmp_name'], "../uploads_golf/{$_FILES['image1']['name']}")) {

$problem = FALSE;

echo '<p align="center"><font color="red"><b>The file was uploaded</b></font></p>';

} else {

$problem = TRUE;

echo '<p>The file could not be moved because </p>';

$img_name1 = 'noimage.jpg' ;

}

$img_name1 = $_FILES['image1']['name'];

} else {

$img_name1 = 'noimage.jpg';

}

What I want to do is change the file name so when it is uploaded it is changed whether it be use of the rand command or not but i cannot seem to do it. Is there a way that you can do this and then have it also go into the mysql table as the same name for instance:-

I upload a file in the form called me.jpeg

It goes throught the mothions but changes to 4093490939.jpg or something like that and then uploads the file as that and inserts into the MYSQL table as this.

Can this be done or not! I am not sure I have read up and this forum is the most helpful by a mile so please plese help

hakre

7:31 pm on Feb 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi dc_duo,

your problem can be solved by simply naming the move-to filename with a function you're already using:

move_uploaded_file($_FILES['image1']['tmp_name'], "../uploads_golf/{$_FILES['image1']['name']}")

just modify the second parameter and replace the name with the name you want to use (let's say it's the value of

$imagefilename
):

move_uploaded_file($_FILES['image1']['tmp_name'], "../uploads_golf/$imagefilname")

i think this should solve one part of your problem. the other maybe you don't know how to name the new file, right? do you want to create a random name for it (rand) or do you want to have it bound to a databases entry primary key?

dc_duo

10:22 pm on Feb 15, 2005 (gmt 0)

10+ Year Member



In an ideal world I would like to create a random name for it for example

$new_image_name = rand (0, 100000000) ;

and then use this variable as the new image name is this possible

hakre

10:01 am on Feb 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



this should be possible. it's not the complete truth, but you should put .jpg/.png/.gif at it's end, too. this should work fine then.