Search code examples
phpexecdir

How to create a folder above the root directory using php


my root dir id

/var/www/html/

i want to create directory at /var/www/ ie /var/www/myfolder

i'm executing create_dir.php from

/var/www/html/site/create_dir.php

when i execute this program it's unable to create folder.

in log i'm getting permission denide. update

This is my code

<?php
$os_type=php_uname('s');
$cur_file_path=$_SERVER['PHP_SELF'];
echo "File path:$cur_file_path<br>";
$scount=substr_count($cur_file_path, '/');
echo "/ count:$scount<br>"; 
$doc_root= $_SERVER['DOCUMENT_ROOT'] ;
echo "doc root:$doc_root<br>";
if($os_type=='Linux')
{
$ds=substr_count("$cur_file_path","/");//directory seperator
echo "Count of /=$ds<br>";
}
if($os_type=='Windows')
{
$ds=substr_count("$cur_file_path","'\'");//directory seperator
}
$path="../";
for($i=1;$i<$scount;$i++)
{

    $path.="../";
}

$dir="myfolder";
exec("mkdir ".$dir);
?>

how to solve this.


Solution

  • That is a security problem as it gives read and write access to the world. It may be that your apache user does not have read/write permissions on the directory.

    Here's what you do if $os_type=='Linux':

    1. Make sure all files are owned by the Apache group and user. In Linux it is the www-data group and user

    exec('chown -R www-data:www-data /path/to/webserver/www',$ouput,$result); 
    

    2. Next enabled all members of the www-data group to read and write files

    exec('chmod -R g+rw /path/to/webserver/www',$ouput,$result);
    

    The php mkdir() function should now work without returning errors. You can also see the error output in $output in case of error.

    There is the another way to create directory using ftp:

    You can try mkdir with ftp, mkdir works with stream wrappers, so it's ok to write mkdir('ftp://user:pass@server/mydir');

    OR

    If you have problems with the SAFE MODE Restriction in effect i.e. if you try to create and access to subdirectorys recursive you can use ftp-lib like this.

    <?php
    
    DEFINE ('FTP_USER','yourUser');
    DEFINE ('FTP_PASS','yourPassword');
    
    /**
    * Returns the created directory or false.
    *
    * @param Directory to create (String)
    * @return Created directory or false;
    */
    
    function mkDirFix ($path) {
    
    
            $path = explode("/",$path);
            $conn_id = @ftp_connect("localhost");
            if(!$conn_id) {
                return false;
            }
            if (@ftp_login($conn_id, FTP_USER, FTP_PASS)) {
    
                foreach ($path as $dir) {
                    if(!$dir) {
                        continue;
                    }
                    $currPath.="/".trim($dir);
                    if(!@ftp_chdir($conn_id,$currPath)) {
                        if(!@ftp_mkdir($conn_id,$currPath)) {
                            @ftp_close($conn_id);
                            return false;
                        }
                        @ftp_chmod($conn_id,0777,$currPath);
                    }
                }
            }
            @ftp_close($conn_id);
            return $currPath;
    
    }
    ?>
    

    Maybe it helps.