How to test if a user has SELECTED a file to upload?

前端 未结 5 946
梦毁少年i
梦毁少年i 2020-11-28 08:52

on a page, i have :

if (!empty($_FILES[\'logo\'][\'name\'])) {
    $dossier     = \'upload/\';
    $fichier     = basename($_FILES[\'logo\'][\'name\']);
            


        
5条回答
  •  执念已碎
    2020-11-28 09:31

    There is a section in php documentation about file handling. You will find that you can check various errors and one of them is

    UPLOAD_ERR_OK
        Value: 0; There is no error, the file uploaded with success.
    <...>
    UPLOAD_ERR_NO_FILE
        Value: 4; No file was uploaded.
    

    In your case you need code like

    if ($_FILES['logo']['error'] == UPLOAD_ERR_OK) { ... }
    

    or

    if ($_FILES['logo']['error'] != UPLOAD_ERR_NO_FILE) { ... }
    

    You should consider checking (and probably providing appropriate response for a user) for other various errors as well.

提交回复
热议问题