Page 1 of 1

File permissions not assigned when uploading images

Posted: Wed Mar 26, 2008 10:25 am
by sjslovechild
I'm using PHP Version 4.4.3 on a Windows NT WEBHOST 5.2 build 3790

I'm having a odd problem with uploading image gallery pics to the server. I can upload the files with my php script and see the files via a FTP program. But the upload files have no file permissions. The server admin tells me that it is the Anonymous User permissions are not begin assigned.

The code I'm using is below. Is there anything in the code that I'm missing? thanks.

Code: Select all

 
<?php
// define a constant for the maximum upload size
define ('MAX_FILE_SIZE', 51200);
 
if (array_key_exists('upload', $_POST)) {
  // define constant for upload folder
  define('UPLOAD_DIR', '/path/to/picture/directory/pics/');
  // replace any spaces in original filename with underscores
  // at the same time, assign to a simpler variable
  $file = str_replace(' ', '_', $_FILES['image']['name']);
  // convert the maximum size to KB
  $max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
  // create an array of permitted MIME types
  $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
  // begin by assuming the file is unacceptable
  $sizeOK = false;
  $typeOK = false;
  
  // check that file is within the permitted size
  if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
    $sizeOK = true;
    }
 
  // check that file is of an permitted MIME type
  foreach ($permitted as $type) {
    if ($type == $_FILES['image']['type']) {
      $typeOK = true;
      break;
      }
    }
  
  if ($sizeOK && $typeOK) {
    switch($_FILES['image']['error']) {
      case 0:
        // move the file to the upload folder and rename it
        
        $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$file);
        if ($success) {
          $result = "$file uploaded successfully";
          }
        else {
          $result = "Error uploading $file. Please try again.";
          }
        break;
      case 3:
        $result = "Error uploading $file. Please try again.";
      default:
        $result = "System error uploading $file. Contact webmaster.";
      }
    }
  elseif ($_FILES['image']['error'] == 4) {
    $result = 'No file selected';
    }
  else {
    $result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png.";
    }
  }
?>
 
 

Re: File permissions not assigned when uploading images

Posted: Wed Mar 26, 2008 10:29 am
by onion2k
You need to add a call to chmod(), and possibly chown(), to the code inside of..

Code: Select all

if ($success) {
  $result = "$file uploaded successfully";
}

Re: File permissions not assigned when uploading images

Posted: Wed Mar 26, 2008 11:19 am
by sjslovechild
I have changed the below
onion2k wrote:You need to add a call to chmod(), and possibly chown(), to the code inside of..

Code: Select all

if ($success) {
  $result = "$file uploaded successfully";
}
to this

Code: Select all

        
if ($success) {
   chmod($result = "$file uploaded successfully", 0755);
}
But I'm having the same result. No permissions assigned.

Re: File permissions not assigned when uploading images

Posted: Wed Mar 26, 2008 12:51 pm
by vish_krish
i think the problem is with the permision of the directory where u r uploading the image . save it some where else and then try

Re: File permissions not assigned when uploading images

Posted: Wed Mar 26, 2008 3:48 pm
by onion2k
sjslovechild wrote:I have changed the below
onion2k wrote:You need to add a call to chmod(), and possibly chown(), to the code inside of..

Code: Select all

if ($success) {
  $result = "$file uploaded successfully";
}
to this

Code: Select all

       
if ($success) {
   chmod($result = "$file uploaded successfully", 0755);
}
But I'm having the same result. No permissions assigned.
Why do you think "$result = "$file uploaded successfully"" would refer to the filename you're trying to change the permissions for?