0

I'm stuck and I was hoping somebody here could help me.

I want to create a link to a file in a php script (the file is actually an image, a .tif image). I have Google d this relentlessly, to no avail.

The firewall is blocking external connections but I am running this query on a machine that mimics an internal connection. I currently use this machine to allow external user to run queries that hit the internal database so I know this works…

For instance this part works just fine:

$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>";

The part of the query that I want to turn into a link is the $row[‘filename’], which is returned as

//host\path\path\path\path\path\path\path.TIF 

… but now I want access to certain files associated with those queries. I want to turn this filename into a url that, when put into a link, goes to this file and opens it:

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

Maybe this isn't possible. I just can't seem to figure out how to fetch the image.

I do not have imagemagick or imagick and I don't want to go that route. Thanks in advance for any help.

EDIT The machine I am running the query on is http://webapps.example.com
The machine I am querying (where the image resides) is not - the image path I am trying to return (as a link) is //hostipaddress\path\path\path\path\path\path\filename.TIF

8
  • Show an actual example of what $imageURL contains currently and what you'd like it to look like. I'm confused if your question is about reformatting that path vs routing information through the network. Commented Mar 5, 2013 at 21:01
  • It is returned as //host\path\path\path\path\path\path\path.TIF. I don't know what I want it to look like, that's the problem. I have reformatted it to look like \\host\path\path\path\path\path\path\filename.TIF which, when I copy and paste that into an internal browser, works fine. I can't get the link to remove the www.domain.com/ before this in the link. Not sure if that's the problem or not... Commented Mar 5, 2013 at 21:08
  • Is the image host running a web server or is this a network share? I ask because 'from memory' you cannot link to files on network resources in that way using firefox or chrome, but internet explorer should work with a file link such as file://///host/path/path/path/path/path/path/path.TIF Commented Mar 5, 2013 at 21:14
  • The server where the image resides is a network share. The machine that hosts this query is external. Commented Mar 5, 2013 at 21:20
  • This sounds like a client (browser) security issue, which browser are you using? Commented Mar 5, 2013 at 21:25

2 Answers 2

2

You can use a proxy page which you can link to normally which does the job of rendering the image.

Let's call the proxy page readimage.php and it takes the filename or path as an argument.

Your link would look something like this:

<?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 );
    }
}

?>
Sign up to request clarification or add additional context in comments.

7 Comments

This looks somewhat promising, where would I use my $row['filename'] in this?
+1 for realising that the issue is network share rather than http server related. Looks like a good work around to me.
@milwaukee66 I modified your link example with my concept solution. It's in the answer
kjetilh, I will check this out later or tomorrow, it looks promising, thanks!
One thing to consider here is the user account that the php process is running under, be sure it has permission to access the shares.
|
0

If you can have a proxy address that would allow you to send request outside, you could do it using curl.

** update 2

Image from curl Save image from url with curl PHP

Curl behind a proxy:

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

13 Comments

you should make this a comment or add more on how this can be done
I've tried Curl, file_get_contents, fopen, readfile, none of them let me use the returned $row['filename'] as the a href part of the link to open the image.
In your echo statement, what do you see for $row['filename'] as an example ?
Echo returns (exactly formatted) //host\image\path\path\path\path\path\filename.TIF I suppose this method may work, however I definitely don't want to save the image anywhere, I want to put it's path into a link that when clicked will open the image on the user's machine. Any more code to do that?
@UnholyRanger thank you for your comment. I am new to stackoverflow.com, it helps me to better do my comments. Cheers. B.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.