Solved

Deleting file after use

Posted on 2008-06-14
3
210 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now