检查上传文件数组中是否存在文件 [英] Check if file exists in the upload file array
问题描述
我在前端提交了表单来提交内容,其中我需要检查提交时文件是否存在.
I got form at frontend to submit stuff, wherein I need to check if file exists on submit.
请注意,表单提交了文件数组.它使我一直在尝试的所有逻辑都失败了.请注意html第二行上的upload[]
.
Please note that the form submits array of files. It is failing the all logics I have been trying. Please notice the upload[]
on second line of html.
HTML
<form method="post" id="upload-form" action="" enctype="multipart/form-data" >
<input type="file" multiple="true" name="upload[]">
<input type="submit" name="upload_attachments" value="UPLOAD">
<form>
PHP
if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {
$upload_error = 'file is not set !';
return; //stop further code execution
}else{
//do further validation
}
在两种情况下,上面的代码均显示'file is not set !'
,即当我单击提交"按钮而不上传文件时,以及当我选择上传文件并单击提交按钮"时.
The code above shows 'file is not set !'
in both cases, that is when I hit submit button without uploading a file and when I select to upload a file and hit the submit button.
这是怎么回事,我什至在做对吗?
What is happening here, am I even doing it right?
基本上,我想在有人单击提交"按钮而不选择要上传的文件时返回文件为空"消息,并停止进一步的代码执行.
Basically I want to return a 'File Empty' message when someone hits the submit button without selecting a file to upload, and stop the further code execution.
推荐答案
$_FILES[<key>]['tmp_name']
-包含服务器上的文件路径
$_FILES[<key>]['tmp_name']
- contains file path on server
$_FILES[<key>]['name']
-只是原始文件名
要处理文件file[]
的数组,您需要通过以下方式获取数据
$_FILES[<key>][<property>][<id>]
To address array of files file[]
you need to fetch data by
$_FILES[<key>][<property>][<id>]
尝试
if(!file_exists($_FILES['upload']['tmp_name'][0])) {
手册
http://www.php.net/manual/zh/features.file-upload.php
http://www.php.net/manual/zh/features.file-upload.multiple.php
这篇关于检查上传文件数组中是否存在文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!