Jump to content

Recommended Posts

I have tried everything I can think of with passthru and imagecreatefrompng and whatnot. I cant copy the file to a web directory, so that is out. I just have to be able to display the image.

So, my php script calls a program that creates an image based on user input:

$file_name = "/site.com/public/www/prod/data/" . $var1 . "_" . $var2 . "_" . $var3 . PHP_EOL;
$image_name = $file_name . "_pic.png";

So the program saves the image, say...
/site.com/public/www/prod/data/12_24_36_pic.png

And yeah, I can totally see the image there. It looks great.
However,

The site's web root directory is this:
/site.com/public/www/prod/htdocs/

So the image is sitting in a directory above what the browser can see. How can I display that image to people?

 

 

So, my php script calls a program that creates an image based on user input:

 

$file_name = "/site.com/public/www/prod/data/" . $var1 . "_" . $var2 . "_" . $var3 . PHP_EOL;

$image_name = $file_name . "_pic.png";

Why is PHP_EOL used in the filename? This will add a new line characters to the filename, which does not seem right. With the above code this will be generated filename (variables substituted with yyy, xxx and zzz)

/site.com/public/www/prod/data/yyy_xxx_zzz
_pic.png

 

 

 have tried this by putting the script in a seperate php file, and then calling it by using img src=image.php, but it just tells me that the image has errors and can't be displayed.

Make sure you're using the correct filepath ($path) in requinix code. Are you sure the path is correct?

Okay, I can display the image if I tell the image,php the exact file to read, (/site.com/public/www/prod/data/12_24_36_pic.png) as opposed to $image_name, but that doesn't really help me... I need to be able to pull the image from the variable name. How can I pass the variable between these two scripts? I've tried to include the other php file, but that didn't work.

 

And I removed the EOL, you are right. That was a stupid mistake.

So the image path is defined from another script? And you want to pass that path onto image.php to display the image which is stored outside of the image root.

 

You could use mod_rewrite, for example output an image path like this in a img tag

<img src="/data/xxx_yyy_zzz_pic.png" />

Now using mod_rewrite  and can capture any url that starts with /data/ and ends in _pic.png and pass the image filename to image.php, which willl then serve the image from /site.com/public/www/prod/data/.

 

So add the following to a .htaccess (that is the actual name) file in the htdocs folder

rewriteEngine On

rewriteRule ^data/([\d+]_[\d+]_[\d+]_pic.png)$ image.php?img=$1 [NC,L]

Now a slight modification to requinix's code would be

<?php

$img_path '/site.com/public/www/prod/data/' . basename($_GET['img']);

// make sure the requested image exists in /site.com/public/www/prod/data/
if(file_exists($img_path))
{
   // serve the image
   header("Content-Type: image/png");
   header("Content-Length: " . filesize($Img_path));
   readfile($img_path);
   exit;
}

Oh my god, its so close. I can taste it.

 

Its working almost perfectly.

 

The script is now trying to display the correct variable, but its telling me:

The requested URL /site.com/public/www/prod/data/12_24_36_pic.png was not found on this server, so its still not displaying the image.

 

PS: Never in a million years would I have thought to use mod_rewrite, brilliant.

So you're using  /site.com/public/www/prod/data/12_24_36_pic.png  as the img src? This is wrong, you misunderstood my post slightly. The path used in the img src should be like /data/12_24_36_pic.png (note starts with  /data  and ends in  _pic.png)

 

mod rewrite will capture this url, and pass only theeimage name to image.php, which will then serve the image from /site.com/public/www/prod/data/

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.