简体   繁体   English

上载多个文件时出现问题

[英]Problem uploading multiple files

Here's the code I'm using. 这是我正在使用的代码。 I can't seem to see where the problem is - its meant to upload multiple files at out ones. 我似乎看不到问题出在哪里-它的意思是要上传多个文件。 The funny thing is that some times it uploads just one file, and then won't upload anything. 有趣的是,有时它仅上传一个文件,然后不上传任何文件。

Can someone help me look into it please.. 有人可以帮我调查一下吗..

<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>

First of all don't use copy() to move the files. 首先,不要使用copy()移动文件。 There is a function for this called move_uploaded_file() This function checks if the file was really uploaded and prevents moving files that aren't supposed to be moved. 为此有一个名为move_uploaded_file()的函数。此函数检查文件是否确实已上载,并防止移动不应移动的文件。

2nd. 2号 Look at the error value of each file uploaded, since you could be triggering some errors on upload. 查看上载的每个文件的错误值,因为您可能会在上载时触发一些错误。

Update. 更新。

Try cutting your solution down to its bare bones. 尝试将您的解决方案缩减到裸露的骨头。 Skip the error reporting until you have it working. 跳过错误报告,直到您可以正常工作为止。 It will help you know what the exact meat and potatoes of the code is and helps you get that part working well before you work around the possible errors. 这将帮助您了解代码的确切含​​义,并帮助您在解决可能的错误之前使该部分正常工作。

2nd Update. 第二次更新。

Here's a minimized version of your code. 这是代码的最小化版本。 It works for me. 这个对我有用。

http://cznp.com/1032712.phps http://cznp.com/1032712.phps

What I had to do also is make the folder I am writing do writable for my web server. 我还需要做的是使要写入的文件夹对Web服务器可写。

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

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