File permissions not assigned when uploading images

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
sjslovechild
Forum Newbie
Posts: 2
Joined: Wed Mar 26, 2008 10:22 am

File permissions not assigned when uploading images

Post 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.";
    }
  }
?>
 
 
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: File permissions not assigned when uploading images

Post 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";
}
sjslovechild
Forum Newbie
Posts: 2
Joined: Wed Mar 26, 2008 10:22 am

Re: File permissions not assigned when uploading images

Post 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.
vish_krish
Forum Newbie
Posts: 6
Joined: Wed Mar 05, 2008 6:52 am

Re: File permissions not assigned when uploading images

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: File permissions not assigned when uploading images

Post 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?
Post Reply