2013-10-02 42 views
9

如果有人能帮我弄清楚为什么浏览器无法加载图像(错误404),那将是非常棒的。代码有效,图像源是正确的,但我无法弄清楚什么是错的。 (使用本地主机)如何使用php显示文件夹中的图像 - PHP

$dir   = '/home/user/Pictures'; 
$file_display = array(
    'jpg', 
    'jpeg', 
    'png', 
    'gif' 
); 

if (file_exists($dir) == false) { 
    echo 'Directory \'', $dir, '\' not found!'; 
} else { 
    $dir_contents = scandir($dir); 

    foreach ($dir_contents as $file) { 
     $file_type = strtolower(end(explode('.', $file))); 

     if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true) { 
      echo '<img src="', $dir, '/', $file, '" alt="', $file, '" />'; 
     } 
    } 
} 
+0

那么你为什么不张贴代码? –

+0

刚刚做到了。忘了缩进。 – user2837048

+0

上面的代码存在语法错误,比如in_array(没有右括号,有人需要学习基本语法 – jerjer

回答

11

您在下面的语句中有错误。使用 。不,

echo '<img src="', $dir, '/', $file, '" alt="', $file, $ 

echo '<img src="'. $dir. '/'. $file. '" alt="'. $file. $ 

echo 'Directory \'', $dir, '\' not found!'; 

echo 'Directory \''. $dir. '\' not found!'; 
+3

实际上用于echo语句逗号可以被用来代替concat运算符 – jerjer

+1

这个逗号实际上有比较好的表现。在回声陈述 – jerjer

+1

字符串串联是内存密集 – jerjer

3

有两种方式做到这一点:

方法1.安全的方式。

把图像上/网络/ htdocs中/

<?php 
    $www_root = 'http://localhost/images'; 
    $dir = '/var/www/images'; 
    $file_display = array('jpg', 'jpeg', 'png', 'gif'); 

    if (file_exists($dir) == false) { 
     echo 'Directory \'', $dir, '\' not found!'; 
    } else { 
     $dir_contents = scandir($dir); 

     foreach ($dir_contents as $file) { 
      $file_type = strtolower(end(explode('.', $file))); 
      if (($file !== '.') && ($file !== '..') && (in_array($file_type, $file_display))) { 
       echo '<img src="', $www_root, '/', $file, '" alt="', $file, '"/>'; 
      break; 
      } 
     } 
    } 
?> 

方法2不安全,但更灵活。

将图像放在任何目录(apache必须有权限读取文件)。

<?php 
    $dir = '/home/user/Pictures'; 
    $file_display = array('jpg', 'jpeg', 'png', 'gif'); 

    if (file_exists($dir) == false) { 
     echo 'Directory \'', $dir, '\' not found!'; 
    } else { 
     $dir_contents = scandir($dir); 

     foreach ($dir_contents as $file) { 
      $file_type = strtolower(end(explode('.', $file))); 
      if (($file !== '.') && ($file !== '..') && (in_array($file_type, $file_display))) { 
       echo '<img src="file_viewer.php?file=', base64_encode($dir . '/' . $file), '" alt="', $file, '"/>'; 
      break; 
      } 
     } 
    } 
?> 

并创建另一个脚本来读取图像文件。

<?php 
    $filename = base64_decode($_GET['file']); 
    // Check the folder location to avoid exploit 
    if (dirname($filename) == '/home/user/Pictures') 
     echo file_get_contents($filename); 
?> 
+0

考虑我认为的第一种方法“break”语句并检查文件的结尾是否为“。”。或“..”是没有必要的,除非我错过了一些东西,然后想知道是什么。 –

7

这里是一个可能的解决方案对我的意见的解决方案#3 blubill的回答:

yourscript.php 
======================== 
<?php 
    $dir = '/home/user/Pictures'; 
    $file_display = array('jpg', 'jpeg', 'png', 'gif'); 

    if (file_exists($dir) == false) 
    { 
     echo 'Directory "', $dir, '" not found!'; 
    } 
    else 
    { 
     $dir_contents = scandir($dir); 

     foreach ($dir_contents as $file) 
     { 
      $file_type = strtolower(end(explode('.', $file))); 
      if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true)  
      { 
       $name = basename($file); 
       echo "<img src='img.php?name={$name}' />"; 
      } 
     } 
    } 
?> 


img.php 
======================== 
<?php 
    $name = $_GET['name']; 
    $mimes = array 
    (
     'jpg' => 'image/jpg', 
     'jpeg' => 'image/jpg', 
     'gif' => 'image/gif', 
     'png' => 'image/png' 
    ); 

    $ext = strtolower(end(explode('.', $name))); 

    $file = '/home/users/Pictures/'.$name; 
    header('content-type: '. $mimes[$ext]); 
    header('content-disposition: inline; filename="'.$name.'";'); 
    readfile($file); 
?> 
+0

我更喜欢这个 – afaolek