如何处理使用PHP的多个文件上传 [英] How to handle multiple file upload using PHP

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

问题描述

我想用PHP上传文件,但问题是我不知道要上传多少个文件。

I want to upload files using PHP but the problem is that I don't know how many files I will upload.

我的问题是如何使用 file [] 来上传文件?

My question is how can I upload files if I use file[]?

<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label><input type="file" name="file[]" id="file" /> 
<br />
<label for="file">Filename:</label><input type="file" name="file[]" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

我将只添加文件框,我将使用JavaScript创建更多的文件输入来上传,但是如何在PHP中处理它们?

I will add just File box and I will use JavaScript to create more file input to upload but how to handle them in PHP?

推荐答案

请参阅 $ _ FILES 处理文件上传

<?php
    if(isset($_FILES['file']['tmp_name']))
    {
        // Number of uploaded files
        $num_files = count($_FILES['file']['tmp_name']);

        /** loop through the array of files ***/
        for($i=0; $i < $num_files;$i++)
        {
            // check if there is a file in the array
            if(!is_uploaded_file($_FILES['file']['tmp_name'][$i]))
            {
                $messages[] = 'No file uploaded';
            }
            else
            {
                // copy the file to the specified dir 
                if(@copy($_FILES['file']['tmp_name'][$i],$upload_dir.'/'.$_FILES['file']['name'][$i]))
                {
                    /*** give praise and thanks to the php gods ***/
                    $messages[] = $_FILES['file']['name'][$i].' uploaded';
                }
                else
                {
                    /*** an error message ***/
                    $messages[] = 'Uploading '.$_FILES['file']['name'][$i].' Failed';
                }
            }
        }
    }
?>

这篇关于如何处理使用PHP的多个文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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