Solved

Deleting file after use

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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 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