suttercain Posted August 27, 2007 Share Posted August 27, 2007 Hi guys, I have a file upload form and the file is moved and renamed based on the users id number. Example userId #150 = 150.jpg. How can I allow the user to upload, using the same form, but replace the existing file so it will still read 150.jpg but with a new image (replacing the old one). FORM <form enctype="multipart/form-data" action="uploadedProfilePic" method="post"> <input name="logoImage" type="file"><br> <input type="submit" name="submit" value="Submit"> </form> PHP <?php session_start(); //DO NOT REMOVE!!!! ?> <?php if ($_SESSION['merchant'] == 1) { ?> <?php require('../database.php'); if ($_FILES['logoImage']['size'] < 250000) { if ($_FILES['logoImage']['type'] === "image/gif" || $_FILES['logoImage']['type'] === "image/jpg" || $_FILES['logoImage']['type'] === "image/jpeg") { $sqlImage = mysql_query("SELECT merchantId FROM merchants WHERE username = '".$_SESSION['user']."'") or die(mysql_error($connect)); $image = mysql_fetch_row($sqlImage); $imageName = $image['0']; // PROCESS IMAGE $target_path = "../profileimages/"; $target_path = $target_path . basename( $_FILES['logoImage']['tmp_name']); $_FILES['logoImage']['tmp_name']; //MOVE THE IMAGE TO THUMBNAILS move_uploaded_file($_FILES['logoImage']['tmp_name'], $target_path); //EXPLODE TO RENAME IMAGE $fileName = $_FILES['logoImage']['name']; $broken = explode(".", $fileName); rename("../profileimages/".basename($_FILES['logoImage']['tmp_name'])."", "../profileimages/".$imageName."." .$broken[1].""); //Upload Image Name Into Table $update = mysql_query("UPDATE merchants SET profilePic='" .$imageName."." .$broken[1]. "' WHERE merchantId='".$imageName."'" ) or die(mysql_error()); $_SESSION['message'] = "Logo Uploaded!"; echo "<script type=\"text/javascript\">document.location.href='logged.php'</script>"; } else { echo "The image file must be in .gif, .jpg or .jpeg format."; $passed = FALSE; } } else { echo "The file was too large!"; } } else { echo "You are not authorized to view this page."; } ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/66825-best-way-to-replace-a-file/ Share on other sites More sharing options...
btherl Posted August 27, 2007 Share Posted August 27, 2007 See this warning here: "If the destination file already exists, it will be overwritten." The default behaviour is to replace the file. Quote Link to comment https://forums.phpfreaks.com/topic/66825-best-way-to-replace-a-file/#findComment-335049 Share on other sites More sharing options...
suttercain Posted August 27, 2007 Author Share Posted August 27, 2007 Hi Btherl, Using the code above does not overwrite the file though. Is it because I rename the file after the move to the directory? SC Quote Link to comment https://forums.phpfreaks.com/topic/66825-best-way-to-replace-a-file/#findComment-335246 Share on other sites More sharing options...
btherl Posted August 27, 2007 Share Posted August 27, 2007 Aha. So is the problem you are having that the rename() fails when the destination file already exists? Quote Link to comment https://forums.phpfreaks.com/topic/66825-best-way-to-replace-a-file/#findComment-335259 Share on other sites More sharing options...
suttercain Posted August 27, 2007 Author Share Posted August 27, 2007 I believe so. This is what the current code does. User Uploads > File is Moved to Directory > File is renamed based on the userId and the extension type (exp. jpg, gif). When a user "re" uploads a photo nothing happens. The form seems to process but no "new" file is placed in the directory. Instead the old file stays the same. SC Quote Link to comment https://forums.phpfreaks.com/topic/66825-best-way-to-replace-a-file/#findComment-335269 Share on other sites More sharing options...
suttercain Posted August 27, 2007 Author Share Posted August 27, 2007 Do you think it would be "safe" to delete the existing file, with unlink, before uploading the new file. In essence replacing the old file? //find the file based on userId $myFile = $row['fileName']; $fh = fopen($myFile, 'w') or die("can't open file"); fclose($fh); //Uplaod file code here Quote Link to comment https://forums.phpfreaks.com/topic/66825-best-way-to-replace-a-file/#findComment-335279 Share on other sites More sharing options...
chocopi Posted August 27, 2007 Share Posted August 27, 2007 Thats what I do for one of my pages <?php $old_file = 'avatars/'.$page_id.'.gif'; if(file_exists($old_file)) { unlink($old_file); } copy ($_FILES['avatar']['tmp_name'], "avatars/".$page_id.".".$file_extension) or die("Your avatar could not be copied correctly!"); ?> ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/66825-best-way-to-replace-a-file/#findComment-335319 Share on other sites More sharing options...
suttercain Posted August 27, 2007 Author Share Posted August 27, 2007 Hey Chocopi, Yeah I was thinking along those same lines. I will give it a shot tonight. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/66825-best-way-to-replace-a-file/#findComment-335327 Share on other sites More sharing options...
btherl Posted August 27, 2007 Share Posted August 27, 2007 Before fixing the problem, it would be good to verify that it really is a problem Check that rename() returns false (indicating failure to rename). If that's the case, then chocopi's approach should work. If you're ultra paranoid, you may want to make a backup of the file you're deleting, just in case. Quote Link to comment https://forums.phpfreaks.com/topic/66825-best-way-to-replace-a-file/#findComment-335766 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.