-1

I went to give the unique name to picture and I save 2 picture and every picture name will be unique.

this code work correct but give the related name and one problem more when i refresh page picture upload again.

$video_type = trim($_GET['video_type']);
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            $target_dir = "pictures/";
            $name1 = $_FILES["img_url1"]["name"];
            $name2 = $_FILES["img_url2"]["name"];
            $target_file = $target_dir . basename($_FILES["img_url1"]["name"]);
            $target_filez = $target_dir . basename($_FILES["img_url2"]["name"]);

            $videoFileType = pathinfo($target_file,PATHINFO_EXTENSION);
            if (strtolower(end(explode(".",$name))) =="jpg") {
                $uploadOk = 1;
            } elseif(strtolower(end(explode(".",$name))) =="png" ) {
                $uploadOk = 1;
            }else {
                $uploadOk = 0;
            }

            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0) {
                $_SESSION['error'] = "Sorry, your file was not uploaded.";
            // if everything is ok, try to upload file
            } else {
                if (move_uploaded_file($_FILES["img_url"]["tmp_name"], $target_file) && move_uploaded_file($_FILES["img_urlz"]["tmp_name"], $target_filez)) {
                    $sql = "INSERT INTO picture (pic1, pic2, approved)
                    VALUES ('$name1', '$name2' , 'false')";

                    if ($db->query($sql) === TRUE) {
                        $_SESSION['success'] = 'picture uploaded successfully.';
                    } else {
                        $_SESSION['error'] = $sql . "<br>" . $db->error;
                    }
                } else {
                    $_SESSION['error'] = "Sorry, there was an error uploading your file.";
                }
            }
        }
Awais
  • 1
  • 3

4 Answers4

0

try this one

//get picture extension
$ext = pathinfo($_FILES['file']['name'])['extension'];
 //generate the new random string for filename and append extension.
$name1 = generateRandomString().".$ext";

function generateRandomString($length = 10) {
  return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"), 0, $length);
}

OR

$name1 = time().'_'.$_FILES['file']['name'];
Bhargav Chudasama
  • 6,928
  • 5
  • 21
  • 39
0

please update your code

$new_name1 = $location.time()."-".rand(1000, 9999)."-".$name1;
$new_name2 = $location.time()."-".rand(0, 9999)."-".$name2;

then replace $name1 with $new_name1 & $name2 with $new_name2; this will give you a unique name.

or you can use:

// Make sure this imagePath is end with slash

 $imagePath = '/image/folder/';
 $uniquesavename=time().uniqid(rand());
 $destFile = $imagePath . $uniquesavename . '.jpg';
 $filename = $_FILES["img"]["tmp_name"];
 list($width, $height) = getimagesize( $filename );       
 move_uploaded_file($filename,  $destFile);
Negi Rox
  • 3,828
  • 1
  • 11
  • 18
0

Hi you can replace these 2 lines of code to

$name1 = $_FILES["img_url1"]["name"] . uniqid();
$name2 = $_FILES["img_url2"]["name"] . uniqid();

will always generate the unique names.

Hope this helps.

Mirza
  • 148
  • 6
0

You can use PHP inbuilt

uniqid()

function to create unique id and append into you your image path.

$name1 = uniqid().$_FILES["img_url1"]["name"];
$name2 = uniqid().$_FILES["img_url2"]["name"];
$target_file = $target_dir . basename(uniqid().$_FILES["img_url1"]["name"]);
$target_filez = $target_dir . basename(uniqid().$_FILES["img_url2"]["name"]);

output:

$name1 : 5a4b2e0ce944dfilename.ext
$name2 : 5a4b2e0ce948cfilename.ext
$target_file = 5a4b2e0ce944dfilename.ext
$target_filez = 5a4b2e0ce948cfilename.ext
Raju
  • 160
  • 2
  • 10
  • Thanks bro. work correct but one problem remain when I refresh page picture upload again. – Awais Jan 02 '18 at 07:28
  • If you are submitting this upload form using PHP, then follow this post https://stackoverflow.com/questions/5690541/best-way-to-avoid-the-submit-due-to-a-refresh-of-the-page – Raju Jan 02 '18 at 08:59