Skip to content
Advertisement

upload image by refreshing page of web site

i write php code that upload image from client to host and site and my problem is if i refresh the page ,the last file going to upload again and its going dupplicate and if refresh again this problem happen again

my code:

<?php
$target_dir = 'C:xampphtdocsuploaduploadjpg\';
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$filename=basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$move=$_FILES["fileToUpload"]["tmp_name"];
 $isss=1;

// 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  - " . $check["mime"] . ".";
//     $uploadOk = 1;
//   } else {
//     echo "File is not an image.";
//     $uploadOk = 0;
//   }
// }
$time =date("Y-m-d-h-i-sa");
 
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" ) {
  echo "jpg png jpeg mojaz hast";
  $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "Upload nashod";
// if everything is ok, try to upload file

} else {
  
  if (move_uploaded_file($move, $target_file) ) {
   
  echo "<div class=dvs>". "File: ". basename( $_FILES["fileToUpload"]["name"]). " Upload shod !"."</div>";
  
  rename("$target_file",$target_dir.$time.".jpg");
 
  
  }
  
  else {
    echo "moshkeli dar upload shoma hast.";
  }
//   foreach(glob('jpg/*.jpg') as $filename1){
//     echo $filename1;
// }
  
}
?>

Advertisement

Answer

The problem is occurring because you stay on the page that is processing the POST request. What you need to do is to redirect to another page or the page where you are uploading from, and show the result of the upload there.

You can redirect by using the header function like this:

header('Location: ' . $the_url);

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement