如何测试用户是否已选择要上传的文件? [英] How to test if a user has SELECTED a file to upload?
问题描述
在页面上,我有:
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)) {
// ...
}
}
问题是,如果用户希望在不上传LOGO的情况下编辑信息,则会引发错误:您必须上传正确类型的错误"
因此,如果用户未在输入框中添加任何内容以进行上传,则我不想输入这些条件测试.
我测试过:if (!empty($_FILES['logo']['name'])
和if (isset($_FILES['logo']['name'])
但两者似乎都不起作用.
有什么想法吗?
edit:也许我不太清楚,我不想测试他是否上传了徽标,我想测试如果他选择了要上传的文件,因为现在,如果他没有选择文件要上传,php会提示您必须以正确的格式上传.
谢谢.
您可以使用以下方法进行检查:
if (empty($_FILES['logo']['name'])) {
// No file was selected for upload, your (re)action goes here
}
或者您可以使用Javascript结构,仅在上载字段具有除空字符串(")之外的其他值时才启用上载/提交按钮,从而避免提交完全不上载的表单.
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'
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'])
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.
thanks.
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.
这篇关于如何测试用户是否已选择要上传的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!