从数据库中获取图像,然后在url中写入文件

时间:2012-04-10 22:02:52

标签: php header

我正在开发我的游戏网站,但为了让Facebook API正常工作,我必须制作一个新的脚本,可以根据游戏的ID获取缩略图。这是这个脚本试图做的,但我得到了这个错误。

Warning: readfile(localhost/thumbs/6c9e40d35e8cf07e.png) [function.readfile]: failed to open stream: No such file or directory in B:\home\home\thumb.php on line 22

我想知道你们中是否有人可以帮助我。

<?php
require_once "functions.php";
if(!isset($_REQUEST['t'])){
die("No ID Defined");
}
$t = $_REQUEST['t'];
$t = intval($t);
connect();
$fetch = fetchdata("select * from `games` where id = $t");
$thumb = $fetch{'thumb'};
$file = "/thumbs/$thumb";
$filename = basename($file);
$file_extension = strtolower(substr(strrchr($filename,"."),1));
switch( $file_extension ) {
    case "gif": $ctype="image/gif"; break;
    case "png": $ctype="image/png"; break;
    case "jpeg":
    case "jpg": $ctype="image/jpg"; break;
    default:
}
header($ctype);
readfile($_SERVER['HTTP_HOST'].'/thumbs/'.$thumb);
?>

2 个答案:

答案 0 :(得分:2)

您不需要httphttps让PHP readfile正常工作......实际上使用http会使slower加载这些图片... 。

 readfile('/thumbs/'.$thumb); //or
 readfile(PATH_TO_LOCAL_DIR. '/thumbs/'.$thumb);

应该可以正常工作。

这是您的脚本版本,共10行

require_once "functions.php";
if (! isset ( $_REQUEST ['t'] )) {
    die ( "No ID Defined" );
}
connect ();
$fetch = fetchdata ( sprintf("select * from `games` where id = '%d'",$_REQUEST ['t']) );
if(!is_file("thumbs/" . $fetch ['thumb'] ))
    die("Thum Does not exist");
header ( "Content-type: ", image_type_to_mime_type ( exif_imagetype ( $fetch ['thumb'] ) ) );
readfile ( "thumbs/" . $fetch ['thumb'] );

答案 1 :(得分:0)

您需要实际设置mime-type标头,如下所示:

header(sprintf("Content-Type: %s", $ctype));

然后看@Baba's answer