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

  • 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. – Matt Mar 05 '13 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... – milwaukee66 Mar 05 '13 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 – Gavin Mar 05 '13 at 21:14
  • The server where the image resides is a network share. The machine that hosts this query is external. – milwaukee66 Mar 05 '13 at 21:20
  • This sounds like a client (browser) security issue, which browser are you using? – Gavin Mar 05 '13 at 21:25
  • Firefox mainly, but I retest everything in IE as well. Same issue in both. – milwaukee66 Mar 05 '13 at 21:30
  • kjetilh has a solution for you. The issue you are facing is due to the browsers considering the files a security threat. You can read how to disable this feature in firefox here http://kb.mozillazine.org/Firefox_:_Issues_:_Links_to_Local_Pages_Don%27t_Work but that is not really workable if you have multiple users so definitely check out a proxy solution. – Gavin Mar 05 '13 at 21:33
  • Gavin, I want this to work in all browsers, of course, I will definitely be checking kjetilh's code... Fingers crossed... Thank you. – milwaukee66 Mar 05 '13 at 21:40

2 Answers2

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

?>
kjetilh
  • 4,821
  • 2
  • 18
  • 24
  • This looks somewhat promising, where would I use my $row['filename'] in this? – milwaukee66 Mar 05 '13 at 21:28
  • +1 for realising that the issue is network share rather than http server related. Looks like a good work around to me. – Gavin Mar 05 '13 at 21:29
  • @milwaukee66 I modified your link example with my concept solution. It's in the answer – kjetilh Mar 05 '13 at 21:31
  • kjetilh, I will check this out later or tomorrow, it looks promising, thanks! – milwaukee66 Mar 05 '13 at 21:37
  • kjetilh - Looks so promising...comes up blank page in both IE and Firefox :( The url returned looks like http://webapps.example.com/wc/test_ImageLoad3.php?image=//host\path\path\path\path\path\path\filename.TIF – milwaukee66 Mar 05 '13 at 22:02
  • I think the server path complicates things here. Try to debug it using both `fopen()` and `opendir()` on the file directory and see if you get any errors. I found this link interesting as well: http://stackoverflow.com/questions/1153824/php-access-network-path-under-windows – kjetilh Mar 05 '13 at 22:15
  • 1
    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. – Gavin Mar 05 '13 at 22:23
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"); 
Community
  • 1
  • 1
Bertrand
  • 388
  • 4
  • 13
  • you should make this a comment or add more on how this can be done – UnholyRanger Mar 05 '13 at 21:04
  • 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. – milwaukee66 Mar 05 '13 at 21:11
  • In your echo statement, what do you see for $row['filename'] as an example ? – Bertrand Mar 05 '13 at 21:16
  • 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? – milwaukee66 Mar 05 '13 at 21:18
  • @UnholyRanger thank you for your comment. I am new to stackoverflow.com, it helps me to better do my comments. Cheers. B. – Bertrand Mar 05 '13 at 21:18
  • The href value has no importance on the php side. So if your href is correct and you do not see the image, you might have an issue the client side and not on the server side. Your network might restrict access to certain domain. Are you able to open the image in your browser directly ? – Bertrand Mar 05 '13 at 21:20
  • I am able to paste in the path and open the image internally. I am able to externally run the query that returns the $row['filename'] so I don't see why I would be restricted to files if I am getting data from the database. All I want to do is take that $row['filename'] and open it in a browser. – milwaukee66 Mar 05 '13 at 21:22
  • I do not think that //host\image\path\path\path\path\path\filename.TIF is a valid href, you might have an issue in your image uri, that would be why you could not see it in your page. – Bertrand Mar 05 '13 at 21:24
  • //host\image\path\path\path\path\path\filename.TIF could only be open on your computer or in a network where people have the same access. Do you know if you image could be access from http:// also the \ is specific to windows, it should be / B. – Bertrand Mar 05 '13 at 21:28
  • not sure about the image, but the queries as posted above which return the data are readily working externally from http:// – milwaukee66 Mar 05 '13 at 21:30
  • I understand. Are your script and images directories on the same server ? For example script/index.php images/my-image.jpg www.my-domain.com/script/ -> point to your script and www.my-domain.com/images/my-image.jpg -> will be the uri of your image – Bertrand Mar 05 '13 at 21:31
  • Bertrand, you lost me, sorry. But I can tell you that the script I am using to query resides on a different machine then the images. The data I am querying which returns $row['fieldname'] does, however, reside on the same machine as the images. – milwaukee66 Mar 05 '13 at 21:42
  • I think this is what you are looking for. The images are on a computer that you cannot access from the Web or from which you do not know the http address. I would talk to a server admin to see if there is a way to access theses images through http or https. If it is not possible, you will have to copy the images on your application server (the one running php) to create a valid uri. B. – Bertrand Mar 05 '13 at 21:47