1

我确信有一个我看不到的简单解决方案。

我有一个上传东西的表格。

当脚本完成时,它会在脚本的其余部分运行之前使用Header('Location: admin.php?success')和键入消息。if($_GET['success']) { echo WOOHOO SUCCESS }

这样做的问题是您想一次上传 2 个文件,因为脚本的第一部分已执行,没有其他内容。然后我考虑使用布尔值来设置 true 或 false 并显示一条消息,但这也失败了。

我希望能够连续上传多个文件并收到每个文件的成功消息。

非常感谢。

相关PHP:

 if(isset($_GET['success']) && empty($_GET['success'])){
        echo '<h2>File Upload Successful! Whoop!</h2>';

    } else{

    if(empty($_POST) === false){

        //check that a file has been uploaded
        if(isset($_FILES['myTrainingFile']) && !empty($_FILES['myTrainingFile']['tmp_name'])){

            file stuff...

            if(in_array($fileExt, $blacklist) === true){
                $errors[] = "File type not allowed";
            }
        }

        if(empty($errors) === true){
            //run update
            move file stuff...
                }

            }

            $comments = htmlentities(trim($_POST['comments']));
            $category = htmlentities(trim($_POST['category']));

            $training->uploadDocument($fileName, $category, $comments);
            header('Location: admin.php?success');
            exit();

        } else if (empty($errors) === false) {
            //header('Location: messageUser.php?msg=' .implode($errors));
            echo '<p>' . implode('</p><p>', $errors) . '</p>';
        }}
    }
    ?>
4

1 回答 1

0

您需要遍历$_FILES超级全局数组,然后上传每个文件。

这是一个工作示例,可以为您提供更好的主意。

<?php

    $upload_dir= './uploads';
    $num_uploads = 2;
    $max_file_size  = 51200;
    $ini_max = str_replace('M', '', ini_get('upload_max_filesize'));
    $upload_max = $ini_max * 1024;
    $msg = 'Please select files for uploading';
    $messages = array();

    if(isset($_FILES['userfile']['tmp_name']))
    {
        for($i=0; $i < count($_FILES['userfile']['tmp_name']);$i++)
        {
            if(!is_uploaded_file($_FILES['userfile']['tmp_name'][$i]))
            {
                $messages[] = 'No file uploaded';
            }
            elseif($_FILES['userfile']['size'][$i] > $upload_max)
            {
                $messages[] = "File size exceeds $upload_max php.ini limit";
            }
            elseif($_FILES['userfile']['size'][$i] > $max_file_size)
            {
                $messages[] = "File size exceeds $max_file_size limit";
            }
            else
            {
                if(@copy($_FILES['userfile']['tmp_name'][$i],$upload_dir.'/'.$_FILES['userfile']['name'][$i]))
                {
                    $messages[] = $_FILES['userfile']['name'][$i].' uploaded';
                }
                else
                {
                    $messages[] = 'Uploading '.$_FILES['userfile']['name'][$i].' Failed';
                }
            }
        }
    }
?>

 <html>
 <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 $max_file_size; ?>" />
 <?php
    $num = 0;
    while($num < $num_uploads)
    {
        echo '<div><input name="userfile[]" type="file" /></div>';
        $num++;
    }
 ?>

 <input type="submit" value="Upload" />
 </form>

 </body>
 </html>

希望这可以帮助!

于 2013-07-23T23:18:33.480 回答