簡體   English   中英

上載多個文件時出現問題

[英]Problem uploading multiple files

這是我正在使用的代碼。 我似乎看不到問題出在哪里-它的意思是要上傳多個文件。 有趣的是,有時它僅上傳一個文件,然后不上傳任何文件。

有人可以幫我調查一下嗎..

<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()的函數。此函數檢查文件是否確實已上載,並防止移動不應移動的文件。

2號 查看上載的每個文件的錯誤值,因為您可能會在上載時觸發一些錯誤。

更新。

嘗試將您的解決方案縮減到裸露的骨頭。 跳過錯誤報告,直到您可以正常工作為止。 這將幫助您了解代碼的確切含​​義,並幫助您在解決可能的錯誤之前使該部分正常工作。

第二次更新。

這是代碼的最小化版本。 這個對我有用。

http://cznp.com/1032712.phps

我還需要做的是使要寫入的文件夾對Web服務器可寫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM