简体   繁体   中英

PHP uploading images twice (when it shouldn't) with move_uploaded_file

I created a form where you upload an image like this

  <form class="form" method="post" action="recipeUpload.php" enctype="multipart/form-data" autocomplete="off" onSubmit="window.location.reload();"> <fieldset style="margin-bottom: 70px;padding-top: 70px;"> <legend style="font-size: 36px;">פרטים אישיים</legend> <input class="inForm" type="text" name="name" placeholder="שם" required/> <input class="inForm" type="text" name="sur-name" placeholder="שם משפחה" required/> </fieldset> <fieldset style="margin-bottom: 70px;padding-top: 70px;"> <legend style="font-size: 36px; ">פרטי המתכון</legend> <fieldset class="inForm" style="width:max-content;"> <legend style="font-size: 36px">העלו תמונה של המתכון</legend> <label class="action" id="bb">בחרו תמונה <input id="file" class="inForm" type="file" name="myfile" accept="image/*" required> </label> <div id="name"></div> </fieldset> <input class="inForm" type="text" name="time" placeholder="זמן הכנה" required/> <input class="inForm" type="text" name="meal" placeholder="שם המנה" required/> <label class="inForm">בחרו קטגוריה מתאימה</label> <select class="inForm" name="category" required/> <option value="מנות ראשונות">מנות ראשונות</option> <option value="בשר">בשר</option> <option value="פחמימות">פחמימות</option> <option value="דברי חלב">דברי חלב</option> <option value="צמחוני">צמחוני</option> <option value="סלטים">סלטים</option> <option value="קינוחים">קינוחים</option> <option value="אחר">אחר</option> </select> </fieldset> <fieldset style="margin-bottom: 70px;padding-top: 70px;"> <legend style="font-size: 36px;">המתכון עצמו</legend> <textarea id="ingredients" name="ingredients" class="inForm long-text" placeholder="רכיבים" required>רכיבים:</textarea> <textarea id="directions" name="directions" class="inForm long-text" placeholder="אופן ההכנה" required>אופן ההכנה:</textarea> </fieldset> <input class="inForm action" id="submit" type="submit" name="submit" value="שלחו את המתכון שלכם"> </form> 

Now the php for uploading the images:

if(isset($_POST['submit'])){


        echo "<label class='inForm'> Your messege has been sent. Thank You!</label>";
         $errors = []; // Store all foreseen and unforseen errors here
$fileName = $_FILES['myfile']['name'];
$fileSize = $_FILES['myfile']['size'];
$fileTmpName  = $_FILES['myfile']['tmp_name'];
$fileType = $_FILES['myfile']['type'];
$tmp =explode('.',$fileName);
$fileExtension = strtolower(end($tmp));
$directory = getcwd(). "/uploads/"; $files = scandir($directory);



$num_files=count($files)-2;

    if ($fileSize > 2000000) {
        $errors[] = "This file is more than 2MB. Sorry, it has to be less than or equal to 2MB";
    }

    if (empty($errors)) {
          $uploadPath = getcwd() ."/uploads/img[" .$num_files. "].". $fileExtension;
       if(move_uploaded_file($_FILES['myfile']['tmp_name'], $uploadPath)) {

} else{
    echo "There was an error uploading the file, please try again!";
}
    } else {
        foreach ($errors as $error) {
            echo $error . "These are the errors" . "\n";
        }
    }

}

I tested this many times, and every time I submit the form it uploads the same image under 2 different numbers. let's say there where 0 images in the directory and I would submit the form once it would upload the same image twice under img[0].png and img[1].png where the image is again the same. Why is the code running twice? can sombody help me? Thank you.

You are reloading the page on form submit: onSubmit="window.location.reload();" , this is submitting the form again on reload. Try removing onSubmit="window.location.reload();" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM