【问题标题】:PHP uploading images twice (when it shouldn't) with move_uploaded_filePHP 使用 move_uploaded_file 上传图像两次(不应该)
【发布时间】:2018-10-11 12:22:16
【问题描述】:

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

	<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 下两次上传相同的图像,图像再次相同。为什么代码运行两次?有人可以帮助我吗?谢谢。

【问题讨论】:

  • 一些合理的代码缩进是个好主意。它可以帮助我们阅读代码,更重要的是,它将帮助 您调试代码 Take a quick look at a coding standard 为您自己谋福利。您可能会被要求在几周/几个月内修改此代码,最后您会感谢我的。

标签: javascript php html forms file


【解决方案1】:

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

【讨论】:

  • 我会在大约一个小时后检查一下,看看是否正确。谢谢!
  • @daniel Visser 我按原样处理了这两个文件并在我的机器上运行了代码,只将一个文件上传到上传目录,我没有删除 window.location.reload();来自 FORM ...我认为这不是由于 window.location.reload();但 INTODAN 的代码中还有其他内容
  • 现在已修复。
猜你喜欢
  • 2021-08-05
  • 1970-01-01
  • 2017-10-09
  • 1970-01-01
  • 2011-11-14
  • 2012-04-14
  • 2016-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多