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... Link to comment https://forums.phpfreaks.com/topic/229792-overwrite-file-on-upload/ 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? Link to comment https://forums.phpfreaks.com/topic/229792-overwrite-file-on-upload/#findComment-1183652 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 Link to comment https://forums.phpfreaks.com/topic/229792-overwrite-file-on-upload/#findComment-1183696 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(); } ?> Link to comment https://forums.phpfreaks.com/topic/229792-overwrite-file-on-upload/#findComment-1183700 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); } Link to comment https://forums.phpfreaks.com/topic/229792-overwrite-file-on-upload/#findComment-1183701 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. Link to comment https://forums.phpfreaks.com/topic/229792-overwrite-file-on-upload/#findComment-1183706 Share on other sites More sharing options...
ja_blackburn Posted March 6, 2011 Author Share Posted March 6, 2011 Thank you. Link to comment https://forums.phpfreaks.com/topic/229792-overwrite-file-on-upload/#findComment-1183711 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.