Tema: Subir Imagen
Ver Mensaje Individual
  #23 (permalink)  
Antiguo 16/02/2008, 23:47
Avatar de elfran222
elfran222
 
Fecha de Ingreso: junio-2006
Mensajes: 550
Antigüedad: 17 años, 10 meses
Puntos: 7
Re: Subir Imagen

Continuación class.upload.php
Código:
            // do we do some image manipulation?
            $image_manipulation  = ($this->file_is_image && (
                                    $this->image_resize 
                                 || $this->image_convert != '' 
                                 || is_numeric($this->image_brightness) 
                                 || is_numeric($this->image_contrast) 
                                 || is_numeric($this->image_threshold) 
                                 || !empty($this->image_tint_color) 
                                 || !empty($this->image_overlay_color) 
                                 || !empty($this->image_text)
                                 || $this->image_greyscale
                                 || $this->image_negative
                                 || !empty($this->image_watermark)
                                 || is_numeric($this->image_rotate)
                                 || is_numeric($this->jpeg_size)
                                 || !empty($this->image_flip)
                                 || !empty($this->image_crop)
                                 || !empty($this->image_border)
                                 || $this->image_frame > 0
                                 || $this->image_bevel > 0
                                 || $this->image_reflection_height));

            if ($image_manipulation) {
                if ($this->image_convert=='') {
                    $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : '');
                    $this->log .= '- image operation, keep extension<br />';
                } else {
                    $this->file_dst_name = $this->file_dst_name_body . '.' . $this->image_convert;
                    $this->log .= '- image operation, change extension for conversion type<br />';
                }
            } else {
                $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : '');
                $this->log .= '- no image operation, keep extension<br />';
            }
            
            if (!$return_mode) {
                if (!$this->file_auto_rename) {
                    $this->log .= '- no auto_rename if same filename exists<br />';
                    $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name;
                } else {
                    $this->log .= '- checking for auto_rename<br />';
                    $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name;
                    $body     = $this->file_dst_name_body;
                    $cpt = 1;
                    while (@file_exists($this->file_dst_pathname)) {
                        $this->file_dst_name_body = $body . '_' . $cpt;
                        $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : '');
                        $cpt++;
                        $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name;
                    }               
                    if ($cpt>1) $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;auto_rename to ' . $this->file_dst_name . '<br />';
                }
            
                $this->log .= '- destination file details<br />';
                $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_name         : ' . $this->file_dst_name . '<br />';
                $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_pathname     : ' . $this->file_dst_pathname . '<br />';

                if ($this->file_overwrite) {
                     $this->log .= '- no overwrite checking<br />';
                } else {
                    if (@file_exists($this->file_dst_pathname)) {
                        $this->processed = false;
                        $this->error = $this->translate('already_exists', array($this->file_dst_name));
                    } else {
                        $this->log .= '- ' . $this->file_dst_name . ' doesn\'t exist already<br />';
                    }
                }
            }
        } else {
            $this->processed = false;
        }

        // if we have already moved the uploaded file, we use the temporary copy as source file, and check if it exists
        if (!empty($this->file_src_temp)) {
            $this->log .= '- use the temp file instead of the original file since it is a second process<br />';
            $this->file_src_pathname   = $this->file_src_temp;
            if (!file_exists($this->file_src_pathname)) {
                $this->processed = false;
                $this->error = $this->translate('temp_file_missing');
            }
        // if we haven't a temp file, and that we do check on uploads, we use is_uploaded_file()
        } else if (!$this->no_upload_check) {
            if (!is_uploaded_file($this->file_src_pathname)) {
                $this->processed = false;
                $this->error = $this->translate('source_missing');
            }
        // otherwise, if we don't check on uploaded files (local file for instance), we use file_exists()
        } else {
            if (!file_exists($this->file_src_pathname)) {
                $this->processed = false;
                $this->error = $this->translate('source_missing');
            }
        }
        
        // checks if the destination directory exists, and attempt to create it        
        if (!$return_mode) {
            if ($this->processed && !file_exists($this->file_dst_path)) {
                if ($this->dir_auto_create) {
                    $this->log .= '- ' . $this->file_dst_path . ' doesn\'t exist. Attempting creation:';
                    if (!$this->rmkdir($this->file_dst_path, $this->dir_chmod)) {
                        $this->log .= ' failed<br />';
                        $this->processed = false;
                        $this->error = $this->translate('destination_dir');
                    } else {
                        $this->log .= ' success<br />';
                    }
                } else {
                    $this->error = $this->translate('destination_dir_missing');
                }
            }

            if ($this->processed && !is_dir($this->file_dst_path)) {
                $this->processed = false;
                $this->error = $this->translate('destination_path_not_dir');
            }

            // checks if the destination directory is writeable, and attempt to make it writeable  
            $hash = md5($this->file_dst_name_body . rand(1, 1000));
            if ($this->processed && !($f = @fopen($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext, 'a+'))) {
                if ($this->dir_auto_chmod) {
                    $this->log .= '- ' . $this->file_dst_path . ' is not writeable. Attempting chmod:';
                    if (!@chmod($this->file_dst_path, $this->dir_chmod)) {
                        $this->log .= ' failed<br />';
                        $this->processed = false;
                        $this->error = $this->translate('destination_dir_write');
                    } else {
                        $this->log .= ' success<br />';
                        if (!($f = @fopen($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext, 'a+'))) { // we re-check
                            $this->processed = false;
                            $this->error = $this->translate('destination_dir_write');
                        } else {
                            @fclose($f);
                        }
                    }                
                } else {
                    $this->processed = false;
                    $this->error = $this->translate('destination_path_write');
                }
            } else {
                if ($this->processed) @fclose($f);
                @unlink($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext);
            }


            // if we have an uploaded file, and if it is the first process, and if we can't access the file directly (open_basedir restriction)
            // then we create a temp file that will be used as the source file in subsequent processes
            // the third condition is there to check if the file is not accessible *directly* (it already has positively gone through is_uploaded_file(), so it exists)
            if (!$this->no_upload_check && empty($this->file_src_temp) && !file_exists($this->file_src_pathname)) {
                $this->log .= '- attempting creating a temp file:';
                $hash = md5($this->file_dst_name_body . rand(1, 1000));
                if (move_uploaded_file($this->file_src_pathname, $this->file_dst_path . $hash . '.' . $this->file_dst_name_ext)) {
                    $this->file_src_pathname = $this->file_dst_path . $hash . '.' . $this->file_dst_name_ext;
                    $this->file_src_temp = $this->file_src_pathname;
                    $this->log .= ' file created<br />';
                    $this->log .= '    temp file is: ' . $this->file_src_temp . '<br />';
                } else {
                    $this->log .= ' failed<br />';
                    $this->processed = false;
                    $this->error = $this->translate('temp_file');
                }
            }
        }
        
        if ($this->processed) {

            if ($image_manipulation) {

                // checks if the source file is readable     
                if ($this->processed && !($f = @fopen($this->file_src_pathname, 'r'))) {
                    $this->processed = false;
                    $this->error = $this->translate('source_not_readable');
                } else {
                    @fclose($f);
                }