ja_blackburn Posted March 6, 2011 Share Posted March 6, 2011 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... Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 6, 2011 Share Posted March 6, 2011 I don't really see a question in there. Are you having a problem with a particular part of it? Quote Link to comment Share on other sites More sharing options...
ja_blackburn Posted March 6, 2011 Author Share Posted March 6, 2011 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 Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 6, 2011 Share Posted March 6, 2011 what about. <?php $filename = $username; if (file_exists($filename)) { die(); } ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 6, 2011 Share Posted March 6, 2011 Before you attempt to move_uploaded_file(), check if it already exists, and if so, unlink() it. if( file_exists($target) ) { unlink($target); } Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 6, 2011 Share Posted March 6, 2011 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. Quote Link to comment Share on other sites More sharing options...
ja_blackburn Posted March 6, 2011 Author Share Posted March 6, 2011 Thank you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.