为了账号安全,请及时绑定邮箱和手机立即绑定

如果发生错误,则停止所有文件上传 - PHP

如果发生错误,则停止所有文件上传 - PHP

PHP
呼如林 2022-12-11 09:10:00
我创建了一些代码,允许用户在 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 correctlyBut what I hope to have only one sentence, like 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;                            } 
查看完整描述

1 回答

?
慕尼黑8549860

TA贡献1818条经验 获得超11个赞

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


查看完整回答
反对 回复 2022-12-11
  • 1 回答
  • 0 关注
  • 130 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号