Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts

    creating, downloading then deleting a file

    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.

  • #2
    Regular Coder
    Join Date
    Sep 2002
    Location
    British Columbia
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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).
    Code:
    <form action=process.php method=POST>
    Filename: <input type="text" name="fname" value="joe.txt">
    <br>
    <textarea name="fdata" style="width: 400px; height: 300px;">
    Hello there joe.
    This is a test.
    </textarea>
    <br>
    <input type="submit" value="Go">
    </form>
    process.php :

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

    PHP Code:
    <?php
    unlink
    ($_SERVER["QUERY_STRING"]);
    ?>
    <script language="javascript">
    self.close();
    </script>

  • #4
    New to the CF scene
    Join Date
    Sep 2002
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    x_goose_x, does that program create a new file? Or does it only work with current files?

  • #5
    New to the CF scene
    Join Date
    Sep 2002
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


    Thanks.

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    New to the CF scene
    Join Date
    Sep 2002
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #9
    New to the CF scene
    Join Date
    Sep 2002
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't see how that could be. In the control panel I can create files fine.

  • #10
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    set the permissions in the directory the file is being created in to 777

  • #11
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Try this

    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>
    </form>

    PHP Code:

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

       if(!
    file_exists($nFile)){

          
    $newFile fopen($nFile,'w');
          
    fwrite($nFile,$cont);
          
    fclose($nFile);

         }

    if(
    file_exists($nFile)){
          echo 
    "<a href=\"newfile.txt\">Download File</a>";
    }



    CLOSE.php ...

    PHP Code:
    unlink($file); 

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •