PHP upload images in directory error

Discussion in 'PHP' started by [ DigitaL ], May 19, 2014.

  1. #1
    I am trying to upload photos in the folders from below php code but it creates new folder with year and month with 000 permission. Please help me. What is wrong with the below code?

    This is the code, please check and thanks for your time

    <?php
    require('include.php'); 
    require('extras/Uploader.php'); 
       
    function createFolder($subdir_path) {
        #$htaccess = "php_flag engine off" . "\r\n";
        #$htaccess .= "RemoveHandler .php .php5 .php4 .php3 .phtml .pl .asp" . "\r\n";
        #$htaccess .= "AddType text/plain .php .php .htm .html .phtml .pl .asp" . "\r\n";
        #$mode = 0777;
        @mkdir($subdir_path, $mode = 0777);
        @chmod($subdir_path, $mode = 0777);
        #file_put_contents($subdir_path . '.htaccess', $htaccess);
        @mkdir($subdir_path."thmb", $mode = 0777);
        @chmod($subdir_path."thmb", $mode = 0777);
        #file_put_contents($subdir_path."thmb/" . '.htaccess', $htaccess);
        @mkdir($subdir_path."bigThmb", $mode = 0777);
        @chmod($subdir_path."bigThmb", $mode = 0777);
        #file_put_contents($subdir_path."bigThmb/" . '.htaccess', $htaccess);
        @mkdir($subdir_path."mobile_thmb", $mode = 0777);
        @chmod($subdir_path."mobile_thmb", $mode = 0777);
        #file_put_contents($subdir_path."mobile_thmb/" . '.htaccess', $htaccess);
        @mkdir($subdir_path."mobile_bigThmb", $mode = 0777);
        @chmod($subdir_path."mobile_bigThmb", $mode = 0777);
        #file_put_contents($subdir_path."mobile_bigThmb/" . '.htaccess', $htaccess);
    }
    
    global $ads_settings;
    
    $widthLimit = 1920;
    $heightLimit = 1080;
    $minWidth = $ads_settings['big_thmb_width'];
    $minHeight = $ads_settings['big_thmb_height'];
    
    $dir = '../images/listings/';
    $subdir = date("Y-m");
    if(!file_exists($dir . $subdir . DIRECTORY_SEPARATOR)) {
        createFolder($dir . $subdir . DIRECTORY_SEPARATOR);
    }
    $filename = date("YmdHis") . "-" . str_replace(" ", "", microtime());
    
    $upload_dir = $dir . $subdir . DIRECTORY_SEPARATOR;
    $valid_extensions = array('gif', 'png', 'jpeg', 'jpg');
    
    $Upload = new FileUpload('uploadfile');
    $ext = $Upload->getExtension();
    $Upload->newFileName = $filename . "." . $ext;
    $result = $Upload->handleUpload($upload_dir, $valid_extensions);
    if (!$result) {
        echo json_encode(array('success' => false, 'msg' => $Upload->getErrorMsg()));  
    } else {
        $path = $Upload->getSavedFile();
        $imgsize = getimagesize($path);
        
        if ($imgsize[0] < $minWidth || $imgsize[1] < $minHeight) {
             echo json_encode(array('success' => false,
                                    'msg' => " not uploaded, minimal size photo {$minWidth}x{$minHeight}"));
             exit(0);
        }
       
        require_once("smart_resize_image.module.php");
        if ($imgsize[0] > $widthLimit || $imgsize[1] > $heightLimit)
            resize_image($path, $upload_dir . $filename . "." . $ext, $widthLimit, $heightLimit, $quality = 90, $crop = false);
    
        # create thmb
        resize_image($upload_dir . $filename . "." . $ext, $upload_dir . 'thmb' . DIRECTORY_SEPARATOR . $filename . "." . $ext, $ads_settings['thmb_width'], $ads_settings['thmb_height'], $quality = 90, $crop = true);
       
        # create bigThmb
        resize_image($upload_dir . $filename . "." . $ext, $upload_dir . 'bigThmb' . DIRECTORY_SEPARATOR . $filename . "." . $ext, $ads_settings['big_thmb_width'], $ads_settings['big_thmb_height'], $quality = 90, $crop = true);
       
        global $config_table_prefix;
        $mobile_settings = $db->fetchAssoc("SELECT * FROM {$config_table_prefix}mobile_settings");
        if ($mobile_settings['enable_mobile_templates']) {
       
            # create mobile_thmb
            resize_image($upload_dir . $filename . "." . $ext, $upload_dir . 'mobile_thmb' . DIRECTORY_SEPARATOR . $filename . "." . $ext, $mobile_settings['mobile_thmb_width'], $mobile_settings['mobile_thmb_height'], $quality = 90, $crop = true);
           
            # create mobile_bigThmb
            resize_image($upload_dir . $filename . "." . $ext, $upload_dir . 'mobile_bigThmb' . DIRECTORY_SEPARATOR . $filename . "." . $ext, $mobile_settings['mobile_big_thmb_width'], $mobile_settings['mobile_big_thmb_height'], $quality = 90, $crop = true);
           
        }
        if (!empty($ads_settings['watermark']) && file_exists($upload_dir . $filename . "." . $ext)) {
            require_once("watermark_image.module.php");
            add_watermark($upload_dir . $filename . "." . $ext, $ads_settings['watermark'], $ads_settings['watermark_position'], $ads_settings['watermark_transparency'], 90);
        }
               
        echo json_encode(array("success" => true,
                               "file" => $Upload->getFileName(),
                               "folder" => $subdir));
    }
    PHP:
     
    [ DigitaL ], May 19, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Haven't looked too closely at your code, but normally PHP shouldn't need to modify access on a created folder - as long as it's PHP that uploads files, it should have permissions already.
    For instance, when creating folders for a medialibrary I've made, it just creates folders, and then adds / removes files and folders without any trouble. Besides, you don't need a 7** permission to access and write files, you need a 644 permission (minimum) which lets the owner read/write, and other groups read
     
    PoPSiCLe, May 19, 2014 IP
  3. 2WDH.com

    2WDH.com Active Member

    Messages:
    143
    Likes Received:
    3
    Best Answers:
    5
    Trophy Points:
    68
    #3
    Hi.
    Try to uncomment the "#$mode = 0777;" line and use
    @mkdir($subdir_path, $mode);
    PHP:
    Also change all the other "$mode = 0777" to "$mode" in "mkdir" and remove all the "chmod" lines.
     
    Last edited: May 20, 2014
    2WDH.com, May 20, 2014 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    And second to that - never, EVER use 0777 on a publicly available site. EVER. It's a major security risk, especially if the check for allowed files aren't very good either.
     
    PoPSiCLe, May 20, 2014 IP
  5. Jigney

    Jigney Active Member

    Messages:
    168
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    93
    #5
    Just try to use below code
    Here you must focus on the absolute path of the directory

    mkdir(ABS_PATH.'/images/galleries/'.$dir, 0777);
    chmod(ABS_PATH.'/images/galleries/'.$dir, 0777);

     
    Jigney, May 21, 2014 IP