php - 在 IMG 标签中回显图像路径

标签 php file upload echo

我关注了 tutorial在线构建多文件 uploader ,添加评论, 当上传的文件移动到上传文件夹并且错误消息有效时,一切都成功运行。然而,虽然我可以 print_r 显示路径和文件名的 $uploaded 数组,但我真正想做的是在 html < img/> 标记中回显此路径,如下所示:

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

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

上传.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);

    }



}


?>

表单索引.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>

最佳答案

好的,在研究了您的代码后,我得到了以下对我来说可以正常工作的内容。

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>

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

   }
}
?>

关于php - 在 IMG 标签中回显图像路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25108818/

相关文章:

ios - 如何将实时照片上传到服务器?

php - dompdf : image not found or type unknown

php - array_search 找不到字符串?

php - 在 Symfony 2 Controller 中抽象通用功能的正确方法是什么

c# - 为什么在尝试读取正在写入的文件时出现未处理的异常 : System. IO.IOException?

c - Fscanf 读取 c 中 float 和字符的混合

android - 从 Android 崩溃应用程序上传大视频到 PHP 服务器

php - 如何在 PHP 标签内添加 css 属性

c++ - 计算搜索比较 C++

iPhone 上的 iOS WKWebView 文件上传失败