PDA

View Full Version : replace ...


HormonX
10-29-2002, 01:50 AM
Here is another question,

i have in my form a field where user can upload a file at the same time i have ascript that generates a random string. Not since there might be a user that will try to upload an image that already exists i came up with an idea, but getting to work that's where the problem lies.

Let's say i have image image.jpg or image.gif ( bothat file types are allowed. Now i have that randomly generated string abcdef how do i or what function do i use to substitue image.jpg or image.gif with abcdef.jpg / abcdef.gif.

I hope that wasn't too confusing :)

All help would be apreciated :)

HormonX

Nightfire
10-29-2002, 03:55 AM
if(file_exists("image.jpg")){
rename(image.jpg,abcdef.jpg);
}elseif(file_exists("image.gif")){
rename(image.gif,abcdef.gif);
}else{
echo 'No file found';
}

HormonX
10-29-2002, 09:37 PM
thanx for that tip .. but that wasn't something i was looking for,

i need to replace the filename weather it exists or not.

That random string that's being generated will be unique for every user that submits the form. So in fact am looking for a way to change the filename not renameing it.

Allow me to demonstrate. :)

- Generated random string is: asdfgh

- user fills the form and upload a file image.jpg

- filename is being renamed before is being saved asdfgh.jpg

- than for information is being inserted in to the database along with the new filename asdfgh.jpg

so what am looking for infact is a way to replace image with asdfgh

that's pretty much it :)

thanx again for all your help

HormonX

Nightfire
10-30-2002, 02:09 AM
By the speed of replies :p I guess it looks like you can't change the name before it's saved.

Ökii
10-30-2002, 08:38 AM
You'd do best running the renaming within the move_uploaded_file call.
I assume you are restricting the possible files to those with an image header - or simply just checking whether they are .gif/.jpg/.png -
You will have a variable that holds the original filename from the client - eg image.jpg - this is the variable that you want to rip apart and part use for renaming.

Assuming you new random string is $randi...

move_uploaded_file($userfile,'/usr/www/site/'.$randi.substr($userfile_name,-4));

note the substr call, which just takes the originating name (if memory serves me, it will be referenced as $[formfieldname]_name ) and takes the last four characters - ie it takes '.jpg' if image.jpg was uploaded.

HormonX
10-31-2002, 04:06 PM
it works like magic :) .. thanx a bunch ... this really saves me.

Thanx again Ökii

HormonX