0

Hi everybody,

i created ajax uploader.what is the problem in my code. upload file failed message display.
anybody,plz help me.

This is the my action file.

class pictureActions extends sfActions
    {

        /**
         * This function may update later when plugin is used.
         */
        public function executeUpload(sfWebRequest $request)
        {
            $planbook_id = $request->getParameter('planbook_id');
                   $planbook = Doctrine::getTable('Planbook')->find($planbook_id);

            if($request->isMethod(sfRequest::POST)){
                $pictureDir = Doctrine::getTable('Planbook')->getPlanbookFolder

    ($planbook);

                $files = $request->getFiles('pictures');

                Doctrine::getTable('Picture')->uploadPicture($planbook, $files, 

    $pictureDir['picDir'], $pictureDir['thumbDir']);

                $planbook->setUpdatedAt(date('Y-m-d H:i:s', time()));
                $planbook->save();
                $this->redirect('@planbook_view?id='.$planbook->getId());
            }
        }

        // temp way to upload picture using plugin.
        public function executeAjaxUploadPicture(sfWebRequest $request)
        {
            $planbook = Doctrine::getTable('Planbook')->find($request-

    >getParameter('planbookId'));
            $pictureDir = Doctrine::getTable('Planbook')->getPlanbookFolder

    ($planbook);
            $pictureName = Doctrine::getTable('Picture')->generatePictureName

    ($request->getParameter('planbook_Id'));

            // list of valid extensions, ex. array("jpeg", "xml", "bmp")
            $allowedExtensions = str_replace('"','',explode(',',sfConfig::get

    ('app_picture_file_type_allowed')));
            // max file size in bytes
            $sizeLimit = sfConfig::get('app_picture_max_size');

            $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
            // store in here first
            $result = $uploader->handleUpload($pictureDir['picDir'], false, 

    $pictureName);
            $file = $result['filename'];
            Doctrine::getTable('Picture')->transformPicture($file, $pictureName, 

    $pictureDir['picDir'], $pictureDir['thumbDir'], $planbook);

            // Remove files in temp dir
            exec("rm -rf {$pictureDir['tempDir']}/*");

            // to pass data through iframe you will need to encode all html tags
            return $this->renderText(htmlspecialchars(json_encode($result

    ['message']), ENT_NOQUOTES));
        }

        /**
         * This function is for displaying image with extra setting to replace 

    showing image directly by showing directory
         */
        public function executeDisplayPic(sfWebRequest $request)
        {
            $info = explode('_',$request->getParameter('planbookId_thumbName'));
            $type = $request->getParameter('type');
            $this->forward404Unless(is_array($info) && in_array($type, 

    Doctrine::getTable('Picture')->getFolderInArray()), '@planbook');
            $planbook_id = $info[0];
            $picName = $info[1].".jpg";
            $planbook = Doctrine::getTable('Planbook')->find($palnbook_id);
            $personalId = $planbook->getPersonalId();

            $file = str_replace(array(":personal_id", ":planbook_id", 

    ":type"),array($personal_id, $planbook_id, $type),sfConfig::get

    ("app_picture_sub_dir")).$picName;

            header('Content-type: image/jpeg');
            header("Content-Length: " . filesize($file));
            readfile($file);
            exit;
        }

        public function executeView(sfWebRequest $request)
        {
            $this->picture = Doctrine_Core::getTable('Picture')->find(array

    ($request->getParameter('id')));
           // $this->forward404Unless($this->picture);

            }




        public function executeEdit(sfWebRequest $request)
        {
            $this->picture = Doctrine_Core::getTable('Picture')->find(array

    ($request->getParameter('id')));

        }

        public function executeUpdate(sfWebRequest $request)
        {
            $this->forward404Unless($request->isMethod(sfRequest::POST) || 

    $request->isMethod(sfRequest::PUT));
            $picture = Doctrine_Core::getTable('Picture')->find(array($request-

    >getParameter('id')));

            $picRequest = $request->getParameter('picture');
            $picture->fromArray($picRequest);
            $picture->save();
            $planbook = Doctrine::getTable('Planbook')->find($picture-

    >getPlanbookId());
             $planbook->save();
            $this->redirect('picture/edit?id='.$picture->getId());
        }

        public function executeDelete(sfWebRequest $request)
        {
            $request->checkCSRFProtection();

            $picture = Doctrine_Core::getTable('Picture')->find(array($request-

    >getParameter('id')));

            // delete files
            $pictureDir = Doctrine::getTable('Planbook')->getPlanbookFolder

    ($picture->getPlanbook());
            exec("rm ".$pictureDir['picDir'].$picture->getName().".jpg 

    ".$pictureDir['thumbDir'].$picture->getName().".jpg ");
            $picture->delete();

            if($nextPicId==$request->getParameter('id')){
                $this->redirect('@planbook');
            }
            $planbook = Doctrine::getTable('Planbook')->find($picture-

    >getPlanbookId());
                   $planbook->save();
               }
        /**
         * checkPhotoIsBelongToLoginUser() will forward user to 404 error page
         * if user try to modify picutre that not belong to him.
         */

    }


    this is the my  model class file




    class PictureTable extends Doctrine_Table
    {
        const FOLDER_THUMB = 'thumb';
        const FOLDER_PIC = 'pic';
        const FOLDER_TEMP = 'temp';

        const PICTURE_BMP = "image/bmp";
        const PICTURE_JPG = "image/jpeg";
        const PICTURE_PNG = "image/png";


        private $folder = array(
            self::FOLDER_THUMB => "thumb",
            self::FOLDER_PIC => "pic",
            self::FOLDER_TEMP => "temp",
        );

        private $mime = array(
            self::PICTURE_BMP=>self::PICTURE_BMP,
            self::PICTURE_JPG=>self::PICTURE_JPG,
            self::PICTURE_PNG=>self::PICTURE_PNG
        );

        public function getFolderInArray()
        {
            return $this->folder;
        }

        public function getMimeInArray()
        {
            return $this->mime;
        }

     public function generatePictureName($planbook_id)
        {
            do{
                $name = mt_rand(1000000000, 9999999999);
                $pdo = Doctrine_Query::create()->getConnection()->getDbh();
                $sql = "SELECT id
                        FROM picture
                        WHERE planbook_id = $planbook_id
                        AND name = $name
                        LIMIT 1";

                $stmt = $pdo->prepare($sql);
                $stmt->execute();
                $result = $stmt->fetch(PDO::FETCH_ASSOC);
                $stmt->closeCursor();
            }
            while($result);


            return $name;
        }

     public function uploadPicture($planbook, $files, $pic_dir, $thumb_dir)
        {
            foreach($files as $file){
                    if($file['size']!=0 && in_array($file['type'], $this->getMimeInArray())){
                        //set name of picture that want save
                        $name = Doctrine::getTable('picture')->generatePictureName($planbook->getId());
                        $this->savePicture($planbook, $name, $file, $pic_dir, $thumb_dir);
                    }
                }
        }

        public function savePicture($planbook, $name, $file, $pic_dir, $thumb_dir)
        {
            $this->transformPicture($file['tmp_name'], $name, $pic_dir, $thumb_dir, $planbook);
        }

        public function transformPicture($file, $name, $pic_dir, $thumb_dir, $planbook)
        {
            //set resized width and height, we try to keep the ration of uploaded image.
            list($width, $height) = getimagesize($file);
            $pic_width = $width;
            $pic_height = $height;
            $ratio = $pic_width/$pic_height;
            if($pic_width>sfConfig::get('app_picture_pic_max_width')){
                $pic_width = sfConfig::get('app_picture_pic_max_width');
                $pic_height = $pic_width/$ratio;
            }
            if($pic_height>sfConfig::get('app_picture_pic_max_height')){
                $pic_height = sfConfig::get('app_picture_pic_max_height');
                $pic_width = $pic_height*$ratio;
            }

            $img = new sfImage($file, mime_content_type($file));
            $img->thumbnail($pic_width,$pic_height);
            $img->saveAs($pic_dir.$name.'.jpg');

            $img = new sfImage($file, mime_content_type($file));
            $img->thumbnail(sfConfig::get('app_picture_thumb_width'),sfConfig::get('app_picture_thumb_height'));
            $img->saveAs($thumb_dir.$name.'.jpg');

            //insert data into database
            // remember to set updated_at of planbook
            $picture = new Picture();
            $picture->setPersonalId($planbook->getPersonalId());
            $picture->setPlanbookId($planbook->getId());
            $picture->setName($name);
            $picture->save();
        }

     public static function getInstance()


     {
            return Doctrine_Core::getTable('Picture');


    }
    Table:
    Picture:
  actAs: [Timestampable]
  columns:
    personal_id: { type: integer, notnull: true }
    planbook_id:    { type: integer, notnull: true }
    name:        { type: string(255), notnull: true }
    description: { type: string(1000) }
    position:    { type: int(1) }
  relations:
    Personal: { local: personal_id, foreign: id, onDelete: CASCADE }
    planbook: { local: planbook_id,foreign:id,onDelete:CASCADE  }
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by pritaeas
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.