我使用的是codeigniter 2.1.3,我在/home文件夹中有网站外的图像,无法显示图像


i am using codeigniter 2.1.3 and i have the images outside the website in the /home folder and cannot show the images

我正在使用Codeigniter 2.1.3,我在/home文件夹中有网站外的图像,无法显示图像。

 <img src="<?php echo img('/home/pedro/iagora.jpg');?>" title="" alt="avatar_<?php echo $project['id']; ?>" width="120" height="80"/>

我试过:

     <img src="<?php echo '/home/pedro/iagora.jpg';?>" title="" alt="avatar_<?php echo $project['id']; ?>" width="120" height="80"/>

和:

     <img src="/home/pedro/iagora.jpg" title="" alt="avatar_<?php echo $project['id']; ?>" width="120" height="80"/>

但这些都不起作用。

如果文件在web根目录之外,则通常无法通过HTTP访问它们。您需要编写一个PHP脚本,为您输出图像:

<img src="/get_image.php?i=iagora.jpg" title="">

/get_image.php将是检索图像并为您输出图像的PHP文件。i参数将是您想要的图像的名称。

该文件看起来像:

<?php
    $file_name = $_GET['i'];
    $file = '/home/pedro/' . $file_name;
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
?>

我建议您在基本文件夹中创建文件夹"assets"。在"assets"中,您将存储js、css、上传图像和所有其他内容。在这种形式下,你可以更好地组织你的应用

然后,您可以使用base_url()加载图像。例如:

<img src="<?php echo base_url(); ?>assets/your_folder/your_image.jpg" title="" alt="avatar_<?php echo $project['id']; ?>" width="120" height="80" />

我将脚本文件保存在网站的根目录上,现在它正在运行

<img src="<?php echo base_url().'get_image.php?i='.$images_thumb_dir.$project['avatar'];?>" title="" alt="avatar_<?php echo $project['id']; ?>" width="120" height="80"/>

谢谢Pedro