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 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2005
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Trying to write to text file

    Greetings,

    I'm using Ubuntu Linux v5.10, Apache2, and PHP5.
    I am the only user of the computer.

    /etc/apache2/sites-available/default.txt is configured to use /var/www/testdir/ as the document root.
    I am simply trying to write to a text file located in /var/www/testdir/test.txt

    /var/www/testdir/ contains: test.txt and test.php

    Here is my PHP script:

    Code:
    <?php
    
      $testfile = "/var/www/testdir/test.txt";
      $filehandle = fopen($testfile, "w");
    
      $stringData = "Write this stuff to file\n";
      fwrite($filehandle, $stringData);
    
      $stringData = "Then write this stuff to file too\n";
      fwrite($filehandle, $stringData);
    
      fclose($filehandle);
    
    ?>
    Here is the error being returned after the PHP script is ran:

    Code:
    Warning: fopen(/var/www/testdir/test.txt) [function.fopen]: failed to open stream: Permission denied in /var/www/testdir/test.php on line 4
    
    Warning: fwrite(): supplied argument is not a valid stream resource in /var/www/testdir/test.php on line 7
    
    Warning: fwrite(): supplied argument is not a valid stream resource in /var/www/testdir/test.php on line 10
    
    Warning: fclose(): supplied argument is not a valid stream resource in /var/www/testdir/test.php on line 12
    I have a feeling this has to do with file permissions and unfortunately I'm a bit of a newbie to Linux. I have not changed any directory or file permissions so they should all be set to the default. I am the only user of this computer.

    Any suggestions on getting this to work would be very much appreciated.

    Thank you for your time,

    *Nick*

  • #2
    New Coder
    Join Date
    Apr 2006
    Location
    Pakistan
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello nkline!

    Right click on the folder testdir and select properties and then "check" read/write permissions for everyone.It should be 777 as in command line language.

    I'm not really a user of linux but it should help out as i had used ubuntu for 2 days and tested this kind of stuff.

    Regards,
    Muhammad Haris

  • #3
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,830
    Thanks
    21
    Thanked 157 Times in 148 Posts
    try this:

    PHP Code:
    <?php

      $testfile 
    "/var/www/testdir/test.txt";
       
    chmod($testfile0777);
      
    $filehandle fopen($testfile"w");

      
    $stringData "Write this stuff to file\n";
      
    fwrite($filehandle$stringData);

      
    $stringData "Then write this stuff to file too\n";
      
    fwrite($filehandle$stringData);

      
    fclose($filehandle);

    ?>
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #4
    New Coder
    Join Date
    Apr 2006
    Location
    Pakistan
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by chump2877
    try this:

    PHP Code:
    <?php

      $testfile 
    "/var/www/testdir/test.txt";
       
    chmod($testfile0777);
      
    $filehandle fopen($testfile"w");

      
    $stringData "Write this stuff to file\n";
      
    fwrite($filehandle$stringData);

      
    $stringData "Then write this stuff to file too\n";
      
    fwrite($filehandle$stringData);

      
    fclose($filehandle);

    ?>
    You do need to chmod the txt file but it is important to chmod the directory first!


  •  

    Posting Permissions

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