【问题标题】:PHP file upload check - it is possible before sending the input via POST?PHP 文件上传检查 - 在通过 POST 发送输入之前可以吗?
【发布时间】:2015-08-18 10:04:56
【问题描述】:

我有一个非常基本的代码来上传表单:

echo "<form action='upload.php' method='post' enctype='multipart/form-data'>";
echo "Select upload:   ";
echo "<input type='file' name='fileToUpload' id='fileToUpload'>";
echo "<input type='submit' value='Upload file' name='submit'>";
echo "</form>";

好的,现在我想检查输入是否用户选择了一个文件,但在通过 POST 将输入文件名发送到另一个 PHP 文件(upload.php)之前

我知道检查是否没有选择文件可以这样做:

if(!isset($_FILES['fileToUpload']) || $_FILES['fileToUpload']['error'] == UPLOAD_ERR_NO_FILE) {
    echo "Error no file selected"; 
} else {
    echo "ok";
}

我知道我需要检查此文件中的输入:

echo '<form action="'.htmlspecialchars($_SERVER["PHP_SELF"]).'" method="post">';

但是如何将这些混合在一起? I want to pass the filename to another php file, when a file is selected.

谢谢。

【问题讨论】:

  • 在提交数据之前可以使用JS检查文件是否被选中。

标签: php file upload


【解决方案1】:

如果你想用 javascript 检查这个,你可以使用以下

var filename = document.getElementById("DealImage").value;

if (filename != ''){
   alert('files selected');
}else{
   alert('empty files input');
}

【讨论】:

    【解决方案2】:

    您可以使用 jQuery 来检查表单提交时是否选择了文件。

    if ( $('#fileToUpload').val() == '' || $('#fileToUpload').val() == null) {
        alert("Please select file");
    }
    

    【讨论】:

    • 感谢 user27715 的帮助!
    猜你喜欢
    • 2019-07-14
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多