luigimia Posted March 11, 2012 Share Posted March 11, 2012 Hi. I have a script here that will let users upload an image to my website but I just can't figure out how to save the uploaded image as "upload/logo.png" so that it will replace the already existing "upload/logo.png". Help would be greatly appreciated. <html> <body> <form action="" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> <?php if(isset($_POST['submit']) && !empty($_FILES["file"]["name"])) { $timestamp = time(); $target = "upload/"; $target = $target . basename($_FILES['uploaded']['name']) ; $ok=1; $allowed_types = array("image/gif","image/jpeg","image/pjpeg","image/png","image/bmp"); $allowed_extensions = array("gif","png","jpg","bmp"); if ($_FILES['file']['size'] > 350000) { $max_size = round(350000 / 1024); echo "Your file is too large. Maximum $max_size Kb is allowed. <br>"; $ok=0; } if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; $ok=0; } else { $path_parts = pathinfo(strtolower($_FILES["file"]["name"])); if(in_array($_FILES["file"]["type"],$allowed_types) && in_array($path_parts["extension"],$allowed_extensions)){ $filename = $timestamp."-".$_FILES["file"]["name"]; echo "Name: " . $filename . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; $path_parts = pathinfo($_FILES["file"]["name"]); echo "Extension: " . $path_parts["extension"] . "<br />"; echo "Size: " . round($_FILES["file"]["size"] / 1024) . " Kb<br />"; //echo "Stored in: " . $_FILES["file"]["tmp_name"]. " <br />"; } else { echo "Type " . $_FILES["file"]["type"] . " with extension " . $path_parts["extension"] . " not allowed <br />"; $ok=0; } } if($ok == 1){ @move_uploaded_file($_FILES["file"]["tmp_name"], $target . $filename); $file_location = $target . $filename; if(file_exists($file_location)){ echo "Uploaded to <a href='$file_location'>$filename</a> <br />"; } else { echo "There was a problem saving the file. <br />"; } } } else { echo "Select your file to upload."; } ?> Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/258668-replace-image-with-an-uploaded-one/ Share on other sites More sharing options...
scootstah Posted March 11, 2012 Share Posted March 11, 2012 move_uploaded_file will overwrite existing files. Quote Link to comment https://forums.phpfreaks.com/topic/258668-replace-image-with-an-uploaded-one/#findComment-1326030 Share on other sites More sharing options...
luigimia Posted March 11, 2012 Author Share Posted March 11, 2012 move_uploaded_file will overwrite existing files. I tried but just kept on getting "There was a problem saving the file". Could someone please show me how to do it? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/258668-replace-image-with-an-uploaded-one/#findComment-1326031 Share on other sites More sharing options...
scootstah Posted March 11, 2012 Share Posted March 11, 2012 The problem is here: @move_uploaded_file($_FILES["file"]["tmp_name"], $target . $filename); $file_location = $target . $filename; if(file_exists($file_location)){ echo "Uploaded to <a href='$file_location'>$filename</a> <br />"; } else { echo "There was a problem saving the file. <br />"; } Having the "if file_exists" bit after move_uploaded_file is entirely pointless, because the operation already happened. Regardless, even though you get this "error", it should still have overwritten the file. You need to remove the "if file_exists" block if you want to overwrite files with existing name. Quote Link to comment https://forums.phpfreaks.com/topic/258668-replace-image-with-an-uploaded-one/#findComment-1326033 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.