php

Download csv using PhpExcel

download phpExcel : https://phpexcel.codeplex.com/

<?php
    //data to make csv (may be from database, form, etc)
    $dbdata = array(
                array("name"=>"name1", "phone"=>"1010101001"),
                array("name"=>"name2", "phone"=>"1010101002")
            );

    require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
    require_once 'PHPExcel/Classes/PHPExcel.php';
    
    // Create new PHPExcel object
    $objPHPExcel = new PHPExcel(); 
    
    //setting column heading
    $objPHPExcel->getActiveSheet()->setCellValue('A1',"Name"); 
    $objPHPExcel->getActiveSheet()->setCellValue('B1',"Phone"); 
    
    //setting column body
    $i=2; //starting from row 2 bcz row 1 set to header
    foreach($dbdata as $data) {
        $objPHPExcel->getActiveSheet()->setCellValue('A'.$i,$data['name']);
        $objPHPExcel->getActiveSheet()->setCellValue('B'.$i,$data['phone']);
        $i++;
    }
    
    // Redirect output to a client’s web browser (Excel5)
    header('Content-type: text/csv');
    header('Content-Disposition: attachment;filename="export.csv"');
    header('Cache-Control: max-age=0');
    
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');

    $objWriter->save('php://output');
    exit;
?>

 

By bm on May 16, 2016 | php | A comment?

PHP How to zip a folder

For ziping and unziping we can use PHP ZipArchive libraries

bellow code will zip a folder recursively

<?php
// Get real path for our folder
echo $rootPath = realpath('Newfolder');

// Initialize archive object
$zip = new ZipArchive();
$zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE );

// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY);

foreach ($files as $name => $file)
{
    // Skip directories (they would be added automatically)
    if (!$file->isDir())
    {
        // Get real and relative path for current file
        $filePath = $file->getRealPath();
        $relativePath = substr($filePath, strlen($rootPath) + 1);

        // Add current file to archive
        $zip->addFile($filePath, $relativePath);
    }
}

// Zip archive will be created only after closing object
$zip->close();
?>

 

For unzip

<?php    
    $zip = new ZipArchive;
    $zip->open('sample.zip');
    $zip->extractTo('./');
    $zip->close(); 
?>

Requirements :

http://php.net/manual/en/zip.requirements.php

Note : – php5.6 bellow version wi;; not support to create password protected zip,  php5.6 and above will support to create password protected archives. All you need is to add this code ZipArchive::setPassword($password).

ref:

http://php.net/manual/en/book.zip.php, http://php.net/manual/en/zip.examples.php, http://stackoverflow.com/questions/4914750/how-to-zip-a-whole-folder-using-php

By bm on May 12, 2016 | php | A comment?

php mearge two arrays

<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
o/p
Array
(
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4
)
By bm on February 13, 2014 | php | A comment?

How to get the values in between tags

<?php

$v = "this is test <abcd>value1 ok</abcd> dont know tell me <abcd>value33 kooo ok</abcd> this is php regress <abcd>teeth value</abcd> ok how";
preg_match_all("#<abcd>([^<]+)</abcd>#", $v, $foo);
echo implode("\n", $foo[1]);

?>
By bm on January 22, 2014 | php | A comment?

prestashop get admin details from outside

<?php
include_once('../config/config.inc.php');
include_once('../config/settings.inc.php');
include_once('../classes/Cookie.php');
$cookie = new Cookie('psAdmin');

print_r($cookie);
?>

 

By bm on January 10, 2014 | php, prestashop | A comment?

php get file extension

echo pathinfo($_FILES['BM_BACKGROUND_IMAGE']['name'], PATHINFO_EXTENSION);

 

By bm on | php | A comment?