Jump to content

Overwrite file on upload


ja_blackburn

Recommended Posts

Hello. i am building a facility for members of my website to upload profile pictures.

 

It works, but what I want to do is have the below script check and overwrite the previous file if they want to update it. As you will see it enforces the image to be named after the username.

 

function findexts ($filename) { $filename = strtolower($filename) ;
        $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts;
        }
        $ext = findexts ($_FILES['uploaded']['name']);

        //$ran = rand () ;
        $ran2 = $eo_user_name."."; 
        $target = "../images/eoprofile/"; //This assigns the subdirectory you want to save into... make sure it exists!
        $target = $target . $ran2.$ext;  //This combines the directory, the random file name, and the extension

         if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
             echo "The file has been uploaded as ".$ran2.$ext; }
             else { echo "Sorry, there was a problem uploading your file."; }

 

Help appreciated.

 

Thanks...

Link to comment
Share on other sites

My problem is that the script used to initially upload a member's profile picture is the same one that is used to update it (e.g. upload  a new one).

 

so the filename on the server will be essentially 'username.jpg' and what the script tries to do is re-upload 'username.jpg' and it doesn't work.

 

Is there some logic I can include to the effect of:

 

if (username.jpg already exists){

replace it

}else{

just upload as normal

}

 

???

 

Thanks

 

 

Link to comment
Share on other sites

Also when uploading a file. make sure the file exstention, is correct and not a .exe and others.

 

Always only let registred users upload files only.

 

Your be amazed, off hacks for files.

 

 

There even talk images, with code written on top of the image, to do spitefull things... seen no example yet.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.