[function.mkdir]: Permission denied warning

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
harshilshah
Forum Newbie
Posts: 16
Joined: Fri Jul 17, 2009 5:13 am

[function.mkdir]: Permission denied warning

Post by harshilshah »

here's the code i tested in a windows WAMP server and it was working fine without any warnings or errors. But when i hosted my site on a live online unix server i got a couple of errors.
here's the code

Code: Select all

<?php
@session_start();
 
set_include_path("." . PATH_SEPARATOR . get_include_path());
 require 'ArchiveExtractor.class.php';
  
 // database code
$dbh=mysql_connect ("localhost", "root",
 "") or die('Cannot connect to the database because: ' . mysql_error());
  mysql_select_db ("wwwgodn_godnels");
  //echo "Connection Successfull";
  
  $sql="SELECT fldUsername FROM tblusers where fldUsername='".$_POST["txtusername"]."'";
  $result = mysql_query($sql) or die("Query to get blah failed with error: ".mysql_error());
  //echo"query";
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
include "adderror.html";
  exit(1);}
   
 mysql_free_result($result);
  
  $rs = @mkdir( $_POST["txtusername"], 0777 );
  //echo "make directory sucessfull";
  
 if ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")
 || ($_FILES["file"]["type"] == "application/x-rar-compressed")|| ($_FILES["file"]["type"] == "application/rar")
  || ($_FILES["file"]["type"] == "application/octet-stream")|| ($_FILES["file"]["type"] == "application/zip")
  ||($_FILES["file"]["type"] == "application/x-zip-compressed")||($_FILES["file"]["type"] == "application/octet-stream")
  ||($_FILES["file"]["type"] == "application/x-compress")||($_FILES["file"]["type"] == "application/x-compressed")
  ||($_FILES["file"]["type"] == "multipart/x-zip")|| ($_FILES["file"]["type"] == "image/pjpeg"))
  && ($_FILES["file"]["size"] < 2000000))
    {
    if ($_FILES["file"]["error"] > 0)
     {
      echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
    else
      {
      //echo "Upload: " . $_FILES["file"]["name"] . "<br />";
      //echo "Type: " . $_FILES["file"]["type"] . "<br />";
     //echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
     //echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
   
      if (file_exists($_REQUEST["txtusername"]. "/" . $_FILES["file"]["name"]))
        {
        echo $_FILES["file"]["name"] . " already exists. ";
         }
       else
        {move_uploaded_file($_FILES["file"]["tmp_name"],
        $_REQUEST["txtusername"]."/" . $_FILES["file"]["name"]);
        //echo "Stored in: " . $_REQUEST["txtusername"]."/" . $_FILES["file"]["name"];
         }
       }
    }
   else
     {
     echo "Invalid file";
     }
    
    if ((($_FILES["file"]["type"] == "application/zip")
  || ($_FILES["file"]["type"] == "application/rar")
  || ($_FILES["file"]["type"] == "application/zip")
  ||($_FILES["file"]["type"] == "application/x-zip-compressed")
 ||($_FILES["file"]["type"] == "application/octet-stream")
  ||($_FILES["file"]["type"] == "application/x-compress")
  ||($_FILES["file"]["type"] == "application/x-compressed")
  ||($_FILES["file"]["type"] == "multipart/x-zip")
  || ($_FILES["file"]["type"] == "application/octet-stream")))
  {
       $archExtractor=new ArchiveExtractor();
       $extractedFileList=$archExtractor->extractArchive($_REQUEST["txtusername"]."/" . $_FILES["file"]["name"],$_REQUEST["txtusername"]."/");
       chmod($_POST["txtusername"]."/" . $_FILES["file"]["name"],0777);
       unlink($_POST["txtusername"]."/" . $_FILES["file"]["name"]);
       echo "<h4 align='center'>album submitted</h4>";
      
   }
  $sql="Insert Into tblusers (fldUsername,fldPassword) Values ('".$_POST["txtusername"]."','".$_POST["txtpassword"]."')";
    
  $result = mysql_query($sql) or die("Query failed with error: ".mysql_error());
   mysql_close($dbh);
 
 
include "list1.php";
 
?>
here's the list fo errors
Warning: mkdir(hhh111122) [function.mkdir]: Permission denied in /home/wwwgodn/public_html/old/imagealbum/add.php

Warning: move_uploaded_file(hhh111122/hhhh.zip) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/wwwgodn/public_html/old/imagealbum/add.php

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php5cIcOL' to 'hhh111122/hhhh.zip' in /home/wwwgodn/public_html/old/imagealbum/add.php

Warning: chmod() [function.chmod]: No such file or directory in /home/wwwgodn/public_html/old/imagealbum/add.php

Warning: unlink(hhh111122/hhhh.zip) [function.unlink]: No such file or directory in /home/wwwgodn/public_html/old/imagealbum/add.php on line 219


Please some help me its really urgent.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [function.mkdir]: Permission denied warning

Post by VladSun »

Please, stop double post
viewtopic.php?f=1&t=103880

1. Use absollute paths! (e.g. /home/wwwgodn/public_html/old/imagealbum/images)
2. Change the permissions of the directory which will contain all $_REQUEST["txtusername"] directories (e.g. /home/wwwgodn/public_html/old/imagealbum/images) to 0777
There are 10 types of people in this world, those who understand binary and those who don't
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: [function.mkdir]: Permission denied warning

Post by cpetercarter »

I guess this is a permissions issue. The programme is probably trying to make a new sub-directory in a place/directory where it does not have write permissions. You can test this by using the php "is_writeable()" function. Don't forget that your php programme is not "you", it is the Apache web server. The directory to which you want Apache to write either has to have "0777" permissions (which has some security risks), or it needs to be "owned" by Apache and not by "you". (There are alternatives which involve running php as cgi, but only your server administrator can set this up).
harshilshah
Forum Newbie
Posts: 16
Joined: Fri Jul 17, 2009 5:13 am

Re: [function.mkdir]: Permission denied warning

Post by harshilshah »

VladSun wrote:Please, stop double post
viewtopic.php?f=1&t=103880

1. Use absollute paths! (e.g. /home/wwwgodn/public_html/old/imagealbum/images)
2. Change the permissions of the directory which will contain all $_REQUEST["txtusername"] directories (e.g. /home/wwwgodn/public_html/old/imagealbum/images) to 0777
how do i change the permission of the directory can i do it using the ftp or what cdoe should i use in php to do it.

and where should i use absolute path( i dont get it)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [function.mkdir]: Permission denied warning

Post by VladSun »

harshilshah wrote:how do i change the permission of the directory can i do it using the ftp or what cdoe should i use in php to do it.
FTP is OK.
harshilshah wrote:and where should i use absolute path( i dont get it)
Everywhere you use $_REQUEST["txtusername"] ...
There are 10 types of people in this world, those who understand binary and those who don't
harshilshah
Forum Newbie
Posts: 16
Joined: Fri Jul 17, 2009 5:13 am

Re: [function.mkdir]: Permission denied warning

Post by harshilshah »

FTP is OK
.

well using ftp i changed the rights of the folder in which i am creating the new directory
to 777.
but now the problem is that the new directory created has permission set 755 whereas in code i mentioned it as

Code: Select all

// code used in upload.php is follows

Code: Select all

 
<?php
@session_start();
 
set_include_path("." . PATH_SEPARATOR . get_include_path());
 require 'ArchiveExtractor.class.php';
  
 // database code
$dbh=mysql_connect ("localhost", "root",
 "") or die('Cannot connect to the database because: ' . mysql_error());
  mysql_select_db ("wwwgodn_godnels");
  //echo "Connection Successfull";
  
  $sql="SELECT fldUsername FROM tblusers where fldUsername='".$_POST["txtusername"]."'";
  $result = mysql_query($sql) or die("Query to get blah failed with error: ".mysql_error());
  //echo"query";
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
include "adderror.html";
  exit(1);}
   
 mysql_free_result($result);
  
  mkdir( $_REQUEST["txtusername"], 0777 );
  //echo "make directory sucessfull";
 
  
 if ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")
 || ($_FILES["file"]["type"] == "application/x-rar-compressed")|| ($_FILES["file"]["type"] == "application/rar")
  || ($_FILES["file"]["type"] == "application/octet-stream")|| ($_FILES["file"]["type"] == "application/zip")
  ||($_FILES["file"]["type"] == "application/x-zip-compressed")||($_FILES["file"]["type"] == "application/octet-stream")
  ||($_FILES["file"]["type"] == "application/x-compress")||($_FILES["file"]["type"] == "application/x-compressed")
  ||($_FILES["file"]["type"] == "multipart/x-zip")|| ($_FILES["file"]["type"] == "image/pjpeg"))
  && ($_FILES["file"]["size"] < 2000000))
    {
    if ($_FILES["file"]["error"] > 0)
     {
      echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
    else
      {
      //echo "Upload: " . $_FILES["file"]["name"] . "<br />";
      //echo "Type: " . $_FILES["file"]["type"] . "<br />";
     //echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
     //echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
   
      if (file_exists($_REQUEST["txtusername"]. "/" . $_FILES["file"]["name"]))
        {
        echo $_FILES["file"]["name"] . " already exists. ";
         }
       else
        { move_uploaded_file($_FILES["file"]["tmp_name"],
        $_REQUEST["txtusername"]."/" . $_FILES["file"]["name"]);
        //echo "Stored in: " . $_REQUEST["txtusername"]."/" . $_FILES["file"]["name"];
         }
       }
    }
   else
     {
     echo "Invalid file";
     }
    
    if ((($_FILES["file"]["type"] == "application/zip")
  || ($_FILES["file"]["type"] == "application/rar")
  || ($_FILES["file"]["type"] == "application/zip")
  ||($_FILES["file"]["type"] == "application/x-zip-compressed")
  ||($_FILES["file"]["type"] == "application/octet-stream")
  ||($_FILES["file"]["type"] == "application/x-compress")
  ||($_FILES["file"]["type"] == "application/x-compressed")
  ||($_FILES["file"]["type"] == "multipart/x-zip")
  || ($_FILES["file"]["type"] == "application/octet-stream")))
  {
       $archExtractor=new ArchiveExtractor();
       $extractedFileList=$archExtractor->extractArchive($_REQUEST["txtusername"]."/" . $_FILES["file"]["name"],$_REQUEST["txtusername"]."/");
       chmod($_POST["txtusername"]."/" . $_FILES["file"]["name"],0777);
       unlink($_POST["txtusername"]."/" . $_FILES["file"]["name"]);
       echo "<h4 align='center'> album submitted </h4><br><br>";
      
   }
  $sql="Insert Into tblusers (fldUsername,fldPassword) Values ('".$_POST["txtusername"]."','".$_POST["txtpassword"]."')";
    
  $result = mysql_query($sql) or die("Query failed with error: ".mysql_error());
   mysql_close($dbh);
 
 
include "list1.php";
 
?>
and this code works fine but when in another file which is for updating this directory.
i get the permission denied error.
here's the code

Code: Select all

<?php
@session_start();
 
set_include_path("." . PATH_SEPARATOR . get_include_path());
require 'ArchiveExtractor.class.php';
 
 if ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")
 || ($_FILES["file"]["type"] == "application/x-rar-compressed")|| ($_FILES["file"]["type"] == "application/rar")
  || ($_FILES["file"]["type"] == "application/octet-stream")|| ($_FILES["file"]["type"] == "application/zip")
  ||($_FILES["file"]["type"] == "application/x-zip-compressed")||($_FILES["file"]["type"] == "application/octet-stream")
  ||($_FILES["file"]["type"] == "application/x-compress")||($_FILES["file"]["type"] == "application/x-compressed")
  ||($_FILES["file"]["type"] == "multipart/x-zip")|| ($_FILES["file"]["type"] == "image/pjpeg"))
  && ($_FILES["file"]["size"] < 2000000))
    {
    if ($_FILES["file"]["error"] > 0)
     {
      echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
    else
      {
      //echo "Upload: " . $_FILES["file"]["name"] . "<br />";
      //echo "Type: " . $_FILES["file"]["type"] . "<br />";
     //echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
     //echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
   
      if (file_exists($_REQUEST["txtusername"]. "/" . $_FILES["file"]["name"]))
        {
        echo $_FILES["file"]["name"] . " already exists. ";
         }
       else
        {move_uploaded_file($_FILES["file"]["tmp_name"],
        $_POST["txtusername"]."/" . $_FILES["file"]["name"]);
        //echo "Stored in: " . $_REQUEST["txtusername"]."/" . $_FILES["file"]["name"];
         }
       }
    }
   else
     {
     echo "Invalid file";
     }
    
    if ((($_FILES["file"]["type"] == "application/zip")
  || ($_FILES["file"]["type"] == "application/rar")
  || ($_FILES["file"]["type"] == "application/zip")
  ||($_FILES["file"]["type"] == "application/x-zip-compressed")
 ||($_FILES["file"]["type"] == "application/octet-stream")
  ||($_FILES["file"]["type"] == "application/x-compress")
  ||($_FILES["file"]["type"] == "application/x-compressed")
  ||($_FILES["file"]["type"] == "multipart/x-zip")
  || ($_FILES["file"]["type"] == "application/octet-stream")))
  {
       $archExtractor=new ArchiveExtractor();
       $extractedFileList=$archExtractor->extractArchive($_REQUEST["txtusername"]."/" . $_FILES["file"]["name"],$_REQUEST["txtusername"]."/");
       chmod($_POST["txtusername"]."/" . $_FILES["file"]["name"],0777);
       unlink($_POST["txtusername"]."/" . $_FILES["file"]["name"]);
       echo "<h4 align='center'>album updated</h4>";
      
   }
 
include "list1.php";
 
?>
which is where i get the error as

Warning: move_uploaded_file(wedding/abcd.zip) [function.move-uploaded-file]: failed to open stream: Permission denied in /home/wwwgodn/public_html/old/imagealbum/update.php on line 182

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpCmLotY' to 'wedding/abcd.zip' in /home/wwwgodn/public_html/old/imagealbum/update.php on line 182

Warning: chmod() [function.chmod]: No such file or directory in /home/wwwgodn/public_html/old/imagealbum/update.php on line 204

Warning: unlink(wedding/abcd.zip) [function.unlink]: No such file or directory in /home/wwwgodn/public_html/old/imagealbum/update.php on line 205


i agian went to ftp and checked the permissions of the new directory created and it was set to 755 and when tried to to set it 777. it wasn't allowing me to do so
i dont understand why the permisssions are set to 755 whereas i have specified them as 777 in the mkdir function.
Last edited by harshilshah on Thu Jul 30, 2009 6:10 am, edited 2 times in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [function.mkdir]: Permission denied warning

Post by VladSun »

1. You don't have a call to mkdir() in your last script, so it's impossible to have this exact error generated by it.
2. You don't need to se 0777 permissions on directories created by the Apache user (i.e. created by using PHP), 0755 is enough.

Please, edit your posts and change [ code ] BBcode tags to [ php ] BBcode tags (without spaces)
There are 10 types of people in this world, those who understand binary and those who don't
harshilshah
Forum Newbie
Posts: 16
Joined: Fri Jul 17, 2009 5:13 am

Re: [function.mkdir]: Permission denied warning

Post by harshilshah »

but i am still getting the error
i tried to check the file permissions as well.
but i am not able to understand what is wrong with my update.php code
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [function.mkdir]: Permission denied warning

Post by VladSun »

VladSun wrote:1. You don't have a call to mkdir() in your last script, so it's impossible to have this exact error generated by it.
Maybe you call the wrong PHP script, because there is NO mkdir(...) in the update.php script you've posted.
There are 10 types of people in this world, those who understand binary and those who don't
harshilshah
Forum Newbie
Posts: 16
Joined: Fri Jul 17, 2009 5:13 am

Re: [function.mkdir]: Permission denied warning

Post by harshilshah »

VladSun wrote:
VladSun wrote:1. You don't have a call to mkdir() in your last script, so it's impossible to have this exact error generated by it.
Maybe you call the wrong PHP script, because there is NO mkdir(...) in the update.php script you've posted.
well first of all i haven't call a mkdir function in the update.php file

the directory has already been created using the previous script where i have also set the permission to 777.(mkdir ($_REQUEST["txtusername"], 0777).

when i am calling the update script it is giving me the error as premission denied.
Hope u understand what i m trying to do.

one script for creating a new directory and uploading a file.(working fine)

second script for just updating a given directory(its not allowing to update the directory)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [function.mkdir]: Permission denied warning

Post by VladSun »

Sorry, try to debug it yourself ...
Good luck!
There are 10 types of people in this world, those who understand binary and those who don't
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: [function.mkdir]: Permission denied warning

Post by cpetercarter »

Have a look at http://uk3.php.net/mkdir - scroll right down to the bottom of the page to the bottom two comments. This sounds like your problem.
harshilshah
Forum Newbie
Posts: 16
Joined: Fri Jul 17, 2009 5:13 am

Re: [function.mkdir]: Permission denied warning

Post by harshilshah »

cpetercarter wrote:Have a look at http://uk3.php.net/mkdir - scroll right down to the bottom of the page to the bottom two comments. This sounds like your problem.
Thank you very much cpetercarter. That really helped by adding the line $old_umask = umask(0); before using the mkdir function in the first script to make new directory.
this really worked well

here's the logic for reference


when you create a new directory using this code:

mkdir($dir, 0777);

The created folder actually has permissions of 0755, instead of the specified
0777. Why is this you ask? Because of umask(): http://www.php.net/umask

The default value of umask, at least on my setup, is 18. Which is 22 octal, or
0022. This means that when you use mkdir() to CHMOD the created folder to 0777,
PHP takes 0777 and substracts the current value of umask, in our case 0022, so
the result is 0755 - which is not what you wanted, probably.

The "fix" for this is simple, include this line:

$old_umask = umask(0);

Right before creating a folder with mkdir() to have the actual value you put be
used as the CHMOD. If you would like to return umask to its original value when
you're done, use this:

umask($old_umask);
thanks once again helping me out dude.
Mean a lot to me.
Post Reply