View Full Version : creating, downloading then deleting a file

02-22-2003, 04:35 AM
Here's what I want to do:

1 - create a text file
2 - offer it as a download
3a - if it's downloaded, delete it from the server on completion
3b - if the user declines the download, delete it straight away

1 and 2 I can do; actually deleting a file I can do; but the logic of 3 I have no idea about.

Dylan Leblanc
02-22-2003, 06:55 AM
What is the source of the text file?

You don't even have to write it to disk. You could send the proper HTTP headers telling the users browser you are sending plain text (and that it should be saved and not opened), and then send the text. No need to write it to disk at all.

02-22-2003, 05:58 PM
Here's a script I wrote that you might find usefull. You enter the filename and the text in the textarea is written to that file.

first file, name doesn't matter (can be .php or .htm).

<form action=process.php method=POST>
Filename: <input type="text" name="fname" value="joe.txt">
<textarea name="fdata" style="width: 400px; height: 300px;">
Hello there joe.
This is a test.
<input type="submit" value="Go">

process.php :

$fname = $_POST["fname"];
$fdata = stripslashes(str_replace("+", "\+", $_POST["fdata"]));
$fp = fopen($fname, "w");
fwrite($fp, $fdata);
<body onUnload="window.open('close.php?<?=$fname ?>')">
<a href="<?=$fname ?>"><?=$fname ?></a><br>
Right-click and select "Save target as..."

close.php :

<script language="javascript">

02-22-2003, 11:58 PM
x_goose_x, does that program create a new file? Or does it only work with current files?

02-23-2003, 01:01 AM
Nevermind about that, but can someone show me the needed code that would allow this to create a new file(using the 'Filename:' field in the form) then write what is submitted to it? This would be instead of opening a file to write to.


02-23-2003, 02:29 AM
The script I put up does create a new file. I just put any random filename in the box. If you don't want the file to be deleted, remove:

onUnload="window.open('close.php?<?=$fname ?>')"

from the process.php file and remove the file close.php

02-23-2003, 02:46 AM
Hmmm.. This is what I get when I try it on a file that isn't already created:

Warning: fopen(joe.txt) [function.fopen]: failed to create stream: Permission denied in /home/yourdomain/public_html/process.php on line 4

Warning: fwrite(): supplied argument is not a valid stream resource in /home/yourdomain/public_html/process.php on line 5

Warning: fclose(): supplied argument is not a valid stream resource in /home/yourdomain/public_html/process.php on line 6

But when I try it with a file already created and writable(chmoded), it writes to the file fine.

02-23-2003, 03:12 AM
Don't know what to say. Works for me. Syntax double checked in my book.
What's with the "Permission denied"? Maybe you're not aloud to create file?

02-23-2003, 06:52 AM
I don't see how that could be. In the control panel I can create files fine.

02-23-2003, 01:51 PM
set the permissions in the directory the file is being created in to 777

02-24-2003, 09:05 PM
This approach is a bit longer than the original, but...

<body onunload="window.location='close.php?file=<?php echo $nFile ?>'">

<form name="f" action="<?php echo $PHP_SELF ?>" method="post">
<input name="attempted" value="yes" type="hidden" />
<textarea name="cont"><?php echo $cont ?></textarea>

if($attempted == 'yes'){
$nFile = "newfile.txt";


$newFile = fopen($nFile,'w');


echo "<a href=\"newfile.txt\">Download File</a>";


CLOSE.php ...


I just wrote this so It may not work exactly right.