dongxu4580 2014-08-03 20:31
浏览 77
已采纳

回显IMG标签中的图像路径

I've followed a tutorial online for building a multi file uploader, added comments, everything running successfully as uploaded files move to the uploads folder and error messaging works. However while I can print_r the $uploaded array which displays the path and file name, what I really want to do is echo this path in an html < img/ > tag something like this :

  if($uploaded) {
     $name = $uploaded['filename']['name'];
     move_uploaded_file($uploaded['filename']['tmp_name'], $name);
     echo "<img src='" . $name . "' />";
 }

And display the images out on the page. Here's the file.

upload.php

<?php



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


$files = $_FILES['files'];

$uploaded = array();

$failed = array();

$allowed = array('png', 'jpg');


//Loops files and collects file name

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

//Collects temporary location size and error of looped file

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


//Collects file extention
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));


//References allowed extensions array

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


// checks for errors otherwise else statements  
    if($file_error === 0) {

// checks file size otherwise else statements       
        if($file_size <= 2097152 ) {

// uniqid generates new file based on time stamp name
// defines server file destination folder           
            $file_name_new = uniqid('', true) . '.' . $file_ext;
            $file_destination = 'uploads/' . $file_name_new;

//new destination for temporary file            
            if(move_uploaded_file($file_tmp, $file_destination)) {
                $uploaded[$position] = $file_destination;

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

            }


        } else {

            $failed[$position] = "[{$file_name}] is too large.";
        }


    } else {

        $failed[$position] = "[{$file_name}] errored with code {$file_error}.";
    }



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

}


    }


//prints $uploaded array if successful
if(!empty($uploaded)) {
    print_r($uploaded); 
}


//prints $failed else statments otherwise
if (!empty($failed)) {
    print_r($failed);

    }



}


?>

Form Index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>untitled</title>
    <meta name="generator" content="TextMate http://macromates.com/">
    <meta name="author" content="">
    <!-- Date: 2014-08-02 -->
</head>
<body>

    <form action="upload.php" method="post" enctype="multipart/form-data">
         <input type="file" name="files[ ]" multiple>
         <input type="submit" value="Upload"> 
      </form>

</body>
</html>
  • 写回答

2条回答 默认 最新

  • douxian3170 2014-08-03 21:56
    关注

    OK, after playing about with your code I have the following that works OK for me.

    index.php

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    
    <html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>untitled</title>
        <meta name="generator" content="TextMate http://macromates.com/">
        <meta name="author" content="">
        <!-- Date: 2014-08-02 -->
    </head>
    <body>
    
        <form action="upload.php" method="post" enctype="multipart/form-data">
             <input type="file" name="files[ ]" multiple>
             <input type="submit" value="Upload"> 
          </form>
    
    </body>
    </html>
    

    Upload.php

    <?php
    //
    if(!empty($_FILES['files']['name'][0])) {
    
    
    $files = $_FILES['files'];
    
    $uploaded = array();
    
    $failed = array();
    
    $allowed = array('png', 'jpg');
    
    
    //Loops files and collects file name
    
    foreach($files['name'] as $position => $file_name) {
    
    //Collects temporary location size and error of looped file
    
    $file_tmp = $files['tmp_name'] [$position];
    $file_size = $files['size'][$position];
    $file_error = $files['error'][$position];
    
    
    //Collects file extention
    $file_ext = explode('.', $file_name);
    $file_ext = strtolower(end($file_ext));
    
    
    //References allowed extensions arry
    
    if(in_array($file_ext, $allowed)) {
    
    
    // checks for errors otherwise else statments   
        if($file_error === 0) {
    
    // checks file size otherwise else statements       
            if($file_size <= 2097152 ) {
    
    // uniqid generates new file based on time stamp name
    // defines server file destination folder           
                $file_name_new = uniqid('', true) . '.' . $file_ext;
                $file_destination = 'uploads/' . $file_name_new;
    
    //new destination for temporary file            
                if(move_uploaded_file($file_tmp, $file_destination)) {
                    $uploaded[$position] = $file_destination;
    
                } else {
                    $failed[$position] = "[{$file_name}] failed to upload.";
    
                }
    
    
            } else {
    
                $failed[$position] = "[{$file_name}] is too large.";
            }
    
    
        } else {
    
            $failed[$position] = "[{$file_name}] errored with code {$file_error}.";
        }
    
    } else {
        $failed[$position] = "[{$file_name}] file extension '{$file_ext}' is not allowed."; 
    
    }
    
        }
    
    //prints $uploaded arry if successful
    if(!empty($uploaded)) {
    }
    
    //prints $failed else statments otherwise
    if (!empty($failed)) {
        //print_r($failed);
    
        }
    
        if($uploaded) {
        foreach ($uploaded as $image) 
        { 
        echo  "<img src='".$image."' />";
        } 
    
       }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?