Jump to content

Processing usage ?


Distant_storm

Recommended Posts

I plan to use this in a page as an example

 

header("image/png"); < or whaterver it is

imagefrompng("image.png");

 

ok now creating an image everytime a user see's the iamge or navigates to a new iamge. and destroying it after woudl this lay heavy on the server ?

 

This is to stop image pathnames from being revealed

Link to comment
Share on other sites

So are you trying to read the file from disk or generate an image and spit it out?

 

If you're wanting to generate it, you could always cache it, and if you already have it on disk, the most effecient thing possible short of caching it in memory is just reading it out to the client.

Link to comment
Share on other sites

$file = (isset($_GET['file'])) ? $_GET['file'] : false;
if($file === false || str_pos($file, '/') !== false || str_pos($file, '\\') !== false) exit; //if the file isn't given or it has / or \ die
if($handle = @fopen('files/' . $file) {
      while(!feof($handle)) {
           echo fread($handle, 8192); //read 8KB at a time
      }
}
else {
     //file not found or unable to open
}

 

The reason I would do it with fopen and fread there instead of file_get_contents is because with file_get_contents you load the entire file into PHP, and larger files could become cumbersome.

Link to comment
Share on other sites

OK....  Example time.

 

Pretend the following

 

Your web root is /html/

Your images are in /images/

Your wanting the file /images/image1.jpg

You're at the page http://yoursite.com/images.php?file=image1.jpg

 

To show /images/image1.jpg, images.php would need to look like this:

 

$file = (isset($_GET['file'])) ? $_GET['file'] : false;
if($file === false || str_pos($file, '/') !== false || str_pos($file, '\\') !== false) exit; //if the file isn't given or it has / or \ die
if($handle = @fopen('/images/' . $file) {
      header("Content-type: image/jpeg");
      while(!feof($handle)) {
           echo fread($handle, 8192); //read 8KB at a time
      }
}
else {
     //file not found or unable to open
}

 

So

<img src="images.php?file=image1.jpg" />

 

Would correctly show the content from /images/image1.jpg.

 

You would most likely want to make the Content-type change depending on the file extension by the way....

Link to comment
Share on other sites

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.