PHP 使用move_uploaded_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其中图像再次相同。为什么代码运行两次?索姆博迪能帮我吗?谢谢。

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

最新更新