简体   繁体   English

我的文件上传代码有什么问题?

[英]what's wrong with my file upload code?

I don't know what the problem is. 我不知道问题是什么。 After file upload from a post form i check it with: 从邮政表格上传文件后,我用以下方法进行检查:

if(!is_uploaded_file($_FILES['pic']['tmp_name'])){
$pic_error="<p>file upload error</p>";
}
else{
do something
};

it does not do something which means file is not uploaded. 它没有do something ,这意味着文件未上传。 When I echo $_FILES['pic']['tmp_name'] it prints the file tmp_name , it even prints the file's size. 当我回显$_FILES['pic']['tmp_name']时,它会打印文件tmp_name ,甚至会打印文件的大小。

I'm using multipart/form-data on my form. 我在multipart/form-data上使用multipart/form-data

What is the problem here? 这里有什么问题? Is there a problem from host?(like deactivating file upload or something?) 主机有问题吗(例如停用文件上传之类的东西?)

the problem still remains, i activated display errors and it says: 问题仍然存在,我激活了显示错误,并显示:

Warning: is_uploaded_file() expects parameter 1 to be string

but when i echo $_FILES['pic']['error']['0']; 但是当我回声$ _FILES ['pic'] ['error'] ['0']; it echo 0 witch means there are no error in uploading files! 它回显0巫婆意味着上传文件没有错误! if it's relative i should say i have several file inputs: 如果是相对的,我应该说我有几个文件输入:

            if(!is_uploaded_file($_FILES['pic-1']['tmp_name'])){
                $pic_1_error="<p>error</p>";
            };
            if(!is_uploaded_file($_FILES['pic-2']['tmp_name'])){
                $pic_2_error="<p>error</p>";
            };
            if(!is_uploaded_file($_FILES['pic-3']['tmp_name'])){
                $pic_3_error="<p>error</p>";
            };

edit: by using var_dump($_FILES); 编辑:通过使用var_dump($ _ FILES); i got this: 我懂了:

 ["pic-1"]=> array(5) { ["name"]=> array(1) { [0]=> string(15) "33333333333.jpg" } ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } ["tmp_name"]=> array(1) { [0]=> string(14) "/tmp/php3ggYlE" } ["error"]=> array(1) { [0]=> int(0) } ["size"]=> array(1) { [0]=> int(81200) } } ["pic-2"]=> array(5) { ["name"]=> array(1) { [0]=> string(7) "728.jpg" } ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } ["tmp_name"]=> array(1) { [0]=> string(14) "/tmp/phpucuBbg" } ["error"]=> array(1) { [0]=> int(0) } ["size"]=> array(1) { [0]=> int(32639) } } ["pic-3"]=> array(5) { ["name"]=> array(1) { [0]=> string(15) "33333333333.jpg" } ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } ["tmp_name"]=> array(1) { [0]=> string(14) "/tmp/phpz6vehT" } ["error"]=> array(1) { [0]=> int(0) } ["size"]=> array(1) { [0]=> int(81200) } } 

which does mean all files have been uploaded correctly(dose not?). 这意味着所有文件都已正确上传(不是吗?)。

The 'if-else' conditional you have written should not end in a semi-colon (;). 您编写的“ if-else”条件不应以分号(;) 结尾

This is best achieved with a for loop. 这最好通过for循环来实现。 Just winging it, without security stuff added ... 只是侧翼,没有添加安全性内容...

define('FILE_UPLOAD_LIMIT', 5);
$numFiles        = null;
$errors          = [];

/*I do not have time to do a complete validation here. I would use loops, anyway.*/
try
{
    if(is_array($_FILES) && !empty($_FILES) &&
        isset($_FILES['files']) && is_array($_FILES['files']) && !empty($_FILES['files']) &&
        isset($_FILES['files']['tmp_name']) && is_array($_FILES['files']['tmp_name']) && !empty($_FILES['files']['tmp_name']) &&
        isset($_FILES['files']['error']) && is_array($_FILES['files']['error']) && !empty($_FILES['files']['error']))
    {
        $numFiles = count($_FILES['files']['tmp_name']);

        if($numFiles > FILE_UPLOAD_LIMIT)
        {
            $_FILES = [];
            throw new DomainException("Too many files uploaded");
        }
    }
    else
    {
        throw new DomainException("No files were uploaded.");
    }
}
catch(DomainException de)
{
     //Handle it.
}

for($i = 0; $i < $numFiles; $i++)
{
    if(is_uploaded_file($_FILES['files']['tmp_name'][$i]) && ($_FILES['files']['error'][$i] === 0))
    {
        //Do something security related.
        //Do something with the file.
    }
    else
    {
        $errors[$i] = "<p>File upload error on file #" . ($i + 1) . "</p>";
    }//<------No semi-colon here.
}

Also, check your php.ini for $_POST, file, processing limit related settings. 另外,检查您的php.ini中的$ _POST,文件,处理限制相关设置。 Check all necessary file permissions in the temp folder and the folder you want to move files to. 检查temp文件夹和要将文件移动到的文件夹中的所有必要文件权限。 Above all, if you are uploading multiple files at the same time add the [] next to the name attribute's value in your HTML5. 最重要的是,如果您要同时上传多个文件,请在HTML5中的name属性值旁边添加[] Make sure to use the multiple="multiple" attribute as well. 确保也使用multiple="multiple"属性。

<form id="fileUploadForm" name="fileUploadForm_" enctype="multipart/form-data" method="post" action="foo.php">
    <input type="file" name="files[]" multiple="multiple">
    <input type="file" name="files[]" multiple="multiple">
    <!--blah-->
</form>

http://php.net/manual/en/features.file-upload.multiple.php http://php.net/manual/zh/features.file-upload.multiple.php

As you can see in your var_dump output, each array contains a further array. 正如您在var_dump输出中看到的那样,每个数组都包含另一个数组。 It's actually: 实际上是:

$_FILES['pic-1']['tmp_name'][0]

Are you by any chance declaring the input in your form with name="pic-1[]" ? 您是否有机会以name="pic-1[]"在表单中声明输入? That would explain the superfluous inner array. 那将解释多余的内部数组。

I found the problem. 我发现了问题。 I used JQuery for image preview before upload. 上传之前,我使用JQuery进行图像预览。 It was changing file inputs, but I didn't know it. 它正在更改文件输入,但我不知道。 For example: from 例如:从

<input type="file" name="pic1" />

to

<input type="file" name="pic1-[]" />

That was the source of all my problems in PHP. 那就是我所有PHP问题的根源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM