Solved

Deleting file after use

Posted on 2008-06-14
3
212 Views
Last Modified: 2008-06-15
Experts,

I have a webpage with which I generate an xml file and then use this file to display statistics in a flash graph.

At the top of the script I write the xml file, and in the middle I show the flash graph pulls in the file to display statistics.

I'd like to delete this file at the bottom.

When I do this the flash graph doesnt display correctly....

I've added at the very bottom:

<?PHP
sleep(5);
fclose($filestream);
unlink($filename);
?>

The sleep function seems to run upon opening the page which I find strange.  I'd like to run it after everything has been displayed.

Please help.
0
Comment
Question by:mhouldridge
3 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 21786760
You need to remember how PHP works. The PHP script runs from top to bottom generating HTML. When the PHP script is complete the streamed output ends and Apache (or IIS) displays the HTML.

In between PHP ending and HTML displaying, your sleep() and unlink(..) execute, deleting the file. When the HTML displays, the file is gone.
0
 
LVL 3

Expert Comment

by:Xorlev
ID: 21787297
You can use the output buffer functions to capture the page contents, and flush it to the browser before sleeping it.

http://us3.php.net/manual/en/ref.outcontrol.php

I'd recommend though that instead you have a cronjob or use this very script to delete other old entries instead, it would be far more reliable. That way every time it's reloaded it would delete OTHER entries say more than a minute old, but not your current one.

This might work though if you decide not to do that:
<?php
// Stuff here
 
// Begin output buffering
ob_start();
?>
<html>
<stuff>
<here load_on_fly><?php include('some/deleted/file.xml'); ?></here>
</stuff>
</html>
<?php
// Send to browser
ob_end_flush();
 
// Wait...
sleep(5);
 
// Delete
unlink('/some/file');
?>

Open in new window

0
 
LVL 1

Accepted Solution

by:
webjema earned 500 total points
ID: 21787845
Put your xml files to separate directory and clean it when your script _starts_ (in top, not in bottom).
So, when page will be open file will be on his place and works correctly. But on next page that old file will be cleaned.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question