在php循环中回显所有上传的文件目的地

时间:2014-11-01 16:06:36

标签: php file loops upload

我有一个允许我上传多个文件的程序。 如何回显所有文件目的地($ filedestinations)?回声一直在底部,但它只回显上传的最后一个文件,因为该程序在循环中工作。寻找它所说的地方:

echo "<br /><br /><b>Articles Posted at: <br /> </b>$file_destination <br /><br /> ";

代码:

if(!empty($_FILES['articlefile']['name'][0])){

    $files = $_FILES['articlefile'];

    $uploaded = array();
    $failed = array();

    $allowed = array('txt', 'php', 'html',);

    foreach($files['name'] as $position => $file_name){

    //File Properties
    $file_tmp = $files['tmp_name'][$position];
    $file_size = $files['size'][$position];
    $file_error = $files['error'][$position];

    // Work out the file extension
    $file_ext = explode('.', $file_name);
    $file_ext = strtolower(end($file_ext));

        if(in_array($file_ext, $allowed)){

            if($file_error === 0){

            if($file_size <= 2097152){

                $file_name_new = uniqid(true).'.'. $file_ext;
                $file_destination ='the_world_news_journal/articles/' . $file_name_new;
                if(move_uploaded_file($file_tmp, $file_destination)){
                    $uploaded[$position] = $file_destination;

                    }else{

                    $failed[$position] = "Article [{$file_name}] failed to upload";

                }
            }else{
                $failed[$position] ="Article [{$file_name}] is to large!";

            }


            }else{
             $failed[$position] ="Article [{$file_name}] failed to upload, error:'{$file_error}'";

            }


        }else{
            $failed[$position] = "[{$file_name}] file extension '{$file_ext}' is not allowed in Article uploader.";


        }
    }
    echo "<u>Article Files</u> <br />";
    echo "<br /><b>Uploaded:</b> <br />";
    if(!empty($uploaded)){
        print_r($uploaded);
            echo "<br /><br /><b>Articles Posted at: <br /> </b>$file_destination <br /><br /> ";
    }
    echo "<br /><br /><b>Not Uploaded: </b><br /><br />";
    if (!empty($failed)){
        print_r($failed);   
    }

}

2 个答案:

答案 0 :(得分:0)

在代码的那部分上,改为保存到数组:

if(!empty($uploaded)) {
        $dest[] = $file_destination;
    }

循环完成后你可以崩溃:

// It's always good to make sure this variable is set
// for an error-free output
if(isset($dest))
    // You can do a simple one-line implode or a loop for a more complex visual
    echo "<b>Articles Posted at:</b><br />".implode("<br />-->",$dest)

答案 1 :(得分:0)

你可以在数组中添加目标,然后在底部循环

foreach($files['name'] as $position => $file_name){

 $file_destination ='the_world_news_journal/articles/' . $file_name_new;
 $file_destination_list[] = $file_destination // Add this new line

}

if(count($uploaded)){
    foreach ($file_destination_list as $file_destination) {
      echo "<br /><br /><b>Articles Posted at: <br /> </b>$file_destination <br /><br /> ";
    }     
}