how to check if a $_FILE is set in php?

前端 未结 5 1801
情书的邮戳
情书的邮戳 2021-01-04 12:01

I\'ve created a form with 3

I see that I get an array with array(name=>\"\").

So I check if (

相关标签:
5条回答
  • 2021-01-04 12:04

    There is: is_uploaded_file(). When dealing with uploaded files, you should always use it (and its cousin move_uploaded_file()) for security reasons.

    0 讨论(0)
  • 2021-01-04 12:14

    You can try this:

    if($_FILES['myfilename']['size'] > 0 ) {
    
    }
    else{
           echo 'File is not uploaded . . .';
    }
    
    0 讨论(0)
  • 2021-01-04 12:20

    You can use empty to check if a variable is blank or not but Pekka's solution is best in this way

    if (empty($_FILES["myfilename"]["name"]))
    

    If you are checking that if a variable is set you can use isset function

    0 讨论(0)
  • 2021-01-04 12:25

    The best way, assuming you're using a recent PHP (4.2+), is to check that:

    $_FILE['myfilename']['error'] === UPLOAD_ERR_OK
    

    If this is true the upload worked, you can see the list of other possible values here

    0 讨论(0)
  • 2021-01-04 12:30

    Try this:

    if($_FILES["uploadImg"]['name'][0] != ''){
        //echo 'file attached';
    }else{
        //echo 'no file attached';
    }
    

    This works for me...

    0 讨论(0)
提交回复
热议问题