Avatar of hqnetwork
hqnetwork asked on

Unlink priority? Seems to delete before running other code

I have a small script that is supposed to display an image, then delete it directly after it is displayed on screen. However, php's unlink(); command seems to be deleting the image before it is displayed.

the Unlink is definitely below where the image is displayed, as well as being encapsulated within an IF statement to ensure it's not executed before it should be.

Perhaps I need to use a different method to delete?

BTW, this is on a linux box as you can probably tell byt eh $img2delete path.
printf("<img src='".$dir.$file."'><br>");
	$lpcnt++;
	$_SESSION['curr_file'] = $file;
	$img2delete = '/var/www/html/Images/'.$_SESSION['curr_file'];
	
	$diff = (time() - filemtime("$img2delete"))/60*60;
	echo "<p>File age in seconds: $diff</p>\n";
	$isviewed = "true";
	if($isviewed == 'true') {
		unlink($img2delete);
	}

Open in new window

PHP

Avatar of undefined
Last Comment
waygood

8/22/2022 - Mon
glcummins

Keep in mind that PHP is a server-side language, which means that all processing is run on the server, and the output is then send in HTML format to the browser to be displayed. The display does not happen until all processing is complete.

When your script attempts to display an image, the HTML code is generated and prepared to send to the browser, but the browser does not actually get any information until your script has run. By this time, unlink() has been called and the image is gone.

You will probably want to display the image via a different script than the one that deletes the image, if possible.
waygood

how about a script to server and delete the image?

<img src="serve.php?file=bob.gif"/>

serve.php
<?php
if (!function_exists('mime_content_type')) {
   function mime_content_type($f) {
       $f = escapeshellarg($f);
       return trim( `file -bi $f` );
   }
}
if (!function_exists('file_get_contents'))
{
    function file_get_contents($filename, $use_include_path = 0)
    {
        $file = @fopen($filename, 'rb', $use_include_path);
        if ($file)
        {
            if ($fsize = @filesize($filename))
            {
                $data = fread($file, $fsize);
            }
            else
            {
                while (!feof($file))
                {
                    $data .= fread($file, 1024);
                }
            }
            fclose($file);
        }
        return $data;
    }
}
$name=urldecode($_GET['file']);
if(file_exists($name))
{
      $header=mime_content_type($name);
      if(ereg("image",$header))
      {
            header('Content-type: '.mime_content_type($name));
            echo file_get_contents($name);
      }
      else
      {
            header('Content-type: image/jpeg');
      }
      unlink($name);
}
?>
ASKER CERTIFIED SOLUTION
danrwhit

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
hqnetwork

I used ob_flush/flush technique since it was pretty straightforward.  thanks guys for the responses!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
waygood

Your accepted answer will not work.
Your code deletes images before the browser requests them, as pointed out by glcummins.
danrwhit

waygood, I beg to differ.  I'm using Firefox with caching disabled, and tested out the code below.  Works fine.  Try it out.
<?php
 
echo "<img src='earth.jpg'>";
 
ob_flush();
flush();
 
sleep(3);
 
unlink('earth.jpg');
 
echo "<img src='earth.jpg'>";
 
?>

Open in new window

waygood

'sleep' was not part of the answer given.
This is very bad practice though. If the browser takes longer than 3 seconds to download the page and the image then your script will fail. This can possibly happen with slow PC's and slow connections like dialup or on mobile phones. It will also depend on the size of the image.
I strongly advise you to delete the image after you send it contents to the browser.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
danrwhit

waygood,

You're right.  That technique is unreliable based on your examples.  Perhaps if you're on a LAN this solution could 'get the job done'.  But for the www, I agree it is not ideal.

I tried your example, waygood, and it did not work for me.  I wonder if the solution to this should involve AJAX.  Then the client computer could tell the server once the page (image) has completely loaded and at that point the server can issue the unlink function.
waygood

you probably would have to hard code the directory into my script to reference where your images are. Keeping them in a seperate directory would prevent deletion of your code.

ie
serve.php in root directory of your website
images in gfx subdirectory from loction of serve.php

code adjusted to:-
$name='gfx/'.urldecode($_GET['file']);