Download image using php from protected server

If you want to download image from a website where allow_url_fopen set to false or some other protection applied then you can’t download it with file_put_content() or copy() etc. Using curl and behaving like a user agent can be used to download image to local directory.

$img ='http://media.example.com/product/428x571/880.jpg'
$newinsrc = 'images/'.time().'.jpg';
$ch = curl_init($img);
$fp = fopen($newinsrc, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_exec($ch);
curl_close($ch);
fclose($fp);

Codeigniter Multiple File Upload function

An easy function to upload two image by a html form.

function save_images()
    {
        $html = '';
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png|JPEG|jpeg|PNG|GIF|JPG';
             
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
           
        if (!$this->upload->do_upload('pic1'))
        {
            $html .= $this->upload->display_errors();
            $html .='<br />File Not Uploaded';
            $upload_data['file_name']='nopic.png';
        }
        else
        {
            $upload_data = $this->upload->data();
            $html .='File Uploaded !<br />';
        }

 if (!$this->upload->do_upload('pic2'))
        {
            $html .= $this->upload->display_errors();
            $html .='<br />File Not Uploaded';
            $upload_data2['file_name']='nopic.png';
        }
        else
        {
            $upload_data2 = $this->upload->data();
            $html .='File Uploaded !<br />';
        }
           
        $dat=array(
        'main_pic'=>$upload_data['file_name'],
        'cover_pic'=>$upload_data2['file_name']
        );
       
        if($this->db->insert('picz', $dat))       
        return $html;
        else
        return 'Failed !';
       
    }
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="pic1" />

<input type="file" name="pic2" />

<input type="submit" name="subpic" value="upload" />
</form>

Send json in HTTP POST request by CURL php

A simple post operation by php curl. And send some json data.

//extract data from the post
extract($_POST);

//set POST variables
$url = 'http://aajit.com/apipath';
$jsodata='{"to":"foo", "msg":"bar", "token":"blah"}';
$fields_string ="aajgw=".$jsodata;
 
//open connection
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 1); // 1 for number of params (aajgw)
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);
echo $result;

//close connection
curl_close($ch);

Pass data from controller to view in magento

It’s may be very easy for Zend framework user But, I find some magento developer fighting to pass data from controller to view.
In magento you have to create a empty model(if your mod don’t have a varient object model) which extends to varient object. Magento use magic getter magic setter so you can pass value by setAnyname() and getAnyname() thus functions will be auto defines by Zend like many other framework do. :-)

Model file:

Packname_Modulename_Model_Viewinfo extends Varien_Object{

} 

Controller function:

public function indexAction()
{	
  $bla = Mage::getSingleton('Modulename/viewinfo');
  $bla->setMsg("Thank You for your visit!");
  $this->loadLayout();
  $this->renderLayout();
}

Now in your View .phtml file add:

<?php $blas = Mage::getSingleton('Modulename/viewinfo'); ?>
<?php echo $blas->getMsg(); ?>

Nobody beat us Fry us and eat us