从内部服务器到图像文件的Php外部链接



我被卡住了,我希望这里有人能帮助我。

我想创建一个链接到一个文件的php脚本(该文件实际上是一个图像,一个。tif图像)。我不停地在谷歌上搜索,但无济于事。

防火墙正在阻止外部连接,但我正在模拟内部连接的机器上运行此查询。我目前使用这台机器允许外部用户运行命中内部数据库的查询,所以我知道这是有效的…

例如:

$result = pg_query($conn, $query); 
while($row=pg_fetch_assoc($result)) 
{ 
echo "<p>image details:<br />image: <u>" . $row['field'] . "</u>&nbsp;image date: <u>" . $row['field'] . "</u>&nbsp;image path: <u>" . $row['filename'] . "</u></p>";

我想转换为链接的查询部分是$row[' filename '],它返回为

//hostpathpathpathpathpathpathpath.TIF 

…但是现在我想访问与这些查询相关的某些文件。我想把这个文件名转换成一个url,当放入链接时,它会进入这个文件并打开它:

$imageURL = $row['filename'];
echo  "<p>Using imageURL: <a href='$imageURL' border='0' target='_blank'>Click Here</a></p>";

也许这是不可能的。我就是想不出怎么取出这张图片。

我没有imagemagick或imagick,我不想走那条路。谢谢你的帮助。

编辑我正在运行查询的机器是http://webapps.example.com
我正在查询的机器(图像驻留的地方)不是-我试图返回的图像路径(作为链接)是//hostipaddresspathpathpathpathpathpath pathfilename.TIF

你可以使用一个代理页面,你可以链接到它来完成渲染图像的工作。

让我们调用代理页面readimage.php,它接受文件名或路径作为参数。

你的链接看起来像这样:

<?php 
$imageURL = $row['filename'];
echo  "<p>Using imageURL: <a href='readimage.php?image=$imageURL' border='0' target='_blank'>Click Here</a></p>";
?>

readimage.php

<?php
$img = isset( $_GET['image'] ) ? $_GET['image'] : null;
if ( !$img )
    return;
// Build internal file path
//$filepath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'myimages' . DIRECTORY_SEPARATOR . $img;
$filepath = $img;
//==========================
// VERY IMPORTANT TO WHITELIST THE PATH OR RESULT to make sure you're not showing unintended data
//==========================
if ( file_exists( $filepath ) ) {
    // Find mime type 
    $mime = '';
    // Try using the fileinfo functions. Requires PHP >= 5.3 and PECL 1.0
    if ( function_exists( 'finfo' ) ) {
        $finfo = new finfo( FILEINFO_MIME ); // return mime type ala mimetype extension
        /* get mime-type for a specific file */
        $mime = $finfo->file( $filepath );
    } 
    // No mime yet? Try to use the deprecated mime_content_type() function 
    if ( !$mime && function_exists( 'mime_content_type' ) ) {
        $mime = mime_content_type( $filepath );
    }
    // Not yet? Fallback to extensions :(
    if ( !$mime ) {
        $ext = pathinfo( $filepath, PATHINFO_EXTENSION );
        switch ( $ext ) {
            case "jpg" :
            case "jpeg" :
            case "jpe" :
                $mime = "image/jpg";
            break;
            case "png" :
            case "gif" :
            case "bmp" :
            case "tiff" :
                $mime = "image/" . strtolower( $ext );
            break;
        }
    }
    if ( $mime ) {
        header( 'Content-type: ' . $mime );
        readfile( $filepath );
    }
}
?>

如果你有一个代理地址可以让你发送请求,你可以使用curl。

** update 2

图片来自curl使用curl从url保存图像

代理后的旋度:

curl_setopt($ch, CURLOPT_PROXY, "http://to.the.proxy.address"); 
curl_setopt($ch, CURLOPT_PROXYPORT, 8080); 
curl_setopt ($ch, CURLOPT_PROXYUSERPWD, "your password"); 

最新更新