Tuesday, 17 May 2016

How to delete an array elements?

How to delete an array elements?

Question: How to delete an array element based on key?
$arrayData = array(0=>'one',1=>'two',3=>'three',4=>'four',5=>'five',6=>'six'); 
print_r($arrayData); echo "
";
/*
 * Array ( [0] => one [1] => two [3] => three [4] => four [5] => five [6] => six ) 
 */
unset($arrayData[4]);


print_r($arrayData);
/**
Array ( [0] => one [1] => two [3] => three [5] => five [6] => six ) 
 * 
 */



Question: How to delete multiple array element?
$arrayData = array(0=>'one',1=>'two',3=>'three',4=>'four',5=>'five',6=>'six'); 
print_r($arrayData); echo "
";
/*
 * Array ( [0] => one [1] => two [3] => three [4] => four [5] => five [6] => six ) 
 */
array_splice($arrayData, 1,2);


print_r($arrayData);
/**
Array ( [0] => one [1] => four [2] => five [3] => six ) 
 * 
 */



Question: How to replace multiple array element with single array?
$arrayData = array(0=>'one',1=>'two',3=>'three',4=>'four',5=>'five',6=>'six'); 
print_r($arrayData); echo "
";
/*
 * Array ( [0] => one [1] => two [3] => three [4] => four [5] => five [6] => six ) 
 */
array_splice($arrayData, 1,2,array('r'=>'replaced'));


print_r($arrayData);
/**
Array ( [0] => one [1] => replaced [2] => four [3] => five [4] => six ) 
 * 
 */



Question: How to delete First element of an array?
$arrayData = array(0=>'one',1=>'two',3=>'three',4=>'four',5=>'five',6=>'six'); 
print_r($arrayData); echo "
";
/*
 * Array ( [0] => one [1] => two [3] => three [4] => four [5] => five [6] => six ) 
 */
array_shift($arrayData);


print_r($arrayData);
/**
Array ( [0] => two [1] => three [2] => four [3] => five [4] => six ) 
 * 
 */



Question: How to delete Last element of an array?
$arrayData = array(0=>'one',1=>'two',3=>'three',4=>'four',5=>'five',6=>'six'); 
print_r($arrayData); echo "
";
/*
 * Array ( [0] => one [1] => two [3] => three [4] => four [5] => five [6] => six ) 
 */
array_pop($arrayData);


print_r($arrayData);
/**
Array ( [0] => one [1] => two [3] => three [4] => four [5] => five ) 
 * 
 */



Friday, 13 May 2016

How to download image from URL with PHP?

How to download image from url with PHP?

Question: How to download image from url and save in server?
//Image path which we will download
$imageURL='http://domain.com/image.jpg';

//get extension of image
$ext = pathinfo($imageURL, PATHINFO_EXTENSION);

//Local path of image - where will we save the image
$downloadTo = fopen('import/image/file'.  rand(1, 9999).'.'.$ext, 'wb');

//Download and save image
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $imageURL);
curl_setopt($ch, CURLOPT_FILE, $downloadTo);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);

For http URL, http image will download.
For https URL, https image will download.




Question: How to get image extension from url?
$imageURL='http://domain.com/image.jpg';
$ext = pathinfo($imageURL, PATHINFO_EXTENSION); //.jpg



Question: Download image from URL and display in browser?
    //Image path which we will download
    $imageURL='http://domain.com/image.jpg';

    //get extension of image
    $ext = pathinfo($imageURL, PATHINFO_EXTENSION);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $imageURL);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $output = curl_exec($ch);
    curl_close($ch);
    switch( $ext ) {
        case "gif": $ctype="image/gif"; break;
        case "png": $ctype="image/png"; break;
        case "jpeg":
        case "jpg": $ctype="image/jpeg"; break;
        default:
    }
    header('Content-type: ' . $ctype);


For http URLhttp image will download.
For https URLhttps image will download.


Question: How to check if image is valid OR Not?
$imageURL='http://domain.com/image.jpg';
 list($width, $height, $type, $attr) = getimagesize($imageURL);
 if (!empty($width) && !empty($height)) {
    echo "Valid Image";
    }else{
    echo "In Valid Image";
    }



Question: Download image from external URL, instead of displaying in browser?
        $imageURL='https://s3.ap-south-1.amazonaws.com/cloud-front-s3/8_12.jpg';
        $imageURLArray = explode('/',$imageURL);
        $downloadFileName=$imageURLArray[count($imageURLArray)-1];
        
        //get extension of image
        $ext = pathinfo($imageURL, PATHINFO_EXTENSION);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $imageURL);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
      
            
        header("Content-Type: application/octet-stream"); //
        // set it as an attachment and give a file name
        header('Content-Disposition: attachment; filename='.$downloadFileName);            
        echo $output;