如果发生错误,停止所有文件上传 - PHP
Stop all files upload if an error occurs - PHP
我创建了一些代码,允许用户在 MySQL 数据库中上传一些图像。
这是我的两个问题:
- 发生错误时,例如因为文件不是图像,所以文件
有问题的文件没有加载,但其他文件加载了。
相反,我
想要这样,即使只是发生错误,没有文件 被上传。
示例:如果我上传了 3 个文件,其中 1 个不是图片:
- 我现在的输出,我不想要:
File 1 uploaded
File 2 uploaded
Just JPG and PNG images are allowed // file 3 not uploaded
- 我希望得到的:
No files uploaded // Even if a single file is not good, nobody gets uploaded
- 如果我上传 n 个文件,将打印 n 个消息。例如,如果我上传 3 个文件,这将是输出:
File 1.png upload correctly
File 2.png upload correctly
File 3.png upload correctly
但是我希望得到的只有一句话,比如3 files uploaded
.
这是我的代码:
$timestamp = time();
$total = count($_FILES[ 'upload' ][ 'name' ]);
if ($total==0) {
echo "Please upload at least one file";
$uploadOk = 0;
} else {
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
for($i = 0; $i < $total; $i++) {
if ($tmpFilePath != '') {
$newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];
$completeFileName = basename($_FILES["upload"]["name"][$i]);
$target_file = "uploads/$completeFileName"; $uploadOk = 1;
$estensione = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
$Name = basename($_FILES["upload"]["name"][$i]);
if($extension != 'jpg' && $extension != 'png') {
echo "Only JPG and PNG files are allowed";
$uploadOk = 0;
} else {
$fileName = str_replace(".$extension", '', $completeFileName);
$string = substr(md5(mt_rand()), 0, 15);
$id = rand(1, 100000);
$finalFileName = 'uploads/'.$string.$id.'.'.$estensione;
if (move_uploaded_file($_FILES["upload"]["tmp_name"][$i], $finalFileName)) {
$mysqli = new mysqli('localhost', 'example', '', 'my_example');
$mysqli->query("INSERT INTO uploads (timestamp, file) VALUES ('$timestamp', '$finalFileName')");
echo "<h2>The file <i>$fileName</i> has been uploaded.</h2>";
} else echo "Error";
}
}
}
}
}
希望我的解释清楚。
谢谢!
<?php
/*
* CREATE TABLE `uploads` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`timestamp` int(11) DEFAULT NULL,
`file` varchar(400) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
*/
$mysqli = new mysqli('localhost', 'devel', 'devel', 'upload_db');
$timestamp = time();
$success_images = [];
$total = count($_FILES[ 'upload' ][ 'name' ]);
if ($total == 0) {
echo "Please upload at least one file";
$uploadOk = 0;
} else {
$uploadOk = 1;
for ($i = 0; $i < $total; $i++) {
$tmpFilePath = $_FILES[ "upload" ][ "tmp_name" ][ $i ];
if ($tmpFilePath != '') {
$newFilePath = "./uploadFiles/" . $_FILES[ 'upload' ][ 'name' ][ $i ];
$Name = basename($_FILES[ "upload" ][ "name" ][ $i ]);
$parts = pathinfo($Name);
$extension = strtolower($parts[ 'extension' ]);
if ($extension == 'jpg' || $extension == 'png') {
// there may be $newFilePath I think
if (move_uploaded_file($_FILES[ "upload" ][ "tmp_name" ][ $i ], $newFilePath)) {
$mysqli->query("INSERT INTO uploads (timestamp, file) VALUES ('$timestamp', '$Name')");
$id = $mysqli->insert_id;
$success_images[ $id ] = $newFilePath;
$uploadOk = 1;
} else {
echo "Error";
$uploadOk = 0;
break; // break loop !!!
}
} else {
echo "Just JPG and PNG files are allowed";
$uploadOk = 0;
break;
}
}
}
if ($uploadOk === 0) {
foreach ($success_images as $id => $filename) {
// `id` - primary key of table uploads?
$mysqli->query(sprintf('DELETE FROM uploads WHERE `id`=%d', $id));
if (file_exists($filename)) {
unlink($filename);
}
}
} else {
foreach ($success_images as $id => $filename) {
echo "<h2>The file <i>$filename</i> has been uploaded.</h2>";
}
}
}
?>
<form method="POST" enctype="multipart/form-data">
<input type="file" multiple name="upload[]"/>
<input type="submit" value="do_upload"/>
</form>
我创建了一些代码,允许用户在 MySQL 数据库中上传一些图像。
这是我的两个问题:
- 发生错误时,例如因为文件不是图像,所以文件
有问题的文件没有加载,但其他文件加载了。
相反,我 想要这样,即使只是发生错误,没有文件 被上传。
示例:如果我上传了 3 个文件,其中 1 个不是图片:- 我现在的输出,我不想要:
File 1 uploaded File 2 uploaded Just JPG and PNG images are allowed // file 3 not uploaded
- 我希望得到的:
No files uploaded // Even if a single file is not good, nobody gets uploaded
- 我现在的输出,我不想要:
- 如果我上传 n 个文件,将打印 n 个消息。例如,如果我上传 3 个文件,这将是输出:
File 1.png upload correctly File 2.png upload correctly File 3.png upload correctly
但是我希望得到的只有一句话,比如3 files uploaded
.
这是我的代码:
$timestamp = time();
$total = count($_FILES[ 'upload' ][ 'name' ]);
if ($total==0) {
echo "Please upload at least one file";
$uploadOk = 0;
} else {
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
for($i = 0; $i < $total; $i++) {
if ($tmpFilePath != '') {
$newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];
$completeFileName = basename($_FILES["upload"]["name"][$i]);
$target_file = "uploads/$completeFileName"; $uploadOk = 1;
$estensione = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
$Name = basename($_FILES["upload"]["name"][$i]);
if($extension != 'jpg' && $extension != 'png') {
echo "Only JPG and PNG files are allowed";
$uploadOk = 0;
} else {
$fileName = str_replace(".$extension", '', $completeFileName);
$string = substr(md5(mt_rand()), 0, 15);
$id = rand(1, 100000);
$finalFileName = 'uploads/'.$string.$id.'.'.$estensione;
if (move_uploaded_file($_FILES["upload"]["tmp_name"][$i], $finalFileName)) {
$mysqli = new mysqli('localhost', 'example', '', 'my_example');
$mysqli->query("INSERT INTO uploads (timestamp, file) VALUES ('$timestamp', '$finalFileName')");
echo "<h2>The file <i>$fileName</i> has been uploaded.</h2>";
} else echo "Error";
}
}
}
}
}
希望我的解释清楚。 谢谢!
<?php
/*
* CREATE TABLE `uploads` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`timestamp` int(11) DEFAULT NULL,
`file` varchar(400) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
*/
$mysqli = new mysqli('localhost', 'devel', 'devel', 'upload_db');
$timestamp = time();
$success_images = [];
$total = count($_FILES[ 'upload' ][ 'name' ]);
if ($total == 0) {
echo "Please upload at least one file";
$uploadOk = 0;
} else {
$uploadOk = 1;
for ($i = 0; $i < $total; $i++) {
$tmpFilePath = $_FILES[ "upload" ][ "tmp_name" ][ $i ];
if ($tmpFilePath != '') {
$newFilePath = "./uploadFiles/" . $_FILES[ 'upload' ][ 'name' ][ $i ];
$Name = basename($_FILES[ "upload" ][ "name" ][ $i ]);
$parts = pathinfo($Name);
$extension = strtolower($parts[ 'extension' ]);
if ($extension == 'jpg' || $extension == 'png') {
// there may be $newFilePath I think
if (move_uploaded_file($_FILES[ "upload" ][ "tmp_name" ][ $i ], $newFilePath)) {
$mysqli->query("INSERT INTO uploads (timestamp, file) VALUES ('$timestamp', '$Name')");
$id = $mysqli->insert_id;
$success_images[ $id ] = $newFilePath;
$uploadOk = 1;
} else {
echo "Error";
$uploadOk = 0;
break; // break loop !!!
}
} else {
echo "Just JPG and PNG files are allowed";
$uploadOk = 0;
break;
}
}
}
if ($uploadOk === 0) {
foreach ($success_images as $id => $filename) {
// `id` - primary key of table uploads?
$mysqli->query(sprintf('DELETE FROM uploads WHERE `id`=%d', $id));
if (file_exists($filename)) {
unlink($filename);
}
}
} else {
foreach ($success_images as $id => $filename) {
echo "<h2>The file <i>$filename</i> has been uploaded.</h2>";
}
}
}
?>
<form method="POST" enctype="multipart/form-data">
<input type="file" multiple name="upload[]"/>
<input type="submit" value="do_upload"/>
</form>