+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: what permissions do I need to use php to write to file?

  1. #1
    diabolo's Avatar
    diabolo is offline Community Advocate diabolo is on a distinguished road
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,683

    what permissions do I need to use php to write to file?

    Code:
    Warning:  fopen(*****) [function.fopen]: failed to open stream: Permission denied in C:\xampp\htdocs\mcs\sources\functions\gallery.php on line 48
    can't open file
    PHP Code:
    function createGalleryDirectory($name) {
       if(!
    is_dir($name)) {
          
    $path ROOT.'gallery/'.$name;
          
    mkdir($path0777);
       }


  2. #2
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: what permissions do I need to use php to write to file?

    755 works fine with most files. The directory and the file must both have permissions set to allow local access. There is nothing wrong with the code you posted.

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: what permissions do I need to use php to write to file?

    Remember that Windows doesn't use Unix style permissions. If you read the PHP.net documentation for mkdir, you'll see that the $mode argument is ignored. Make sure whichever user the Apache process runs as has write access to ROOT . 'gallery/'. You can use procexp to figure out what user httpd runs as. If you run WinXP home, use Reinhard Tchorz's security tab patch to enable the security tab.

    What's the definition for ROOT? If it's missing a trailing path separator, that will probably cause the permission denied error. Edit: the reason being the script will try to create a directory within the wrong parent directory, which may have too restrictive permissions.

    Lastly, use 'file_exists()' rather than 'is_dir' as the former will return true for existing directories while the latter will return false if $path is a file that's not a directory.
    Last edited by misson; 07-25-2009 at 12:18 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  4. #4
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: what permissions do I need to use php to write to file?

    is_dir seem appropriate since he is trying to create a directory, not a file, so it is natuaral he should try to ignore them. Its more concise. Anyway, misson brought up a good point. What operating system are you using?
    Last edited by Twinkie; 07-25-2009 at 03:42 PM.

  5. #5
    diabolo's Avatar
    diabolo is offline Community Advocate diabolo is on a distinguished road
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,683

    Re: what permissions do I need to use php to write to file?

    right now it is running off xampp on my windows xp

    so if I upload it to x10 server's which has linux, it should work?

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: what permissions do I need to use php to write to file?

    Quote Originally Posted by Twinkie View Post
    is_dir seem appropriate since he is trying to create a directory, not a file, so it is natuaral he should try to ignore them. Its more concise. Anyway, misson brought up a good point. What operating system are you using?
    Except that if he tries to create a directory with the same name as a non-directory file, is_dir() will return false and the mkdir will fail. Directories are a special kind of file, as are symbolic links and junction points; file_exists is very appropriate.

    Quote Originally Posted by diabolo View Post
    so if I upload it to x10 server's which has linux, it should work?
    Maybe. Permissions will always be an issue. They just need to be handled a little differently depending on the OS. First check that the directory is being created where you thing it's being created (print $path), then check the permissions on the directories in the path to see whether or not the web server user has write access.

    Do you have an answer for my question about ROOT?
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  7. #7
    diabolo's Avatar
    diabolo is offline Community Advocate diabolo is on a distinguished road
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,683

    Re: what permissions do I need to use php to write to file?

    Quote Originally Posted by misson View Post
    Do you have an answer for my question about ROOT?
    o sorry, i was at a another computer and just scanned the post.
    but I know the root is defined and working because it can make the directory, it just can not write a file to it.
    Edit:
    actually it was because I had an error in my path for the fopen function. but now I have a different problem

    PHP Code:
       $dir_handle = @opendir($url) or die("Unable to open $url");
       
    $count "0";
       while (
    $file readdir($dir_handle)) {
          if (!
    is_dir($url.'/'.$file) && ($file="*.jpg" || $file="*.gif" || $file="*.png") && $file!="picture0.*") {
             
    $galleryEventFile[$count] = $file;
             
    $count++;
          }
       }
       
    closedir($dir_handle); 
    $count is not being changed.
    i believe it has to do something with this line, but im not sure
    PHP Code:
    if (!is_dir($url.'/'.$file) && ($file="*.jpg" || $file="*.gif" || $file="*.png") && $file!="picture0.*"
    Last edited by diabolo; 07-26-2009 at 04:53 PM. Reason: Automerged Doublepost

  8. #8
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: what permissions do I need to use php to write to file?

    You need to use glob to expand file patterns or (e.g.) substr to extract the file extension. $file="*.jpg" is just a string comparison; '*' is not magical in any way.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  9. #9
    diabolo's Avatar
    diabolo is offline Community Advocate diabolo is on a distinguished road
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,683

    Re: what permissions do I need to use php to write to file?

    sorry i should have updated the script once I realized it was wrong.
    PHP Code:
       $dir_handle = @opendir($url) or die("Unable to open $url");
       
    $count 0;
       while (
    $file readdir($dir_handle)) {
          
    $extension pathinfo($filePATHINFO_EXTENSION);
          
    $allowedExtensions = array('jpg''png''gif');
          if (!
    is_dir($url.'/'.$file) && in_array(strtolower($pathinfo), $allowedExtensions) && !preg_match('/picture0./i'$file)) {
             
    $galleryEventFile[$count] = $file;
             
    $count++;
          }
       }
       
    closedir($dir_handle); 

  10. #10
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: what permissions do I need to use php to write to file?

    Are you sure according to your debug information that it is entering the if statement appropriately? I don't see anything wrong with that, but I can't tell if it is executing properly. I need some additional info.

    I use a function that writes to a file when ever is is called with some info about its surrounding variables (if there is a better way to do this, I am open to ideas). If you want it, I could post it for you.

    Also on line 6, misson is right about the file_exists in that small possibility (damn him he is always right).
    Last edited by Twinkie; 07-27-2009 at 01:21 AM.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. [PHP] Basics of PHP Scripting
    By jeeter in forum Tutorials
    Replies: 21
    Last Post: 02-03-2010, 04:29 PM
  2. Replies: 2
    Last Post: 08-17-2008, 08:30 PM
  3. User permissions and PHP security
    By YppBay in forum Computers & Technology
    Replies: 7
    Last Post: 12-09-2007, 10:31 PM
  4. Converting a .RMVB file
    By SEŅOR in forum Off Topic
    Replies: 7
    Last Post: 06-19-2006, 02:20 PM
  5. PHP Cannot Write.. "not writeable"
    By eshcorp in forum Free Hosting
    Replies: 6
    Last Post: 10-15-2005, 10:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers