Php 上载多个文件时出现问题

Php 上载多个文件时出现问题,php,javascript,html,Php,Javascript,Html,这是我正在使用的代码。我似乎看不出问题出在哪里——它意味着一次上传多个文件。有趣的是,有时它只上传一个文件,然后就什么也不上传了 有人能帮我调查一下吗 <pre><code> <?php /*** the upload directory ***/ $upload_dir= './uploads'; /*** numver of files to upload ***/ $num_uploads = 5; /*** maximum filesize allow

这是我正在使用的代码。我似乎看不出问题出在哪里——它意味着一次上传多个文件。有趣的是,有时它只上传一个文件,然后就什么也不上传了

有人能帮我调查一下吗

<pre><code>
<?php

/*** the upload directory ***/
$upload_dir= './uploads';

/*** numver of files to upload ***/
$num_uploads = 5;

/*** maximum filesize allowed in bytes ***/
$max_file_size  = 999999951200;

/*** the maximum filesize from php.ini ***/
$ini_max = str_replace('M', '', ini_get('upload_max_filesize'));
$upload_max = $ini_max * 99999991024;

/*** a message for users ***/
$msg = 'Please select files for uploading';

/*** an array to hold messages ***/
$messages = array();

/*** check if a file has been submitted ***/
if(isset($_FILES['userfile']['tmp_name']))
{
    /** loop through the array of files ***/
    for($i=0; $i < count($_FILES['userfile']['tmp_name']);$i++)
    {
        // check if there is a file in the array
        if(!is_uploaded_file($_FILES['userfile']['tmp_name'][$i]))
        {
            $messages[] = 'No file uploaded';
        }
        /*** check if the file is less then the max php.ini size ***/
        elseif($_FILES['userfile']['size'][$i] > $upload_max)
        {
            $messages[] = "File size exceeds $upload_max php.ini limit";
        }
        // check the file is less than the maximum file size
        elseif($_FILES['userfile']['size'][$i] > $max_file_size)
        {
            $messages[] = "File size exceeds $max_file_size limit";
        }
        else
        {
            // copy the file to the specified dir 
            if(@copy($_FILES['userfile']['tmp_name'][$i],$upload_dir.'/'.$_FILES['userfile']['name'][$i]))
            {
                /*** give praise and thanks to the php gods ***/
                $messages[] = $_FILES['userfile']['name'][$i].' uploaded';
            }
            else
            {
                /*** an error message ***/
                $messages[] = 'Uploading '.$_FILES['userfile']['name'][$i].' Failed';
            }
        }
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Multiple File Upload</title>
</head>

<body>

<h3><?php echo $msg; ?></h3>

<p>
<?php
 if(sizeof($messages) != 0)
 {
    foreach($messages as $err)
    {
        echo $err.'<br />';
    }
 }
?>
</p>

<form enctype="multipart/form-data" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $upload_max; ?>" />
<?php
$num = 0;
while($num < $num_uploads)
{
    echo '<div><input name="userfile[]" type="file" /></div>';
    $num++;
}
?>
<input type="submit" value="Upload" />
</form>

</body>
</html>

</code></pre>

多文件上传


首先,不要使用copy()移动文件。此函数有一个名为move_uploaded_file()的函数。此函数用于检查文件是否确实已上载,并防止移动不应移动的文件

第二。查看上传的每个文件的错误值,因为上传时可能会触发一些错误

更新

试着把你的解决方案精简到最简单的部分。跳过错误报告,直到它正常工作。它将帮助您了解代码的具体内容,并帮助您在解决可能的错误之前使该部分正常工作

第二次更新

这是您的代码的最小化版本。它对我有用


我还需要做的是使我正在编写的文件夹为我的web服务器可写。

您将输入[文件]字段视为一个数组?e、 g..是的,我把它当作一个数组,我不知道它在这个网站上没有正确显示:(我会再次尝试格式化代码…啊,是代码的格式让我得到了我=P对不起。谢谢Olafur,没有错误报告。这真的让我很生气:(我讨厌代码不运行也没有错误。糟糕的东西:):)你是个救生员。我的代码怎么了?我猜你没有使用move_上传的文件。其余的都差不多。