php 如何测试用户是否选择了要上传的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2958167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:14:37  来源:igfitidea点击:

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

phpfileupload

提问by sf_tristanb

on a page, i have :

在一个页面上,我有:

if (!empty($_FILES['logo']['name'])) {
    $dossier     = 'upload/';
    $fichier     = basename($_FILES['logo']['name']);
    $taille_maxi = 100000;
    $taille      = filesize($_FILES['logo']['tmp_name']);
    $extensions  = array('.png', '.jpg', '.jpeg');
    $extension   = strrchr($_FILES['logo']['name'], '.');

    if(!in_array($extension, $extensions)) {
        $erreur = 'ERROR you  must upload the right type';
    }

    if($taille>$taille_maxi) {
         $erreur = 'too heavy';
    }

    if(!empty($erreur)) {
      // ...
    }
}

The problem is, if the users wants to edit information WITHOUT uploading a LOGO, it raises an error : 'error you must upload the right type'

问题是,如果用户想在不上传 LOGO 的情况下编辑信息,则会引发错误:'error you must upload the right type'

So, if a user didn't put anything in the inputbox in order to upload it, i don't want to enter in these conditions test.

所以,如果用户没有在输入框中输入任何内容来上传它,我不想在这些条件测试中输入。

i tested : if (!empty($_FILES['logo']['name'])and if (isset($_FILES['logo']['name'])

我测试过: if (!empty($_FILES['logo']['name'])if (isset($_FILES['logo']['name'])

but both doesn't seems to work.

但两者似乎都不起作用。

Any ideas?

有任何想法吗?

edit : maybe i wasn't so clear, i don't want to test if he uploaded a logo, i want to test IF he selected a file to upload, because right now, if he doesn't select a file to upload, php raises an error telling he must upload with the right format.

编辑:也许我不太清楚,我不想测试他是否上传了徽标,我想测试他是否选择了要上传的文件,因为现在,如果他没有选择要上传的文件, php 引发一个错误,告诉他必须以正确的格式上传。

thanks.

谢谢。

回答by Oldskool

You can check this with:

您可以通过以下方式检查:

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

Or you can use a javascript construction that only enables the upload/submit button whenever the upload field has a value other then an empty string ("") to avoid submission of the form with no upload at all.

或者,您可以使用 javascript 构造,该构造仅在上传字段具有除空字符串 ("") 以外的值时才启用上传/提交按钮,以避免在根本不上传的情况下提交表单。

回答by Jonas

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

php 文档中有一节是关于文件处理的。你会发现你可以检查各种错误,其中之一是

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.

您还应该考虑检查(并可能为用户提供适当的响应)是否存在其他各种错误。

回答by baloo

You should use is_uploaded_file($_FILES['logo']['tmp_name'])to make sure that the file was indeed uploaded through a POST.

您应该使用is_uploaded_file($_FILES['logo']['tmp_name'])来确保文件确实是通过 POST 上传的。

回答by Jess

I would test if (file_exists($_FILES['logo']['tmp_name']))and see if it works.

我会测试看看它是否有效。if (file_exists($_FILES['logo']['tmp_name']))

Or, more approperately (thanks Baloo): if (is_uploaded_file($_FILES['logo']['tmp_name']))

或者,更恰当地(感谢 Baloo): if (is_uploaded_file($_FILES['logo']['tmp_name']))

回答by Your Common Sense

if ($_FILES['logo']['error'] === 0)

is the only right way

是唯一正确的方法