【问题标题】:Echoing image path in a IMG tag在 IMG 标签中回显图像路径
【发布时间】:2014-09-26 08:16:20
【问题描述】:

我在网上关注了tutorial,用于构建多文件上传器,添加了 cmets, 随着上传的文件移动到上传文件夹并且错误消息正常工作,一切都成功运行。但是,虽然我可以 print_r 显示路径和文件名的 $uploaded 数组,但我真正想做的是在 html 标记中回显这个路径,如下所示:

  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>

【问题讨论】:

  • echo ""; ??未测试。
  • 好的,我会试一试,谢谢!

标签: php file upload echo


【解决方案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>

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

   }
}
?>

【讨论】:

  • 成功!感谢您花时间帮助我。
【解决方案2】:

嗯,你基本上是在输入&lt;img src='apple.jpg'/&gt;,这很可能不会导致任何结果。

你真正想做的事:&lt;img src='/path/to/apple.jpg/&gt;

在 PHP 中:echo "&lt;img src='/path/to/".$name." /&gt;'";

【讨论】:

  • 感谢 Linial: echo "";是否可以显示多个图像?因为只显示最后选择的图像。
猜你喜欢
  • 2022-01-26
  • 2021-11-30
  • 1970-01-01
  • 2019-09-02
  • 1970-01-01
  • 2014-09-29
  • 1970-01-01
  • 2022-01-24
相关资源
最近更新 更多