42

私のフォームには、ファイルをアップロードするための 3 つの入力フィールドがあります。

<input type=file name="cover_image">
<input type=file name="image1">
<input type=file name="image2">

cover_imageが空かどうかを確認するにはどうすればよいですか - アップロードするファイルがありませんか?

4

16 に答える 16

83

次のようsizeに、配列のフィールドを使用して確認できます。$_FILES

if ($_FILES['cover_image']['size'] == 0 && $_FILES['cover_image']['error'] == 0)
{
    // cover_image is empty (and not an error)
}

(何か問題が発生errorした可能性があるため、ここでもチェックします。オーバーライドできるため、このチェックには使用しません)0name

于 2013-01-22T12:29:12.083 に答える
26

方法 1

if($_FILES['cover_image']['name'] == "") {
// No file was selected for upload, your (re)action goes here
}

方法 2

if($_FILES['cover_image']['size'] == 0) {
// No file was selected for upload, your (re)action goes here
}
于 2013-01-22T12:29:10.837 に答える
12

次のようにして、値があるかどうか、および画像が有効かどうかを確認できます。

if(empty($_FILES['cover_image']['tmp_name']) || !is_uploaded_file($_FILES['cover_image']['tmp_name']))
{
   // Handle no image here...
}
于 2013-01-22T12:30:54.860 に答える
7
if (empty($_FILES['cover_image']['name']))
于 2013-01-22T12:29:49.527 に答える
3

フォームが投稿された後、以下を確認してください

$_FILES["cover_image"]["size"]==0
于 2013-01-22T12:29:14.507 に答える
2
 if( ($_POST) && (!empty($_POST['cover_image'])) )    //verifies  if post exists and cover_image is not empty
    {
    //execute whatever code you want
    }
于 2013-01-22T12:32:53.153 に答える
1

これはうまくいきます

if ($_FILES['cover_image']['size'] == 0 && $_FILES['cover_image']['error'] == 0)

// ファイルが選択されているかどうかをチェックし、エラーではない

{

// ファイルが選択されておらず、エラーではありません

}

于 2021-04-24T04:19:01.437 に答える