Hey all. I just noticed that when uploading image files from an HTML form with PHP that I've written, the file does indeed upload, but with permissions set at 600. Oddly enough, for these files, I cannot change permissions on them through my FTP program. I know that that's not really a PHP question, and it's not my real question, but does anyone know what's up with that? Weird.

Anyway, on to my actual question. I'm uploading these files on the PHP side with the standard move_uploaded_file() function, and they upload fine, but with what looks like a default set of permissions. Since it seems I can't set the proper permissions for these files through my FTP program, I figured I'd just do it through PHP after my file is successfully loaded. I'm using the following piece of code to do just this:

// $artwork is the name of the file with the appropriate directory information prepended already

if (!chmod($artwork, 0775))
{
   // generate error message
}

After I run the script, I check the appropriate directory through my FTP program (Transmit, BTW, for the Mac and unregistered), the file is there, but the permissions still show as 600. I don't get any error message stating that the chmod() didn't work, so I don't have that to go on. Any idea why it wouldn't work? I know that according to the php.net documentation, chmod() doesn't work with remote files, but I thought that it would technically be considered a local file since the actual PHP script resides on the server. Is this why it won't work? If so, is there any possible workaround? Thanks.

James

    make sure that $artwork is the full path to your file, not just your files name.

      Well, your first non-question question is a PHP question....

      When PHP uploads an image and moves items around as the script, it takes on the role of whatever user is running it. Most shared hosts run it as "noone" or an arbitrary account. So when you upload an image via FTP, you as the FTP user are the owner of that file. When PHP does it, PHP (or the user running PHP on the server) is the owner.

      The only possible reason that I would say it wouldn't work is if you're using an old name. Like if you had a script that uploaded a file, and then you ran this to try and CHMOD it to 0755, and used old referenced variables that are no longer available, you wouldn't chmod anything, or you'd chmod a temporary file. My guess is, you're chmodding a temp file, and you want the actual file.

      Double check your paths, and also, double check your filenames. One other alternative you could do, is do a directory listing, and if the permissions aren't right, correct them with the chmod() function.

      <?php
      
      $dir = '/some/usr/dir/images/';
      $d = dir($dir);
      while(FALSE !== ($entry = $d->read()))
      {
         if($entry != '.' && $entry != '..')
         {
            // get the file permissions
            $perms = substr(sprintf('%o', fileperms($dir.$entry)), -4);
            if($perms !== '0755')
            {
               if(chmod($dir.$entry, 0755)===TRUE)
               {
                  echo 'Changed permissions of file ('.$dir.$entry.') successfully.<br>';
               }
               else
               {
                   echo 'Failed to change permissions of file ('.$dir.$entry.').<br>';
               }
            }      
      } } ?>

      ~Brett

        Thanks for the responses! I'll take a look at my script and make sure I am using the correct file/path. It's entirely possible that I'm using the wrong file. I've contacted the developers of Transmit to find out why I can't change the perms through their app. I doubt it's a registration issue, as I can change the perms on other files in the same directory. It's only the ones that get uploaded via my script that it won't change. Thanks again.

        James

          5 days later

          It was an owner issue. PHP is running under a different user than what I log into FTP with. Which explains why I couldn't change the perms through my FTP app because the files belonged to the user account PHP is running under.

          Still haven't tested to make sure I have the correct path when changing the perms in my PHP script after a successful upload, but I'm sure I can get that squared away.

          James

            Write a Reply...