Stusara Posted February 8, 2016 Share Posted February 8, 2016 Hi Im trying to put a link on to upload an image.. I have the code and its working fine.. BUT.. i want to change it slightly so the image file is changed so it does not give the error "Sorry, file already exists." i manage to remove this but then it just over writes the existing image... Can some one help with a simple piece of code that will change the image file name on upload even if its just a number after the file name will suffice... Here is my current code: thanks in advance! Stu <?php $target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); // Check if image file is a actual image or fake image if(isset($_POST["submit"])) { $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if($check !== false) { echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } } // Check if file already exists if (file_exists($target_file)) { echo "Sorry, file already exists."; $uploadOk = 0; } // Check file size if ($_FILES["fileToUpload"]["size"] > 10000000) { echo "Sorry, your file is too large."; $uploadOk = 0; } // Allow certain file formats if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) { echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; // if everything is ok, try to upload file } else { if ( move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "upload/".$username.".".$extension)) { echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded."; } else { echo "Sorry, there was an error uploading your file."; } } ?> Quote Link to comment Share on other sites More sharing options...
requinix Posted February 8, 2016 Share Posted February 8, 2016 If you don't want it overwriting then what do you mean by having the image "changed"? Quote Link to comment Share on other sites More sharing options...
Stusara Posted February 8, 2016 Author Share Posted February 8, 2016 Just a name change on the image so it doesn't overwrite the file if someone uploads the same named file. I have seen some code that does this and adds numbers to the file name which would be handy i.e image01.png image02.png etc Quote Link to comment Share on other sites More sharing options...
Stusara Posted February 8, 2016 Author Share Posted February 8, 2016 Just a name change on the image so it doesn't overwrite the file if someone uploads the same named file. I have seen some code that does this and adds numbers to the file name which would be handy i.e image01.png image02.png etc Excuse my longwinded reply .. pretty new to this Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted February 8, 2016 Share Posted February 8, 2016 If you are concerned with duplicate file names try adding a timestamp to the beginning using time() 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.