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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with creating files in PHP

    I'm a first-time PHP user, and I'm trying to use the fopen() function in order to create a file so that I can write to it. I believe I've used the function correctly, but for some reason, it keeps telling me that it can't open the file. I can't figure the problem out. Can anyone help?

    My code is below:

    <?php

    $ourFileName = "testFile.txt";
    $fh = fopen($ourFileName, "a") or die("Can't open $ourFileName");
    fclose($fh);

    ?>

    When I try to load the webpage, I get the error message "Can't open testFile.txt". So clearly it's dying. What am I doing wrong? I should mention that I'm using a Linux server with PHP version 5.2.6. Any help would be much appreciated.

    Thanks!

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,131
    Thanks
    2
    Thanked 328 Times in 320 Posts
    Get php to tell you why the function failed by turning on full php error reporting. Add the following two lines after your first opening <?php tag -

    PHP Code:
    ini_set ("display_errors""1");
    error_reporting(E_ALL); 
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    File permissions may be disabling you're ability to create new files. Enable you're error reporting: Error_reporting(E_ALL) and possibly ini_set('display_errors', 1); at the top of you're script. It will issue an actual warning telling you what the problem is.
    If safe mode is enabled you'll need to chown the directory to apache so it has the ability to write files to it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    New to the CF scene
    Join Date
    Jul 2008
    Location
    Canada
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I my self am new with php but I have been writing c for a long time and using Linux for years. So I hope this helps!

    This may seem a bit trivial but what are the current privileges on that file? If you do a
    ( ls -l ) or ( ll ) while in the same directory check that you have sufficient access. If your not concerned security on that file do ( chmod 777 testFile.txt) This might be your problem.

    Good luck I will check back to see if that did it for you.

  • #5
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you all very much! You were all right, turning on full php error reporting revealed that I did not have sufficient file privileges. I fixed that by creating the file beforehand and doing a chmod 777 on it, and I was then able to open and write to the file. But that was a previously created file. My question now is how to create a file from scratch using php without giving everyone rw access to my directory. I guess this is the chown part.. how do I do that?

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I would use a new directory and get PHP to create it with the mkdir command. Set it with the mode of 0660 for its permissions, and I believe that mkdir defaults the chown to Apache or whatever the name is for the user running under apache. Note with 660 permissions, you will lose the ability to navigate this directory on you're own. I don't have my linux box working yet so I can't actually test this out for you right now, sorry.
    Chown information can be found here. Another option is to use ssh or ftp to create a new directory and apply the permissions 0666 (r/w for user, group and everyone) since PHP will fall into everyone.
    So long as you do it on the containing directory, PHP will attempt to make the file and if they own the directory and have a bit 4 permissions they will succeed.
    And finally, I don't recommend the use of 7 for any of you're permissions on web environments. 7 includes the execute privilege (1) and if you allow users to upload files of their own, creating a sh/perl/php/etc file would be a piece of cake to do. Now you have a file that can be directly executed by any use who attaches to the file. Bad.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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