簡體   English   中英

如何檢查文件輸入字段是否為空?

[英]How to check if the file input field is empty?

我很難使用$_FILES

我想檢查文件上傳字段是否為空,然后應用一個條件,如果文件上傳為空,則腳本不會嘗試上傳文件。 我該如何執行?

if($_FILES["file"]["error"] != 0) {
//stands for any kind of errors happen during the uploading
} 

還有

if($_FILES["file"]["error"] == 4) {
//means there is no file uploaded
}

這應該工作

if ( ! empty($_FILES)) {...}

其他答案對我不起作用。 所以我發布我的解決方案:

if($_FILES['theFile']['name']=='')
{
    //No file selected
}

這是對我有用的:

if ($_FILES['theFile']['tmp_name']!='') {
    // do this, upload file
} // if no file selected to upload, file isn't uploaded.

您可以使用UPLOAD_ERR_NO_FILE值:

function isset_file($file) {
    return (isset($file) && $file['error'] != UPLOAD_ERR_NO_FILE);
}

if(isset_file($_FILES['input_name'])) {
    // It's not empty
}

更新:因為發送$_FILES['input_name']可能會拋出一個通知

function isset_file($name) {
    return (isset($_FILES[$name]) && $_FILES[$name]['error'] != UPLOAD_ERR_NO_FILE);
}

if(isset_file('input_name')) {
    // It's not empty
}

這個問題是重復的,但你的答案是is_uploade_file() function。

if(!empty($_FILES['myFileField'])) {
    // file field is not empty..
} else {
    // no file uploaded..
}

要檢查類型文件的輸入是否為,您必須使用$_FILES arrays 中的任何一個,並根據空數組檢查它。 我在上面看到的所有內容都是針對無效的空字符串進行檢查。 例子:

if($_FILES["your_field_name"]["size"] == [' '])
{
Perform your validation here•
}

我希望這有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM