Solved

Deleting file after use

Posted on 2008-06-14
3
216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

626 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