dcsimg
www.webdeveloper.com
Results 1 to 13 of 13

Thread: php mkdir not working

  1. #1
    Join Date
    Sep 2014
    Posts
    9

    php mkdir not working

    Hi all,

    Am trying to write a php script to create a directory but am getting the following error

    Warning: mkdir() [function.mkdir]: Permission denied in /var/www/html/Validate.php on line 23

    I know this is some problem with apache user not having the permissions to run the commands in /bin like ls or mkdir or cp. But how do i make it work ?

    How do i write a php script to create and copy files on a linux server running apache as web server ??

    I added the below lines to /etc/sudoers but even this did not solve the problem


    apache ALL=(ALL) NOPASSWD: /home/kan/
    php ALL=(ALL) NOPASSWD: /bin/cp
    apache ALL=(ALL) NOPASSWD: /bin/cp
    php ALL=(ALL) NOPASSWD: /bin/mkdir
    apache ALL=(ALL) NOPASSWD: /bin/mkdir

    Can someone please suggest how to do this ?

    Regards,
    kan

  2. #2
    Join Date
    Mar 2012
    Posts
    1,834
    The only way I know how to do it is with an FTP script, not PHP. Well, I say "know", but that should really be "knew" 'cos I haven't used raw FTP in years!

  3. #3
    Join Date
    Sep 2014
    Posts
    9
    I didn't convey the info properly i think.

    Apache and PHP script are on the same Linux server where i want to create a file and copy that file to some other directory
    1. HTML form at front end
    2. Validate the form at the back end
    3. After validation, say, i have to create a directory in /var/www/html/TEST and copy some files from /home/user2/file1 to /var/www/html/TEST/

    Regards,
    kan

  4. #4
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    891
    Can you show us the php code? Are you absolutely sure you have specified a valid path to be created? If you specify your folder name incorrectly and there simply isn't a way to make that folder where you say to put it, it's going to fail. For ex. if you have a tree like:

    /home/name/public_html/folders

    and you specify

    mkdir('/home/name/public_folder/newfolder/newfolder2')

    I believe that this will fail if you dont' add the correct option.

  5. #5
    Join Date
    Sep 2014
    Posts
    9
    Below is the snippet of the code

    mkdir("/home/kan2/abcd", 0755);
    $file1 = '/home/kan/Sample.cnf';
    $file2 = '/home/kan2/abcd/Sample.cnf';

    if (!copy($file1, $file2)) {
    echo "failed to copy $file1...\n";
    }

    Below is the error_log from /var/log/httpd/

    [Tue Sep 30 12:08:15 2014] [error] [client 135.x.x.x] PHP Warning: mkdir() [<a href='function.mkdir'>function.mkdir</a>]: Permission denied in /var/www/html/Validate.php on line 63, referer: http://<IP of the server>/FrontEnd.php

    [Tue Sep 30 12:08:15 2014] [error] [client 135.x.x.x] PHP Warning: copy(/home/kan/FBSR-sample.csr.openssl.cnf) [<a href='function.copy'>function.copy</a>]: failed to open stream: Permission denied in /var/www/html/Validate.php on line 67, referer: http://135.x.x.x/FrontEnd.php

    Regards,
    kan

  6. #6
    Join Date
    Sep 2014
    Posts
    9
    Also the directory permission of kan folder is 755 and owner i changed from kan to apache

  7. #7
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    891
    I don't know about that permission change you made. Why would you change ownership to apache when you are running it using php?

    Anyway - looking at your code and your messages - the message doesn't match the code you gave us. The copy(....) command references a file that can't be opened, but that name is not in your code.

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,636
    If the script is being executed via Apache (e.g. a web request), then the apache user (or "nobody", or whoever the Apache user is) will need to be able to write on the directory that the new directory is to be added to, and will need at least read/execute on all parent directories.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Sep 2014
    Posts
    9
    Okie here is my complete process.

    I needed a front end User interface which takes an input from user validates that input and if that input is correct run a shell script on a linux server (this shell script creats a directory, copies files from a location and also lists the contents of the directory which is created)

    So, this is how i started to implement

    1. On a linux server installed LAMP
    2. Created a php/html file Test.php (just a simple form with text area and radio button and submit button) in the /var/www/html directory on the same linux server where i installed LAMP
    3. Another Validate.php script after pressing the submit button on the Test.php it comes here

    All the above steps are done and fine
    I am stuck in the next step, where
    4. I have to run the shell script on the linux server which is located at /var/www/html itself and post the output of the scirpt on the html page

    Am not able to get pass the last step where the /var/log/httpd/error_log says permission denied while executing mkdir or ls or copy

    Any ideas on alternate implementation ??? !!

    Regards,
    kan

  10. #10
    Join Date
    Dec 2013
    Location
    Chandigarh
    Posts
    60
    Dear,

    Either it is ftp permission error, or you are not follows right path.

    Use this code :


    <?php
    mkdir("/path/to/my/dir", 0700);
    ?>


    it may help you.

    regards

  11. #11
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    891
    I asked you a question back in #7 - you didn't respond to it.

  12. #12
    Join Date
    Sep 2014
    Posts
    9
    Hi ginerjm,

    Sorry for the delayed reply. That was an editing mistake.

    @Avrillavigne
    even with mkdir("/path/to/my/dir", 0700); same error in error_log

    I think its something to do with selinux. In the /etc/selinux/config i have disabled it. But still when i run getenforce command it says permissive. !!!

    Regards,
    kan

  13. #13
    Join Date
    Dec 2013
    Location
    Chandigarh
    Posts
    60
    Quote Originally Posted by kan303 View Post
    Hi ginerjm,

    Sorry for the delayed reply. That was an editing mistake.

    @Avrillavigne
    even with mkdir("/path/to/my/dir", 0700); same error in error_log

    I think its something to do with selinux. In the /etc/selinux/config i have disabled it. But still when i run getenforce command it says permissive. !!!

    Regards,
    kan
    Have you checked your file permission.
    and which ftp do you have? is it master ftp with all permission. or working on client project.

    many times client creates ftp with limited permission.
    Please check and let me know. because mathod which you have done is right.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles