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

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
marshmellows_22
Forum Newbie
Posts: 2
Joined: Fri Jul 17, 2009 4:16 am

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

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post by VladSun »

I think it's a path issue.
So ... use absolute paths, not relative.
There are 10 types of people in this world, those who understand binary and those who don't
marshmellows_22
Forum Newbie
Posts: 2
Joined: Fri Jul 17, 2009 4:16 am

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

Post 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...
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

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

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post 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"]
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply