多文件上传不起作用 [英] Multifile upload is not working

查看:187
本文介绍了多文件上传不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的表单,用户可以选择要上传的文件。这部分是好的,多选择工作:

 < form action =upload.phpmethod =post enctype =multipart / form-data> 
< input type =filemultiple =multiplename =fileToUpload []id =fileToUpload>
< button type =submitclass =btn btn-primaryvalue =上传图片name =submit>书签文本< / button>
< / form>

以下是用于上传文件的php:

 <?php 
$ ID = uniqid();
mkdir(temp /。$ ID);
$ target_dir =temp /;
$ target_file = $ target_dir。 /。 $ ID。 /。基名($ _ FILES [ fileToUpload] [ 名称]);
$ target = basename($ target_file,。docx);
$ uploadOk = 1;
$ imageFileType = pathinfo($ target_file,PATHINFO_EXTENSION);
//检查文件是否已经存在
//检查$ uploadOk设置为0是否有错误
if($ uploadOk == 0){
echoSorry,your文件没有上传。;
//如果一切正常,尝试上传文件
} else {
if(move_uploaded_file($ _ FILES [fileToUpload] [tmp_name],$ target_file)){
回显文件。 basename($ _FILES [fileToUpload] [name])。 已上传。
} else {
echo抱歉,上传文件时出现错误。
}
}
?>

我无法使代码正常工作。我尝试了许多不同的方法。我想我需要一个foreach而不是在哪里呢?如果我粘贴upload.php: die('< pre>。print_r($ _ FILES [ fileToUpload],true)。'< / pre>'); 在PHP的乞讨我得到以下内容:

<$ p数组

[name] =>数组

[0] => sablonTeszt20150805.docx
[1 ] => sablonTeszt20150805másolat.docx
[2] => sablonTeszt20150805másolat2.docx


[type] => Array

[0] => application / vnd.openxmlformats-officedocument.wordprocessingml.document
[1] => application / vnd.openxmlformats-officedocument.wordprocessingml.document
[2] => application / vnd.openxmlformats-officedocument.wordprocessingml.document


[tmp_name] => Array

[0] => / Applications / MAMP / tmp / php / phpiu1FTX
[1] => / Applic ations / MAMP / tmp / php / phpHm4xiV
[2] => / Applications / MAMP / tmp / php / phpjKZV72


[error] =>数组

[0] => 0
[1] => 0
[2] => 0


[size] => Array

[0] => 65647
[1] => 65647
[2] => 65647






解决方案

如果你想上传多个.docx文件,这是你可以做的。在每个关键步骤中,我都会添加注释,让您了解每一步所发生的事情。
$ b HTML $ b

 < form action =index.phpmethod =postenctype =multipart / form-data> 
< input type =filemultiple =multiplename =fileToUpload []id =fileToUpload>
< button type =submitclass =btn btn-primaryvalue =上传图片name =submit>书签文本< / button>
< / form>

PHP

< pre $ <?php

if(isset($ _ POST ['submit'])){
$ ID = uniqid();

//文件模式:777
//为每个人读,写和执行
//你应该根据你的需求改变这个文件模式
mkdir( temp。DIRECTORY_SEPARATOR。$ ID,0777,true);

$ target_dir =temp。 DIRECTORY_SEPARATOR。 $ ID;

//计数上传的文件数量
$ num_of_files = count($ _ FILES [fileToUpload] [name]);

//通过每个文件循环上传
($ i = 0; $ i <$ num_of_files; ++ $ i){
$ target_file = $ target_dir。 DIRECTORY_SEPARATOR。基名($ _ FILES [ fileToUpload] [ 名称] [$ I]);
$ uploadOk = 1;

//获取文件扩展名
$ ext = strtolower(pathinfo($ target_file,PATHINFO_EXTENSION));

检查它是否是.docx文件
if($ ext ==docx){

//检查文件是否已经存在
//如果($ uploadOk == 0){
echo抱歉,您的文件没有上传,请检查$ uploadOk是否被错误设置为


//如果一切正常,尝试上传文件
} else {
if(move_uploaded_file($ _ FILES [fileToUpload] [tmp_name] [$ i] ,$ target_file)){
echoThe file。基本名($ _ FILES [ fileToUpload] [ 名称] [$ i])。 已上传。
} else {
echo抱歉,上传文件时出现错误。
}

}

} else {
echo只允许.docx文件;
}
}
}

?>

注意:为了提供跨平台兼容性,您应该使用PHP的 DIRECTORY_SEPARATOR 常量来写路径字符串,例如..。DIRECTORY_SEPARATOR。foo,因为在Windows上执行的方式是.. \foo../ foo

I have the following form in which the user can select the files to upload. This part is okay, the multi-select is working:

        <form action="upload.php" method="post" enctype="multipart/form-data">
          <input type="file" multiple="multiple" name="fileToUpload[]" id="fileToUpload">
          <button type="submit"  class="btn btn-primary" value="Upload Image" name="submit">Fájlok feltöltése</button>
        </form>

And the following php which is used to upload the files:

     <?php
        $ID = uniqid();
        mkdir("temp/" . $ID);
        $target_dir = "temp/" ;
        $target_file = $target_dir . "/" . $ID . "/" . basename($_FILES["fileToUpload"]["name"]);
        $target = basename($target_file,".docx");
        $uploadOk = 1;
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
        // Check if file already exists
       // Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
        ?>

I can,t make the code working. I tried many different ways. I suppose that I need a foreach somewhere but where? What am I doing wrong?

update: If I paste upload.php: die('<pre>'.print_r($_FILES["fileToUpload"], true).'</pre>'); at the begging of the php I get the following:

Array
(
    [name] => Array
        (
            [0] => sablonTeszt20150805.docx
            [1] => sablonTeszt20150805 másolat.docx
            [2] => sablonTeszt20150805 másolat 2.docx
        )

    [type] => Array
        (
            [0] => application/vnd.openxmlformats-officedocument.wordprocessingml.document
            [1] => application/vnd.openxmlformats-officedocument.wordprocessingml.document
            [2] => application/vnd.openxmlformats-officedocument.wordprocessingml.document
        )

    [tmp_name] => Array
        (
            [0] => /Applications/MAMP/tmp/php/phpiu1FTX
            [1] => /Applications/MAMP/tmp/php/phpHm4xiV
            [2] => /Applications/MAMP/tmp/php/phpjKZV72
        )

    [error] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
        )

    [size] => Array
        (
            [0] => 65647
            [1] => 65647
            [2] => 65647
        )

)

解决方案

If you want to upload multiple .docx files, this is what you can do. At every critical step I added comments to make you understand what's happening at each step.

HTML

<form action="index.php" method="post" enctype="multipart/form-data">
  <input type="file" multiple="multiple" name="fileToUpload[]" id="fileToUpload">
  <button type="submit"  class="btn btn-primary" value="Upload Image" name="submit">Fájlok feltöltése</button>
</form>

PHP

<?php

    if(isset($_POST['submit'])){
        $ID = uniqid();

        // file mode: 777
        // read, write and execute for everyone
        // you should change this file mode based on your requirement
        mkdir("temp" . DIRECTORY_SEPARATOR . $ID, 0777, true);

        $target_dir = "temp" . DIRECTORY_SEPARATOR . $ID ;

        // count the number of files uploaded
        $num_of_files = count($_FILES["fileToUpload"]["name"]);

        // loop through each file to upload
        for($i = 0; $i < $num_of_files; ++$i){
            $target_file = $target_dir . DIRECTORY_SEPARATOR . basename($_FILES["fileToUpload"]["name"][$i]);
            $uploadOk = 1;

            // get the file extension
            $ext = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

            // check if it's a .docx file
            if($ext == "docx"){

                // Check if file already exists
                // Check if $uploadOk is set to 0 by an error
                if ($uploadOk == 0) {
                    echo "Sorry, your file was not uploaded.";

                    // if everything is ok, try to upload file
                } else {
                    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"][$i], $target_file)) {
                        echo "The file ". basename($_FILES["fileToUpload"]["name"][$i]). " has been uploaded.";
                    } else {
                        echo "Sorry, there was an error uploading your file.";
                    }

                }

            }else{
                echo "only .docx files are allowed";
            }
        }
    }

?>

Sidenote: In order to provide cross-platform compatibility, you should use PHP's DIRECTORY_SEPARATOR constant to write path strings, e.g. "..".DIRECTORY_SEPARATOR."foo", because the way to do it on Windows would be "..\foo" while on everything else (Linux, UNIX, Mac OS X) it would be "../foo".

这篇关于多文件上传不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆