PHP - 多张图片上传时经常出错
PHP - Frequent error on multiple images upload
我创建了一个多图像上传表单(图像存储在一个文件夹中,信息存储在 MySQL 数据库中)。图像关联了一些信息,如描述、名称等。
经常,当我上传很多图片(比如 3,4)时,one/two 的图片没有上传,并且出现错误消息出现(上传文件时发生错误)。然后,如果我重试上传未上传的图片,不会发生错误。有时追加,有时不追加,不明白是什么问题(我觉得不正常)。
在这里你可以看到我的 PHP 代码和我的 HTML 表格:
当用户点击 input type="file"
时,我的代码只是创建另一个输入并隐藏旧的(这样,如果用户想要上传更多文件,已经上传的文件不会被删除)。
function boomFunction(obj) {
obj.style.display = 'none';
$(".upload-container").append("<input name='upload[]' type='file' multiple='multiple' id='upload' class='upload' onclick='boomFunction(this)' title='Carica un altro file!'>");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="result.php" method="post" enctype="multipart/form-data" class="form-inline" autocomplete="off" novalidate>
<div class="upload-container">
<input name="upload[]" type="file" multiple="multiple" id="upload" class="upload" required="required" aria-required="true" onclick="boomFunction(this)" title="Choose a file!">
</div><br>
<label>Subjects:</label>
<textarea type="text" id="argomenti" name="argomenti" required="required" aria-required="true" placeholder="Subjects..."></textarea><br>
<label for="anno">Year:</label>
<select id="anno" name="anno" required="required" aria-required="true">
<option value="" disabled selected>Choose year</option>
<option value="2016-2017">2016-2017</option>
<option value="2017-2018">2017-2018</option>
<option value="2018-2019">2018-2019</option>
</select><br>
<input type="submit" value="Carica i file" class="upload_button"><br><br><br>
</form>
然后,这是我的 result.php 页面。
<?php
$total = count(array_filter($_FILES['upload']['name'])); // Count the number of uploaded files
$timestamp = time();
if ($total==0) {
echo "<h2>You must upload at least one file.</h2>";
$uploadOk = 0;
} else {
for($i = 0; $i < $total; $i++) {
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
if ($tmpFilePath != '') {
$newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];
$nomeCompletoFile = basename($_FILES["upload"]["name"][$i]);
$target_file = "uploads/$nomeCompletoFile"; $uploadOk = 1;
$estensione = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
if($estensione != 'jpg' && $estensione != 'png' && $estensione != 'jpeg') {
echo "<h2>Just JPG e PNG files are allowed.</h2>";
$uploadOk = 0;
}
elseif (empty($_POST["argomenti"]) or empty($_POST["anno"])) {
echo "<h2>You cannot leave empty fields</h2>";
$uploadOk = 0;
} else {
$nomeFile = str_replace(".$estensione", '', $nomeCompletoFile);
$string = base64_encode(openssl_random_pseudo_bytes(15)); // Random 15 letters for the new file name
$id = rand(1, 100000); $nomeFinaleFile = 'uploads/'.$string.$id.'.'.$estensione;
if (move_uploaded_file($_FILES["upload"]["tmp_name"][$i], $nomeFinaleFile)) {
$argomenti = stripslashes(htmlspecialchars($_POST['argomenti']));
$anno = $_POST['anno'];
$mysqli = new mysqli('localhost', 'name', '', 'my_name');
$mysqli->query("INSERT INTO uploads (timestamp, file, argomenti, anno, nFile) VALUES ('$timestamp', '$nomeFinaleFile', '$argomenti', '$anno', '$i')");
echo "<h2>The file <i>$nomeFile</i> has been uploaded.</h2>";
} else echo "<h2>An error has occurred while uploading the file</h2>";
}
}
}
}
?>
貌似没有错误,确实有时候一切正常。
任何帮助将不胜感激
使用 base64encode url 安全
替换
$string = base64_encode(openssl_random_pseudo_bytes(15)); // Random 15 letters for the new file name
至
$string = rtrim(strtr(base64_encode(openssl_random_pseudo_bytes(15)), ['+' => '-', '/' => '_']), '=');
我创建了一个多图像上传表单(图像存储在一个文件夹中,信息存储在 MySQL 数据库中)。图像关联了一些信息,如描述、名称等。
经常,当我上传很多图片(比如 3,4)时,one/two 的图片没有上传,并且出现错误消息出现(上传文件时发生错误)。然后,如果我重试上传未上传的图片,不会发生错误。有时追加,有时不追加,不明白是什么问题(我觉得不正常)。
在这里你可以看到我的 PHP 代码和我的 HTML 表格:
当用户点击 input type="file"
时,我的代码只是创建另一个输入并隐藏旧的(这样,如果用户想要上传更多文件,已经上传的文件不会被删除)。
function boomFunction(obj) {
obj.style.display = 'none';
$(".upload-container").append("<input name='upload[]' type='file' multiple='multiple' id='upload' class='upload' onclick='boomFunction(this)' title='Carica un altro file!'>");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="result.php" method="post" enctype="multipart/form-data" class="form-inline" autocomplete="off" novalidate>
<div class="upload-container">
<input name="upload[]" type="file" multiple="multiple" id="upload" class="upload" required="required" aria-required="true" onclick="boomFunction(this)" title="Choose a file!">
</div><br>
<label>Subjects:</label>
<textarea type="text" id="argomenti" name="argomenti" required="required" aria-required="true" placeholder="Subjects..."></textarea><br>
<label for="anno">Year:</label>
<select id="anno" name="anno" required="required" aria-required="true">
<option value="" disabled selected>Choose year</option>
<option value="2016-2017">2016-2017</option>
<option value="2017-2018">2017-2018</option>
<option value="2018-2019">2018-2019</option>
</select><br>
<input type="submit" value="Carica i file" class="upload_button"><br><br><br>
</form>
然后,这是我的 result.php 页面。
<?php
$total = count(array_filter($_FILES['upload']['name'])); // Count the number of uploaded files
$timestamp = time();
if ($total==0) {
echo "<h2>You must upload at least one file.</h2>";
$uploadOk = 0;
} else {
for($i = 0; $i < $total; $i++) {
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
if ($tmpFilePath != '') {
$newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];
$nomeCompletoFile = basename($_FILES["upload"]["name"][$i]);
$target_file = "uploads/$nomeCompletoFile"; $uploadOk = 1;
$estensione = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
if($estensione != 'jpg' && $estensione != 'png' && $estensione != 'jpeg') {
echo "<h2>Just JPG e PNG files are allowed.</h2>";
$uploadOk = 0;
}
elseif (empty($_POST["argomenti"]) or empty($_POST["anno"])) {
echo "<h2>You cannot leave empty fields</h2>";
$uploadOk = 0;
} else {
$nomeFile = str_replace(".$estensione", '', $nomeCompletoFile);
$string = base64_encode(openssl_random_pseudo_bytes(15)); // Random 15 letters for the new file name
$id = rand(1, 100000); $nomeFinaleFile = 'uploads/'.$string.$id.'.'.$estensione;
if (move_uploaded_file($_FILES["upload"]["tmp_name"][$i], $nomeFinaleFile)) {
$argomenti = stripslashes(htmlspecialchars($_POST['argomenti']));
$anno = $_POST['anno'];
$mysqli = new mysqli('localhost', 'name', '', 'my_name');
$mysqli->query("INSERT INTO uploads (timestamp, file, argomenti, anno, nFile) VALUES ('$timestamp', '$nomeFinaleFile', '$argomenti', '$anno', '$i')");
echo "<h2>The file <i>$nomeFile</i> has been uploaded.</h2>";
} else echo "<h2>An error has occurred while uploading the file</h2>";
}
}
}
}
?>
貌似没有错误,确实有时候一切正常。
任何帮助将不胜感激
使用 base64encode url 安全
替换
$string = base64_encode(openssl_random_pseudo_bytes(15)); // Random 15 letters for the new file name
至
$string = rtrim(strtr(base64_encode(openssl_random_pseudo_bytes(15)), ['+' => '-', '/' => '_']), '=');