lost_toys Posted April 15, 2014 Share Posted April 15, 2014 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.pngAnd 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? Quote Link to comment https://forums.phpfreaks.com/topic/287768-how-to-display-a-variable-filename-picture-in-a-directory-above-web/ Share on other sites More sharing options...
requinix Posted April 15, 2014 Share Posted April 15, 2014 header()s and readfile() in a script. header("Content-Type: image/png"); header("Content-Length: " . filesize($path)); readfile($path); exit; Quote Link to comment https://forums.phpfreaks.com/topic/287768-how-to-display-a-variable-filename-picture-in-a-directory-above-web/#findComment-1476148 Share on other sites More sharing options...
lost_toys Posted April 15, 2014 Author Share Posted April 15, 2014 I 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. Quote Link to comment https://forums.phpfreaks.com/topic/287768-how-to-display-a-variable-filename-picture-in-a-directory-above-web/#findComment-1476213 Share on other sites More sharing options...
Ch0cu3r Posted April 15, 2014 Share Posted April 15, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/287768-how-to-display-a-variable-filename-picture-in-a-directory-above-web/#findComment-1476215 Share on other sites More sharing options...
lost_toys Posted April 15, 2014 Author Share Posted April 15, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287768-how-to-display-a-variable-filename-picture-in-a-directory-above-web/#findComment-1476220 Share on other sites More sharing options...
Ch0cu3r Posted April 15, 2014 Share Posted April 15, 2014 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; } Quote Link to comment https://forums.phpfreaks.com/topic/287768-how-to-display-a-variable-filename-picture-in-a-directory-above-web/#findComment-1476224 Share on other sites More sharing options...
lost_toys Posted April 15, 2014 Author Share Posted April 15, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287768-how-to-display-a-variable-filename-picture-in-a-directory-above-web/#findComment-1476230 Share on other sites More sharing options...
Ch0cu3r Posted April 15, 2014 Share Posted April 15, 2014 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/ Quote Link to comment https://forums.phpfreaks.com/topic/287768-how-to-display-a-variable-filename-picture-in-a-directory-above-web/#findComment-1476232 Share on other sites More sharing options...
lost_toys Posted April 15, 2014 Author Share Posted April 15, 2014 Okay, I have done so and now I am getting The image at image.php can not be displayed because it contains errors. I really appreciate your guys help, I never would have made it this far. Quote Link to comment https://forums.phpfreaks.com/topic/287768-how-to-display-a-variable-filename-picture-in-a-directory-above-web/#findComment-1476243 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.