Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Deleting file after use

Posted on 2008-06-14
3
Medium Priority
?
218 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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 count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

719 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