Page 1 of 2
PHP in a image file
Posted: Sat Nov 15, 2003 5:59 am
by vigge89
I want to somehow create a php script, like image randomizer or something, and then save the script as php file, but with png extension, and then when the image should be outputted, it runs the script as a PHP script, like somehow telling the server to execute the file as a php file, and not as an image. So it would just be a PHP file, but with .png extension. The file should then be able to be runned from a different page.
Is this possible?
Posted: Sat Nov 15, 2003 7:32 am
by qads
very much so, check out my avatar, its a random image

.
step 1:
make a httaccess with this code:
Code: Select all
AddType application/x-httpd-php .png
save it as ".htaccess"
step 2:
put the code below in notepad and save it as "*.png", * = file name that u want.
make sure you define a correct path to a images folder for $folder!!!!!
Code: Select all
<?php
Header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
Header("Expires: Thu, 11 May 1983 08:52:00 GMT");
Header("Pragma: no-cache");
$folder = ".";
/* no need to edit the settings below */
if($handle = opendir($folder))
{
while($files = readdir($handle))
{
if ($files != "."
&& $files != "..")
{
$g[] = $files;
}
}
}
$t = array_rand($g);
header("Location: $g[$t]");
?>
step 3:
upload both files to your server and go to
http://www.yoursite.com/imagefile.png.
hope that helps.
Posted: Sat Nov 15, 2003 7:38 am
by vigge89
but if I have other png-files, will they also be processed with PHP, and they will have no output?
I currently do not have any, but i just wanna know

Posted: Sat Nov 15, 2003 8:41 am
by qads
ALL files in $folder's path will be processed, i suggest you only keep .png files in there.
but if you want to only process .png gile then use this:
Code: Select all
<?php
Header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
Header("Expires: Thu, 11 May 1983 08:52:00 GMT");
Header("Pragma: no-cache");
$folder = ".";
/* no need to edit the settings below */
if($handle = opendir($folder))
{
while($files = readdir($handle))
{
if ($files != "."
&& $files != ".."
&& strstr($files, ".png"))
{
$g[] = $files;
}
}
}
$t = array_rand($g);
header("Location: $g[$t]");
?>
Posted: Sat Nov 15, 2003 8:53 am
by vigge89
well, i just want to process ONE png file as PHP, and that one should be the script. All the others should just be normal Png-images
Posted: Sat Nov 15, 2003 9:01 am
by Weirdan
Posted: Sat Nov 15, 2003 9:04 am
by vigge89
well, if there isn't another way to do it, i'll just stick to using no ther png's.
Oh, btw, is there possible to just assign
"AddType application/x-httpd-php .png"
in httaccess to only one directory?
Posted: Sat Nov 15, 2003 9:37 am
by Linkjames
The htaccess should only effect the folder you place it in. Just use a seperate folder for the .png you want to run as .php
Posted: Sat Nov 15, 2003 9:38 am
by vigge89
ok, that's what i wanted to know, now i just have to wait for my 3rd host-change in this 2 last months :/
Posted: Sat Nov 15, 2003 10:30 am
by d3ad1ysp0rk
put your .png file (that is actually php) in the main folder (/new) and then make an imgs folder, and put the ones u want to be processed as images, in a new folder (/new/imgs/).
Posted: Sat Nov 15, 2003 11:34 am
by vigge89
LiLpunkSkateR wrote:put your .png file (that is actually php) in the main folder (/new) and then make an imgs folder, and put the ones u want to be processed as images, in a new folder (/new/imgs/).
I know, that's what i currently have
Posted: Sun Nov 16, 2003 2:27 pm
by vigge89
Well, when run image.png, i sometimes get re-directed to the .htacces file, i've got 2 gif-files in the same dir and folder set to ".".
Posted: Sun Nov 16, 2003 2:45 pm
by vigge89
oh, got it to work, just added
&& $files != ".htaccess"
in the script

, thanks all
Posted: Sun Nov 16, 2003 3:20 pm
by vigge89
hmm, when using the script in signature in IPB, it only displays one img (it doesn't dandomly pick one, just the same all the time).
Why won't it work?
maybe it's the header:location?
have IPB disabled it or something
Posted: Tue Nov 18, 2003 10:28 am
by vigge89
does it work here?
testing right now
Edit: no it doesn't work here or on IP boards either, why?
Well, it works here, but the image seems to get cached, so i have to reload the pge lots of times before another one shows up...