【问题标题】:PHP : How to skip uploading a file if no file selectedPHP:如果没有选择文件,如何跳过上传文件
【发布时间】:2020-01-10 12:32:42
【问题描述】:

我有这个 PHP 脚本,它可以将表单的内容与图像一起上传到数据库中。现在,我想让它在选择时上传所有内容(工作)并跳过上传图片部分并上传除图片以外的所有内容。

下面是上传图片到服务器的部分。

$time = time();
$uploader = $_SESSION['username'];
$target_dir = "../uploads/";
$target_file = $target_dir . $uploader . $time. basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

if(isset($_POST["addpost-btn"])){

$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
} else {
    echo "File is not an image.";
    $uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 2000000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
//the rest is a database insert that works

当我尝试插入没有返回图像的表单内容时:

警告:getimagesize(): 第 14 行 C:\xampp\htdocs\vinhub\inc\addpost.inc.php 中的文件名不能为空

文件不是图像。抱歉,只允许使用 JPG、JPEG、PNG 和 GIF 文件。抱歉,您的文件未上传。

第 14 行是 $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);

我认为这是因为它需要选择文件,但我如何绕过它并在没有图像的情况下继续插入?

任何帮助将不胜感激,在此先感谢您

【问题讨论】:

  • if (isset($_FILES["fileToUpload"])) ?

标签: php file-upload


【解决方案1】:

虽然 Andrea 的解决方案效果很好,但我仍然需要在数据库中插入正确的路径。路径在$target_file 中指定,因此初始为$photo = $target_file。无论文件是否上传,这都会插入路径。

我的解决方案是:

if (is_uploaded_file($_FILES['fileToupload']['tmp_name'])) {
    $photo = $target_file;
 } else {
    $photo = '';
 }

检查文件是否实际上传并采取相应措施。 感谢大家的帮助!

【讨论】:

    【解决方案2】:

    您的getimagesize() 部分代码没有任何问题。如果没有图像文件,它只会向您发送警告。该警告不会中断代码运行。

    但这里可能存在问题:

    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    //the rest is a database insert that works
    

    //the rest is a database insert that works之前没有关闭}

    这意味着只有在$uploadOk == 1 时才会进行数据库插入。

    因此,如果您仍想进行数据库插入(文件是否已上传),则需要将此代码修复为:

    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    } //added brace
    
    //database insertion here
    

    【讨论】:

    • 谢谢,你说得对,我粘贴的代码没有},但这只是复制粘贴的错误,原来的代码确实有:)
    【解决方案3】:

    尝试检查 isset($_FILES["fileToUpload"])

    <?php
    
    $time = time();
    $uploader = $_SESSION['username'];
    $target_dir = "../uploads/";
    
    if (isset($_FILES["fileToUpload"])) {
        $target_file = $target_dir . $uploader . $time . basename($_FILES["fileToUpload"]["name"]);
        $uploadOk = 1;
        $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
    
        if (isset($_POST["addpost-btn"])) {
    
            $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
            if ($check !== false) {
                echo "File is an image - " . $check["mime"] . ".";
                $uploadOk = 1;
            } else {
                echo "File is not an image.";
                $uploadOk = 0;
            }
        }
    // Check if file already exists
        if (file_exists($target_file)) {
            echo "Sorry, file already exists.";
            $uploadOk = 0;
        }
    // Check file size
        if ($_FILES["fileToUpload"]["size"] > 2000000) {
            echo "Sorry, your file is too large.";
            $uploadOk = 0;
        }
    // Allow certain file formats
        if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
            echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
            $uploadOk = 0;
        }
    // Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) {
            echo "Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
        } else {
            if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                echo "The file " . basename($_FILES["fileToUpload"]["name"]) . " has been uploaded.";
            } else {
                echo "Sorry, there was an error uploading your file.";
            }
            //the rest is a database insert that works
        }
    }
    

    【讨论】:

    • 谢谢!奇迹般有效。现在我需要弄清楚如何为数据库插入做同样的事情,因为我有 $photo = $target_file 以任何方式插入“../uploads/$uploader.$time”。相同的 if 语句在这里工作吗?假设: if (isset($_FILES["fileToUpload"])){ $photo = $target_file; } elseif(!isset($_FILES["fileToUpload"])) { $photo = ' ';}
    猜你喜欢
    • 2019-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 2018-07-27
    • 2012-10-11
    • 2016-03-13
    相关资源
    最近更新 更多