how to execute jpg or other extensions like php - php

I want to execute only one or two jpg files like php in a folder.
forexample
www/folder/index.jpg should run and shown like www/folder/index.php
or
www/folder/index.php should run and shown like www/folder/index.jpg but it should work as php and it should shown as jpg
and other jpg files should run it is own name
www/folder/photo.jpg > www/folder/photo.jpg

You can't execute JPEG files per se, but you can have them be a PHP script that generates JPEG data to use in e.g. a <img> tag.
You need to set the handler for the file to application/x-httpd-php, output a content type of image/jpeg in the script, and, most importantly, output JPEG data. Might want to consider naming it <something>.jpg.php so you don't have to do the first one though, since that's server configuration.

Related

Display animated GIF from outside public directory (GD?)

Once users upload their images to a non public (i.e outside htdocs or public_html folder) folder, I use GD to fetch the image from that folder. This is all in the name of security, as the images themselves are never displayed without being processed.
The issue lies in animated GIFs, which GD is only capable of displaying by showing only it's first frame.
I know that there are classes out there that can slice the gifs up into their respective frames and "glue" them back together, but I'm looking to display these on the fly...
I don't want to create a new animated gif and show that, but rather have a GD script that renders the image from the information it gets from the photo directory, located outside of the public folder.
Thanks so much!
If you're not manipulating the image at all, I would just return the contents of the file with the correct header.
For example:
<?php
$file = 'whatever.gif';
// Do whatever checks you want on permissions etc
header('Content-type: image/gif');
readfile($file);
?>
Readfile outputs the contents of the file, and the header makes sure the browser sees it as a gif and shows it. Because you're not using GD it will still be animated.
The catch with this method is you'll need to know the content type for each file to server it correctly.
http://php.net/manual/en/function.readfile.php

Clean up after creating the pdf

I'm generating a pdf file with html2fpdf.
$pdf = new HTML2FPDF();
$pdf->HTML2FPDF("P","mm","A4");
$pdf->AddPage();
$pdf->WriteHTML($html);
$pdf->output('sample.pdf');
This sample works great. But:
How do I delete the pdf after the output? I just want to have links in my tool, the users can download the pdf and after that it shoud be deleted on the server.
How can I 'clean up' after generating the pdf?
You can use PHP's file deletion function called unlink()
Call this function with the full path to the generated PDF file (or any file for that matter) and PHP will delete that file.
http://php.net/manual/en/function.unlink.php
You don't necessarily have to delete the file immediately after the user has downloaded it. You can just as easily place all the generated files in one central folder and have a cron job execute a more general clean up script simply removing the older files.
One method could be -
Scan the contents of the folder using scandir().
Iterate over its files in a foreach loop..
Inspect the creation time of each file using filemtime().
If the creation time was over hour ago, delete the file using unlink().
Because you are generating the PDF file yourself within your PHP code, I didn't mention the permissions consideration. Here would be a good place to mention that your PHP must have the correct file system permissions in order to perform any action on the file system. You are creating a PDF file so it's safe to assume that you have the correct permissions to make changes to the file system but if you plan on using this unlink() function in other scripts make sure that the files you are dealing with have the correct permissions set.
If you don't add the 'F' flag to the output function there will be no pdf files stored on the server at all:
$pdf->output('sample.pdf', 'F'); //stores PDF on server
In your case the script itself behaves like an actual pdf file. So, creating a link to the script is just like a link to the pdf, except that the PDF is created every time the script is requested. To tell the browser it's a PDF the content-type response header must be set to application/pdf:
content-type: application/pdf
This way the broser knows that it's a pdf even if the URL is ending in a .php. You can use rewrite engine to make it end in pdf or whatever else.
Sending the headers is done by the fpdf/tcpdf. In short: you don't have to do any cleanup, because no pdf file is stored on the server.
If you wonder what the name is for than, try saving the pdf file. The recommanded name when saving will be sample.pdf.
Reference:
PHP header() function, at the examples there is one for sending pdf
FPDF::Output()
TCPDF::Output()

PHP set an image on my server as uploaded temp file

I am editing a photo gallery script to allow the use of TIFF to be uploaded and saved, but i must keep the files in jpg format also for web viewing.
What I have done is installed image magick to convert TIF to JPEG, once i have it converted I want the script to continue with making thumbnails, zoom images, etc. it makes them from
$_FILES['image']['tmp_name']
Is there a way to set my newly created file as $_FILES['image']['tmp_name']? my new jpeg file path is set to $nw.
basically I need
$nw='path/to/newfile.jpg';
$_FILES['image']['tmp_name']=$nw;
but it does not work. any ideas?
If you need to work on the same file across multiple page requests, move it somewhere safe using move_uploaded_file.
If the functions that you wrote require access to $_FILES['image']['tmp_name'], rewrite them to accept the name of the file as a parameter and call them using the new location of the file as argument.

Image SRC From PHP Script on IIS - Not Displaying Consistently

Last week I converted my page img src values from pointing at image files to using a PHP script to serve up the images. The primary reason was to accommodate both files and database BLOBs as the actual source.
Now when a user goes to a page, sometimes images show and sometimes not. If not, and the page is refreshed\reloaded, then the image appears. When images do not appear, sometimes it is an image the user has already accessed previously today.
I am stumped.
Here is the img tag:
<img src="../somedir/image_script.php?i=1234">
The image_script.php file figures out where to get the image from, then finishes up with:
header("Content-type: image/jpeg");
if($from_db){
print $image_blob;
} else {
$im = imagecreatefromjpeg($image_file);
imagejpeg($im,null,100);
imagedestroy($im)
}
I am using PHP 5.2.8 on IIS 6 using FastCGI. There are no cache headers on the image_script.php file nor on the directory it is in. Currently 99.9% of the images are file based, so I do not know if there is a difference in result between db-based and file-based images. When I go directly to image_script.php in my browser it returns the requested image (i=????) 100% of the time.
a> Any clue as to why the hit and miss with images being displayed? and,
b> what would be a proper way to actually cache the images served up by the PHP script? (they are very static)
Scott
Hmm. Can't tell for sure, but maybe your imagecreatefromjpeg is occasionally running out of memory? In that case, you'd serve an error message out as JPEG data and never see it, right?
Incidentally, wouldn't just grabbing the image file in as a string and shovelling it out without going through imagecreatefromjpeg/imagejpeg/imagedestroy be more efficient? It looks like you're reading a JPEG file, creating an internal PHP memory image from it, then reconverting it to a JPEG (at hefty 100% quality) then serving that data out, when you could simply read the JPEG file data in and print it, like you do from the database.
What happens if you do, say...
...
} else {
header ('Content-length: ' .filesize($image_file));
readfile ($image_file);
}

Downloading images from a server iPhone SDK

I have created a program which allows me to upload images to my server. They are given a random file name when uploaded. I want to be able to download all the images from a folder on the server so I can display them in my application. The only example I have seen requires that I know the file name of the images which I don't. How could I download all the images in a given directory (and store the downloads in an NSArray)? If there is no native way to do it does anyone know a way that it could be done via calling a PHP script? (I use a PHP script which the iPhone calls to upload the images).
Thanks.
To list all images in a directory using PHP and return a JSON encoded string:
$path = '/full/path/to/images/';
// find all files with extension jpg, jpeg, png
// note: will not descend into sub directorates
$files = glob("{$path}/{*.jpg,*.jpeg,*.png}", GLOB_BRACE);
// output to json
echo json_encode($files);
you can call a php script that will return the url for all of your images.
something like
yoursite.com/image123.jpg;yoursite.com/image213.jpg;yoursite.com/imageabc.jpg
then you parse the result, split by ";" and get the array of urls which you need to download.

Categories