【发布时间】:2014-10-30 17:49:14
【问题描述】:
我知道这可能是一个非常微不足道的问题,尽管我在过去的几个小时里一直在爬网,但我似乎无法解决这个问题..
我有一个 image_get.php 文件,它应该显示一个与 php 文件位于同一目录中的图像文件“keepcalm.png”。
我正在使用一个类来处理所有与图像相关的操作。
image_get.php
<?php
include "classes/image.class.php";
include "classes/database.class.php";
$database_sso = new database('SSO'); //will be needed later and is dummy for now
$testimage = new image($database_sso, "1"); //parameters are dummy for now
$testimage->show_image();
?>
image.class.php
<?php
class image{
private $database_image; //Image Database containing all images
private $imageid; //ID of Image in Database
private $filepath; //Path to image file
//PUBLIC FUNCTIONS
public function __construct($database, $imageid){
$this->database_image = $database; //Right now this is just a placeholder
$this->imageid = $imageid; //Right now this is just a placeholder
}
public function show_image(){
$remoteImage = $this->get_local_link_from_id($this->imageid); // Right now this returns "keepcalm.png"
header('Content-Type: image/x-png')
$returnstring = readfile($remoteImage);
}
//PRIVATE FUNCTIONS
private function get_local_link_from_id($imageid){
$local_link = "keepcalm.png"; //dummy for now
return $local_link;
}
}
?>
我得到的输出是完全乱码,可以在这里看到 -> http://niederrad.no-ip.org/portal/image_get.php
我错过了什么? 我已经尝试了上述的很多迭代,并且完全不知道应该如何进行..
【问题讨论】:
-
您的带有 content-type 的标头不会发送到浏览器。这是函数,不是字符串!!
header('Content-Type: image/x-png')。至少应该是$returnstring = readfile($remoteImage); header('Content-Type: image/x-png'); -
你会怎么寄? $returnstring = header('Content-Type: image/x-png').readfile($remoteImage);返回$返回字符串;那行不通……
-
我写的!
$returnstring = readfile($remoteImage); header('Content-Type: image/x-png');你应该运行这个函数,它会生成http响应头,而不是返回它。 -
嗯。如果我按该顺序执行此操作,则会收到“标头已发送”错误。如果我把 header('Content-Type: image/x-png');首先我只得到一个空的图像图标.. 见niederrad.no-ip.org/portal/image_get.php
-
这意味着你的代码是一团糟,阅读这个(在发送标头之前应该没有输出到浏览器) - stackoverflow.com/questions/8028957/… 你打算从函数返回
$returnstring吗? ??如果您不返回文件的内容或不从函数内将其发送到浏览器 - 将不会显示。