Question about File Creation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kudzugazette
    New Member
    • Jul 2008
    • 1

    Question about File Creation

    So I would like it so that a user can input something into a textbox, type a name for the file, and the file is saved in a subdirectory marked content. For example, if I picked the name of the file to be test.txt, I want the file to be saved in content/test.txt. I can't figure out how to save the file in that directory without putting the script in that directory.

    What I have:
    [PHP]$filename = $_POST['title'];
    $text = $_POST['content'];

    $fp = fopen ($filename, "w");
    if ($fp) {
    fwrite ($fp, $text);
    fclose ($fp);
    echo ("Content Added.");
    }
    else {
    echo ("Error.");
    }[/PHP]
  • nashruddin
    New Member
    • Jun 2008
    • 25

    #2
    Why not add the path to the filename? suppose the directory is /var/www/upload/content, you can add the path when creating the file.

    Code:
    <?php
    $filename = '/var/www/upload/content/' . $_POST['title'];
    $fp = fopen ($filename, "w");
    
    /* ...the rest of your code... */
    
    ?>

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      You should also be able to use a relative path.
      Meaning that if your script is in /var/www/html/ and you want your content in
      /var/www/html/content/, you could simply prefix your file name with content/.

      Comment

      Working...