Chmod Help On Php

Discussion in 'PHP' started by EliasJabb, Nov 25, 2007.

  1. #1
    i have this site which uploads images, but all images get a permission chmod of 66 not 777can ssomeone help me make it uploaded files receives a CHMOD of 777
     
    EliasJabb, Nov 25, 2007 IP
  2. -NB-

    -NB- Peon

    Messages:
    153
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Easy.
    
    $image_url = 'images/image.gif';
    chmod($image_url, 0777);
    
    PHP:
     
    -NB-, Nov 26, 2007 IP
  3. EliasJabb

    EliasJabb Well-Known Member

    Messages:
    119
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    tryed it doesnt work for some reason :S
     
    EliasJabb, Nov 26, 2007 IP
  4. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #4
    Some hostings disable chmod via PHP.
     
    wmtips, Nov 27, 2007 IP
  5. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You should chmod files 0644 and dirs 0755.

    Are you getting the chmod value from a string? Please post all related code.
     
    MMJ, Nov 27, 2007 IP
  6. salmanshafiq

    salmanshafiq Well-Known Member

    Messages:
    260
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    128
    #6
    this function works only in PHP 5 see which version of PHP you are using
     
    salmanshafiq, Nov 27, 2007 IP
  7. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Who told you that?

     
    MMJ, Nov 28, 2007 IP
  8. EliasJabb

    EliasJabb Well-Known Member

    Messages:
    119
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #8
    im tryin to get the cmod code into this script but i cant do it
    <?php
    /*

    - PHP4 Image upload script
    - coded by kreoton

    */

    class imageupload
    {
    //pblic variables
    var $path = '';
    var $errorStr = '';
    var $imgurl = '';

    //private variables
    var $_errors = array();
    var $_params = array();
    var $_lang = array();
    var $_maxsize = 1048576;

    var $_im_status = false;

    //public methods
    function imageupload ()
    {
    require 'config.php';
    $this->_types = $types;
    $this->_lang = $lang;
    $this->_upload_dir = $upload_dir;
    $this->_maxsize = $maxsize;

    $this->path = $PHP_SELF;

    if (is_array($_FILES['__upload']))
    {
    $this->_params = $_FILES['__upload'];
    if (function_exists('exif_imagetype'))
    $this->_doSafeUpload();
    else
    $this->_doUpload();

    if (count($this->_errors) > 0)
    $this->_errorMsg();
    }
    }

    function allowTypes ()
    {
    $str = '';
    if (count($this->_types) > 0) {
    $str = 'Allowed types: (';
    $str .= implode(', ', $this->_types);
    $str .= ')';
    }

    return $str;
    }

    // private methods
    function _doSafeUpload ()
    {
    preg_match('/\.([a-zA-Z]+?)$/', $this->_params['name'], $matches);
    if (exif_imagetype($this->_params['tmp_name']) && in_array(strtolower($matches[1]), $this->_types))
    {
    if ($this->_params['size'] > $this->_maxsize)
    $this->_errors[] = $this->_lang['E_SIZE'];
    else
    $this->_im_status = true;

    if ($this->_im_status == true)
    {
    $ext = substr($this->_params['name'], -4);
    $this->new_name = md5(time()).$ext;

    move_uploaded_file($this->_params['tmp_name'], $this->_upload_dir.$this->new_name);

    $this->imgurl = 'http://'.$_SERVER['HTTP_HOST'].preg_replace('/\/([^\/]+?)$/', '/', $_SERVER['PHP_SELF']).$this->_upload_dir.$this->new_name;
    }
    }
    else
    $this->_errors[] = $this->_lang['E_TYPE'];
    }

    function _doUpload ()
    {
    preg_match('/\.([a-zA-Z]+?)$/', $this->_params['name'], $matches);
    if(in_array(strtolower($matches[1]), $this->_types))
    {
    if ($this->_params['size'] > $this->_maxsize)
    $this->_errors[] = $this->_lang['E_SIZE'];
    else
    $this->_im_status = true;

    if ($this->_im_status == true)
    {
    $ext = substr($this->_params['name'], -3);
    $this->new_name = md5(time()).$ext;

    move_uploaded_file($this->_params['tmp_name'], $this->_upload_dir.$this->new_name);

    $this->imgurl = 'http://'.$_SERVER['HTTP_HOST'].preg_replace('/\/([^\/]+?)$/', '/', $_SERVER['PHP_SELF']).$this->_upload_dir.'/'.$this->new_name;
    }
    }
    else
    $this->_errors[] = $this->_lang['E_TYPE'];
    }

    function _errorMsg()
    {
    $this->errorStr = implode('<br />', $this->_errors);
    }
    }

    ?>

    can someone help me put the cmod code into it
     
    EliasJabb, Dec 1, 2007 IP
  9. Kwaku

    Kwaku Well-Known Member

    Messages:
    1,217
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    140
    #9
    under both move_uploaded_file($this->_params['tmp_name'], $this->_upload_dir.$this->new_name);

    chmod($this->_upload_dir.$this->new_name, 0777);

    but like MMJ commented; you need to study permissions and think why you would want 777, as world-writable is probably not needed at all.
     
    Kwaku, Dec 1, 2007 IP
  10. LilPutian

    LilPutian Well-Known Member

    Messages:
    250
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    128
    #10
    I am having a smimilar problem but I am getting no such file or directory

    
    $file_size=$HTTP_POST_FILES['Picture']['size'];
     if($file_size<50000)
            $r=mysql_query("show table status like 'member'");
            $id1=mysql_fetch_array($r);
            $id=$id1['Auto_increment'];
            $my_file ="usr".$id.".".strtolower($Extension[$ext]);
            move_uploaded_file($tmp_file,"picture/".$my_file);
            $image_url = '"picture/".$my_file';
            chmod($image_url,0644);
            $insert="INSERT INTO member SET UserName='".$_REQUEST['UserName']."',Password='".$_REQUEST['Password']."',Email='".$_REQUEST['Email']."',Regdate='".time()."',Picture='".$my_file."'"; 
           $r=mysql_query($insert);
            if(!session_is_registered("LogedInUserId"))
                                                                    {
            session_register("LogedInUserId");
    
    
    
    PHP:
    it works without the chmod line but the files are all chmod 600, I just moved severs and the old server didnt need this chmod line :(
     
    LilPutian, Feb 20, 2008 IP
  11. bpasc95

    bpasc95 Active Member

    Messages:
    196
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    70
    #11
    In some environments, we have found that permissions cannot be changed by the web user, typically Apache. Log in via SSH to see who "owns" the files. You can probably use FTP as well. This is what we have seen on "protective" web hosts.

    Hope that gets you in the right direction.

    -Bing
     
    bpasc95, Feb 20, 2008 IP
  12. LilPutian

    LilPutian Well-Known Member

    Messages:
    250
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    128
    #12
    I am running VPS so I am able to change my environment, what should i change and where? I currently have a cron job changing permissions every 15 minutes but that doesnt look very professional on a website :|

    BTW I am clueless about PHP so changing to FTP is going to be difficult for me
    Sample ls -l output

    
    -rw-r--r-- 1 vu2019 vu2019   9272 Feb 20 13:04 usr27.jpg
    -rw-r--r-- 1 vu2019 vu2019   9510 Feb 20 13:15 usr28.jpg
    -rw-r--r-- 1 vu2019 vu2019 365354 Feb 20 11:28 usr4.jpg
    -rw-r--r-- 1 vu2019 vu2019 838291 Feb 20 11:29 usr5.jpg
    -rw-r--r-- 1 vu2019 vu2019  28506 Feb 20 11:29 usr6.gif
    -rw-r--r-- 1 vu2019 vu2019   1621 Feb 20 11:29 usr7.gif
    -rw-r--r-- 1 vu2019 vu2019   5328 Feb 20 11:29 usr8.gif
    -rw-r--r-- 1 vu2019 vu2019  14497 Feb 20 11:29 usr9.gif
    
    PHP:
     
    LilPutian, Feb 20, 2008 IP
  13. keyspy

    keyspy Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    You should chmod files 644 and dirs 755.
    If you chmod 777 (can execute), your host will be hacked easily.
     
    keyspy, Feb 20, 2008 IP
  14. LilPutian

    LilPutian Well-Known Member

    Messages:
    250
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    128
    #14
    So is there anything I can change in my code or config to make this functional?
     
    LilPutian, Feb 20, 2008 IP