回显IMG标签中的图像路径

时间:2014-08-03 20:31:21

标签: php file upload echo

我已经在网上tutorial建立了一个多文件上传器,添加了评论, 一切运行成功,因为上传的文件移动到uploads文件夹,错误消息工作。然而,虽然我可以print_r显示路径和文件名的$ uploaded数组,但我真正想做的是在html<中回显这个路径。 img />标记这样的东西:

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

并在页面上显示图像。这是文件。

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

    }



}


?>

表格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 个答案:

答案 0 :(得分:1)

你基本上是在打字<img src='apple.jpg'/>这很可能导致什么都没有。

您真正想做的事:<img src='/path/to/apple.jpg/>

并在PHP中:echo "<img src='/path/to/".$name." />'";

答案 1 :(得分:1)

好的,在玩了你的代码后,我有以下对我来说没问题。

的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."' />";
    } 

   }
}
?>