Thursday, 10 October 2013

Uploader class for cakephp

In this Post Iam writing code for uploading files in cakephp....

Create Uploader.php in Model folder & paste below code :-
<?php
 class Uploader extends AppModel
    {
        private $destinationPath;
        private $errorMessage;
        private $extensions;
        private $allowAll;
        private $maxSize;
        private $uploadName;
private $seqnence;
public $name='Uploader';
public $useTable =false;
        
        function setDir($path){
            $this->destinationPath  =   $path;
            $this->allowAll =   false;
        }
        
        function allowAllFormats(){
            $this->allowAll =   true;
        }
        
        function setMaxSize($sizeMB){
            $this->maxSize  =   $sizeMB * (1024*1024);
        }
        
        function setExtensions($options){
            $this->extensions   =   $options;
        }
        
function setSameFileName(){
$this->sameFileName = true;
$this->sameName = true;
}
        function getExtension($string){
            $ext = "";
            try{
                    $parts = explode(".",$string);
                    $ext = strtolower($parts[count($parts)-1]);
            }catch(Exception $c){
                    $ext = "";
            }
            return $ext;
}
        
        function setMessage($message){
            $this->errorMessage =   $message;
        }
        
        function getMessage(){
            return $this->errorMessage;
        }
        
        function getUploadName(){
            return $this->uploadName;
        }
        function setSequence($seq){
$this->imageSeq = $seq;
}

function getRandom(){
return strtotime(date('Y-m-d H:i:s')).rand(1111,9999).rand(11,99).rand(111,999);
}
function sameName($true){
$this->sameName = $true;
}
        function uploadFile($fileBrowse){ 
            $result =   false;
$size   =  $fileBrowse["size"];
            $name   =  $fileBrowse["name"];
            //$size   =   $_FILES[$fileBrowse]["size"];
            //$name   =   $_FILES[$fileBrowse]["name"];
            $ext    =   $this->getExtension($name);
            if(!is_dir($this->destinationPath)){
                $this->setMessage("Destination folder is not a directory ");
            }else if(!is_writable($this->destinationPath)){
                $this->setMessage("Destination is not writable !");
            }else if(empty($name)){
                $this->setMessage("File not selected ");
            }else if($size>$this->maxSize){
                $this->setMessage("Too large file !");
            }else if($this->allowAll || (!$this->allowAll && in_array($ext,$this->extensions))){
                
if($this->sameName==false){
                $this->uploadName   =  $this->imageSeq."-".substr(md5(rand(1111,9999)),0,8).$this->getRandom().rand(1111,1000).rand(99,9999).".".$ext;
                }else{
$this->uploadName= $name;
}
                if(move_uploaded_file($fileBrowse["tmp_name"],$this->destinationPath.$this->uploadName)){
                    $result =   true;
                }else{
                    $this->setMessage("Upload failed , try later !");
                }
            }else{
                $this->setMessage("Invalid file format !");
            }
            return $result;
        }
        
        function deleteUploaded(){
            unlink($this->destinationPath.$this->uploadName);
        }
        
    }  ?>

Now I will explain how to use it..

In the controller file include Uploder model in the $uses array.

the...
if the View file code is like this:-

<form name="fileform" method="post" action="<?php echo $urlRoot; ?>image/add"/>
<input type="file" name="txtFile"/>

</form>
Now  create folder media and then add index to core.php
Configure::write('MediaDir',dirname(dirname(__FILE__)).'/media/');

Now in controller folder,code like this

if(!empty($_FILES['txtFile']['name'])){ 
$mediaBaseDir = Configure::read('MediaDir');//Media Base Directory//created outside root folder,path set in the core.php
if(!is_dir($mediaBaseDir.'news')){
mkdir($mediaBaseDir.'news');
chmod($mediaBaseDir.'news',0777);
}
$mediaPath = $mediaBaseDir.'news'.'/';
$this->Uploader->setDir($mediaPath);
$this->Uploader->setExtensions(array('jpg','jpeg','png','gif'));
$this->Uploader->setMaxSize(.8);
$this->Uploader->setSequence('news'); 
$filename = $_FILES['txtFile'];
if($this->Uploader->uploadFile($filename)){ //upload success
$image = $this->Uploader->getUploadName();

}
}



Tuesday, 1 October 2013

Integrating album gallery in a wordpress site

In this post Iam explaining about integrating a new album gallery named Grand photo gallery plugin.
Download that plugin from: http://wordpress.org/plugins/flash-album-gallery/installation/

  1. Upload the files to 'wp-content/plugins/flash-album-gallery'.
  2. Activate the plugin.
  3. Be sure that after activation 'wp-content/plugins/flagallery-skins' folder (chmod 755) created successfully. If not, create it manually and install skins through admin Skins page or via ftp.
  4. Add a gallery and upload some images (the main gallery folder must have write permission).
  5. Go to your post/page an enter the tag '[flagallery gid=X]', where X - gallery IDs separated by comma. Easy way is click FlAGallery button on the Editor panel.
  6. If you would like to use additional Skins (only a option), go to Skins, download the skin and upload the file through Skins page in WordPress admin panel.
Steps

Add Gallery

The first thing you will want to do is upload some images. Before you can do that, you have to create a gallery. You do that on the Manage Galleries -> Add Gallery tab. Galleries are created below the path specified on the General Options page, typically wp-content/flagallery/. Creating a gallery actually creates a new folder on the server to store the images.

Adding Images

Once you’ve created a gallery you can add images to it. There are two different ways to add images to a gallery, by uploading them and by copying them to a location and then scanning the directory.
Note that when uploading images, check that the php.ini file for your server sets the maximum file upload size high enough.
Upload images one or more at a time. This is done from the Upload Images tab on the Manage Galleries page. You can select multiple files to upload and see progress of each image as it is being uploaded.

Managing Images

Once you get images into a gallery, you can perform a number of different operations on them. Probably the first thing you’ll want to do is to annotate them. While you’re at it you may as well add a description to your gallery.

Add Skins

Skin Options.
Each skin has its own set of properties. To change these properties, there is a control panel (Click to Active Skin Options or Options)

Add Gallery to Page / Post

add like 
<?php echo do_shortcode( '[flagallery gid=all name="Gallery"]' ); ?>

f you use gid=all:
[flagallery gid=all name="Gallery" orderby=title order=ASC excluude=3,5]

This shortcode will display all galleries (except ID=3 and ID=5) sorted by title, ASC order.
Wanna display only three galleries in specific order? No problem:
[flagallery gid=4,2,5 name="Gallery"]