PHP使用move_uploaded_file上传图像两次(不应该上传图像)

时间:2018-10-11 12:22:16

标签: javascript php html forms file

我创建了一个表单,您可以在其中上传这样的图像

	<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>

现在使用php上传图片:

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";
        }
    }

}

我对此进行了多次测试,每次提交表单时,它都会以2个不同的数字上传同一张图片。假设在目录中有0张图片,我将提交表单一次,它将在img [0] .png和img [1] .png下再次上传同一张图片两次,其中图片再次相同。为什么代码运行两次?有人可以帮助我吗?谢谢。

1 个答案:

答案 0 :(得分:4)

您正在重新加载表单提交页面:onSubmit="window.location.reload();",这是在重新加载时再次提交表单。尝试删除onSubmit="window.location.reload();"