Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8282839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:31:14+00:00 2026-06-08T10:31:14+00:00

I have a php script that I use to send images to mobile devices

  • 0

I have a php script that I use to send images to mobile devices on request. In the current version the script runs and before the end I use imagePNG() to output the image to the device but in doing some reading in the examples in the php manual online I saw this example:

<?php
 header("Content-Type: image/png");

 # Generate cachefile for image, if it doesn't exist
 if( !file_exists($cachefile) ) {
   $im = generateimage();     # some code generates an image resource
   imagepng($im, $cachefile); # store the image to cachefile

   # don't output it like this:
   /* imagepng($im);*/

   imagedestroy($im);
 }

 $fp = fopen($cachefile, 'rb'); # stream the image directly from the cachefile
 fpassthru($fp);
 exit;
?> 

I used this example with some modifications to send my images to mobile and it works fine but I have some questions Id like to ask:
1) is this more efficient than creating an image from the file and sending using imagePNG?

2) Also shouldnt I close the file right after using fpassthru?

3) If I do use fopen does that mean that the file is locked meaning no other devices but that one would be able to access it for that moment its being streamed?

Any opinions on the matter would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:31:24+00:00Added an answer on June 8, 2026 at 10:31 am

    I think that caching the generated image is a good idea if you have a busy site. It will certainly save memory and CPU cycles in exchange for some hard drive space.

    In regards to closing the file after using fpassthru, it is unnecessary as PHP will close the file when the script terminates. Since the very next line calls exit, there is no need to explicitly close the file. If there was more going on after outputting the image, I would recommend closing the file when no longer in use.

    As for your third question, since you have not called flock, the file is not locked. If one process has it open for reading, another can open it for reading at the same time.

    Just a final note, you could use the function readfile to output the file. This saves you having to call fopen, but internally, readfile and fpassthru call the same PHP function php_stream_passthru so there is little to no difference in time or efficiency based on which one you use.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php script that i use to send mail to customers. How
In php I have a script, that I use mod_rewrite to redirect all calls
I have php script that creates a temporary watermark image for users that are
I have a php script that inserts data from an Android app into a
I have a php script that watches for the newest json file in a
I have a php script that grabs links from another website. I am storing
I have a PHP script that very much like a commenting system, but requires
I have a PHP script that opens an XML document, gets the DOM and
I have a PHP script that creates large complex tables. I am attempting to
I have a PHP script that executes a .bat file using system(cmd /c C:\dir\file.bat);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.