0

i want to upload image with numerics at end of the file name or something unique, if duplicate file name found in directory. like if i upload imag.jpg it should be some uniqueid+imag.php . i researched multiple resources but now almost confused. this is default reference code from w3schools. as i am new and learn from there and by practise, so,any help it will be thank you.

    if($_POST){

$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["file"]["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"] > 500000) {
    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["file"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
        $pptimg =  $_FILES["file"]["name"];
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

}
imgrv
  • 99
  • 3
  • 17

2 Answers2

0

Save the file with a time stamp that includes seconds.

The chances of another file having the same file name will be low.

  • I am unsure as to what you want to do exactly. Are you uploading it to a folder or a website ? Are you generating these images your self ? Depending on the job you want done there could be multiple ways of doing it. –  Jan 21 '18 at 17:54
  • i am uploading this to a folder on server and saving its path to database using form input. so can preview it later on. – imgrv Jan 21 '18 at 18:03
  • Cool, May I ask how the image files are generated and is there any particular reason for your method ? Would be worth looking at other options also :) . I might have some alternative ideas. Although Gonzalo De-Spirito might have you covered :) –  Jan 21 '18 at 18:19
  • @nerdiere : i am not getting the sentence, how the image files are generated. well, i have created 3 step form and a preview page where we get all the submitted data and image for confirmation. on 3rd page of form we are taking image input and after submit it will go to a php page where this phpupload code is written, after processing and saving its path to db it will redirect to preview.php page. – imgrv Jan 21 '18 at 18:41
  • @nerdiere : well buddy ! i followed Gonzalo De-Spirito answer and it worked and files getting save into directory but how i will save this new image path to DB, it is storing the default name of file, as i am using variable $pptimg = $_FILES["file"]["name"]; what should i do here. – imgrv Jan 21 '18 at 18:45
  • I found this , you will need to connect to the database and make a query. You may want to rewrite this to suite your needs. https://stackoverflow.com/questions/17153624/using-php-to-upload-file-and-add-the-path-to-mysql-database . –  Jan 21 '18 at 18:53
  • thanxx ! got it. – imgrv Jan 21 '18 at 22:00
0

You can use uniqid() php function to do this.

$target_file = $target_dir . uniqid() . basename($_FILES["file"]["name"]);

  • Thank Buddy! it is working, remaned file saved to directory. but how to save the path to database. currentlly i am storing this variable $pptimg = $_FILES["file"]["name"]; to database. what should i change here to store new image to db. – imgrv Jan 21 '18 at 18:24
  • Its Working Perfectly. got it and was able to update it to db. – imgrv Jan 21 '18 at 22:01