Page 1 of 1

CHMOD Major Help needed.... permission from 0600 to 0644

Posted: Fri Jul 17, 2009 4:30 am
by marshmellows_22
Hello

I am developing a website which requires images to be uploaded through a CMS my the company owner only.

I have a upload.php(file browse option) which links to upload_file.php which works fine except when the files upload they are stored with a permission of 0600 and cannot for the life of me figure out how to change this automatically when the files upload.

If I transfer the files through the FTP manually then the permission is correct (0644) but not when uploaded through my CMS.

I have the following code for the upload_file:

Code: Select all

 
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/JPG")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000))
  {
  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("../images/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "../images/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "../images/" . $_FILES["file"]["name"];
     
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>
I have tried variations of the following:

Code: Select all

 
chmod("../images/" . $_FILES["file"]["name"], 0644); 
 
Nothing seems to work, I either break the whole server folder with the images in it or I receive an error message.

I can get the chmod to work if i manually enter the file name like:

Code: Select all

<?php chmod ("images/photo1.jpg", 0644); ?>
but thats not much use when the company owner is uploading files as it would require the code to be added every time

I have also tried changing the permissions when the files are being retrieved but that doesnt work either: (storing the filename in the database manually.) then retriveing it.

photos.php

Code: Select all

 
 
$result = @mysql_query('SELECT * FROM tbl_images');
 
if (!$result) {
exit('<p>Error Performing Query: ' . mysql_error() . '</p>');
}
 
 
while ($row = mysql_fetch_array($result)) {
 
chmod("images/' . $row['filename'] . '",0644);
 
 
I really am starting to get desperate as the client would like his website working properly but I am rather new to all this and this is just one thing I cannot seem to get my head around.

Any help would be greatly appreciated

Thank you

Re: CHMOD Major Help needed.... permission from 0600 to 0644

Posted: Fri Jul 17, 2009 5:34 am
by VladSun
I think it's a path issue.
So ... use absolute paths, not relative.

Re: CHMOD Major Help needed.... permission from 0600 to 0644

Posted: Fri Jul 17, 2009 8:12 am
by marshmellows_22
VladSun wrote:I think it's a path issue.
So ... use absolute paths, not relative.
What exactually does that mean? an example would be appreiciated...

Re: CHMOD Major Help needed.... permission from 0600 to 0644

Posted: Fri Jul 17, 2009 8:21 am
by spider.nick
I am wondering if the problem exists in this this line:

Code: Select all

chmod("../images/" . $_FILES["file"]["name"], 0644);
Is $_FILES['file']['name'] empty?

You could do something like:

Code: Select all

 
if( empty($_FILES['file']['name']) )
     die('Empty File Name');
 
That will kill the script, and output 'Empty File Name' if $_FILES['file']['name'] is indeed empty, and will just bypass the if-statement if it is not empty. That issue would explain why your folder is having permission errors.

Nick

Re: CHMOD Major Help needed.... permission from 0600 to 0644

Posted: Fri Jul 17, 2009 8:32 am
by VladSun
marshmellows_22 wrote:
VladSun wrote:I think it's a path issue.
So ... use absolute paths, not relative.
What exactually does that mean? an example would be appreiciated...
E.g., instead of

Code: Select all

"../images/" . $_FILES["file"]["name"]
use

Code: Select all

"/var/www/htdocs/images/" . $_FILES["file"]["name"]