I want my users to be able to upload images to my website.
I think I need to change the folder permissions for this uploading to work, not sure how to do this though, I have tried the below but it doesn't work.

$uploaddir = chmod("/images/directory", 0777);

Here is the code for the page:

$uploaddir = "/images/directory"; // Where you want the files to upload to - Important: Make sure this folders permissions is 0777!

if(is_uploaded_file($_FILES['file']['tmp_name']))
{
move_uploaded_file ($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
}
print "Your file has been uploaded successfully! Yay!";
    $uploaddir = "/full/path/to/images/directory"; // Where you want the files to upload to - Important: Make sure this folders permissions is 0777!
    
    if(!is_writable($uploaddir))
      if(!chmod($uploaddir, 0777))
        die('Image directory not writable.');
    
    if(is_uploaded_file($_FILES['file']['tmp_name']))
    {
    move_uploaded_file ($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
    }
    print "Your file has been uploaded successfully! Yay!"; 

      Thanks for the reply!

      I'm getting the message:

      "Image directory not writable."

      I'm now using the code below:

      $uploaddir = "/user/htdocs/images/directory"; // Where you want the files to upload to - Important: Make sure this folders permissions is 0777! 
      
      if(!is_writable($uploaddir)) 
        if(!chmod($uploaddir, 0777)) 
          die('Image directory not writable.'); 
      
      if(is_uploaded_file($_FILES['file']['tmp_name'])) 
      { 
      move_uploaded_file ($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']); 
      } 
      print "Your file has been uploaded successfully! Yay!";
      

      Do I need to supply the username and password etc. in this file so it knows where to add the new image file?!?

        The problem is that PHP doesn't have permission to write to this folder or possibly its parent folder as well.

        Try manually chmodding the directory to 766 outside of PHP. If PHP doesn't have permissions to modify the folder in the first place, it definitely won't be able to change permissions.

        Alternatively, and I'm not 100% positive on this, but if you give PHP permission to write to the parent folder (e.g. the "images" folder in the path you gave above), I think PHP might then have the ability to change the permissions on the directory.

          Write a Reply...