亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果發生錯誤,則停止所有文件上傳 - 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 關注
  • 117 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號