Saturday 30 December 2017

How to download and Extract Uncompress zip / gz file from URL in PHP


File zip / gz download from URL

ini_set('display_errors', 1);
$dbname = 'db_'.date('Ymdhis').".gz";
$url = 'http://www.domainname.com/backups/bkp30122017.gz';
file_put_contents($dbname, fopen($url, 'r'));

Extract / Uncompress GZ / ZIP file

try{
    //This input should be from somewhere else, hard-coded in this example
    $file_name = $dbname;
    // Raising this value may increase performance
    $buffer_size = 4096; // read 4kb at a time
    $out_file_name = str_replace('.gz', '.sql', $file_name);
    // Open our files (in binary mode)
    $file = gzopen($file_name, 'rb');
    $out_file = fopen($out_file_name, 'wb');
    // Keep repeating until the end of the input file
    while (!gzeof($file)) {
        // Read buffer-size bytes
        // Both fwrite and gzread and binary-safe
        fwrite($out_file, gzread($file, $buffer_size));
    }
    // Files are done, close files
    fclose($out_file);
    gzclose($file);
} catch (Exception $ex) {
    echo $ex->getMessage();
}

Wednesday 27 December 2017

How to upload image / file in magento


<input type="file" name="image">

Select path of media directory

$path = Mage::getBaseDir() . DS . 'media' . DS . 'customerimages' . DS;
$imagename = $this->uploadImageFile($_FILES, $path);

Create functions

function uploadImageFile($filedata, $path){
   
    if(isset($filedata['image']) && $filedata['image']['error'] == 0){
        try{
           
            $uploader = new Varien_File_Uploader('image');
            $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png'));
            $uploader->setAllowRenameFiles(true);
           
            if(!is_dir($path)){
                mkdir($path, 0755, true);
            }
            $ext = pathinfo($filedata['image']['name'], PATHINFO_EXTENSION);
            $uploader->save($path, date('Ymd') . "_" . date('his') . '.' . $ext);
            $uploadFile = $uploader->getUploadedFileName();
            if($uploadFile){
                return $uploadFile;
            } else {
                return 'File not uploaded!';
            }
        } catch (Exception $e) {
            return $e->getMessage();
        }
    } else {
        return 'Please select image file!';
    }
}

Tuesday 26 December 2017

How to get product Tire / Final / Group price custom in Magento

How to get product Tire / Final / Group price custom order in Magento


$productid = 4108;
$qty = 3;
$customergroup = 1;
$_productB = Mage::getModel('catalog/product')->load($productid);
$_productB->setCustomerGroupId($customergroup);
echo $_productB->getPriceModel()->getFinalPrice($qty, $_productB);exit;

Monday 25 December 2017

Export Big sql/mysql database using php file

Export Big sql/mysql database using php file

error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', 1);
ini_set('memory_limit', '512M');
$dbinfo = array(
    "host" => 'localhost',
    "user" => 'root',
    "pass" => 'root',
    "dbname" => 'dbname'
);

// Database Config
$sqlhost = $dbinfo["host"];
$dbuser = $dbinfo["user"];
$dbpassword = $dbinfo["pass"];
$dbname = $dbinfo["dbname"];
// filename
$file = date('Ymdhis');
echo shell_exec("mysqldump --add-drop-table -u $dbuser -p$dbpassword`cat /etc/psa/.psa.shadow` $dbname > $file.sql");
//shell_exec("mysql -u $dbuser --password='$dbpassword' --host='$sqlhost' $dbname < $file");
echo 'Finished!<br/>';

File Directly Open in NetBeans in Ubuntu / Linux using terminal


 File Directly Open in NetBeans in Ubuntu / Linux using terminal

1. Open terminal and login with sudo su
2. Edit file using nano in terminal (Use following command)
sudo nano /usr/share/applications/netbeans-8.1.desktop 
3. Change

Exec=/bin/sh "/usr/local/netbeans-8.1/bin/netbeans"
to
Exec=/bin/sh "/usr/local/netbeans-8.1/bin/netbeans" %U

Terminal=0
to
Terminal=false
4. Press CTRL+X , than Y and Enter
5. That't IT, Now you can see NetBeans in open with application

Ref by. Nirav Patel

Thursday 21 December 2017

How to extract/ unzip ZIP / GZIP file using PHP

How to extract ZIP / GZIP file using PHP


<?php
$zip = new ZipArchive;
$res = $zip->open('myproject.zip');
if ($res === TRUE) {
  $zip->extractTo('/var/www/html/directoryname/');
  $zip->close();
  echo 'woot!';
} else {
  echo 'doh!';
}
?>

Tuesday 12 December 2017

How to Enable SSL certificate in cloud.google.com

How to Enable SSL certificate in cloud.google.com


Open cloud.google.com
1. Goto your VM instance
2. Click on name of your instance
3. Edit instance
4. Checked "Firewalls Allow HTTPS traffic" and save


5. Goto Network services -> Load balancing -> Edit / add your Load balancer
6. Add / Edit "https protocol" on Frontend configuration and select / create new certificate




7. Again goto your VM instance list
8. Click on SSH



9. After open google cloud ssh window, write "sudo su" on ssh window
10. ssh-keygen
11. gcloud compute ssl-certificates list
12. gcloud compute ssl-certificates describe ssl2111(name of your ssl certificate)
13. nano /etc/apache2/sites-available/default-ssl.conf
Upload certificate files and update path here where you place your SSLCertificateFiles
<IfModule mod_ssl.c>
    <VirtualHost _default_:443>
        ServerAdmin webmaster@localhost
        <Directory /var/www/html/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
    </Directory>
    DocumentRoot /var/www/html

    ..........
    ..........
    ..........
    ..........
    #SSLCertificateFile     /etc/ssl/certs/ssl-cert-snakeoil.pem
    #SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
    SSLCertificateFile "/var/www/html/cert/cert.pem"
    SSLCertificateKeyFile "/var/www/html/cert/privkey.key"
    SSLCertificateChainFile "/var/www/html/cert/chain.crt"
    ..........
    ..........
    ..........
    ..........
</VirtualHost>
</IfModule>

14. sudo service apache2 restart
15. gcloud auth login
16. gcloud config set project projectname-010412(Your project id)
17. sudo a2ensite default-ssl
18. service apache2 reload
19. sudo a2enmod ssl
20. sudo service apache2 restart
21. gcloud compute firewall-rules create www-firewall     --target-tags https-tag --allow tcp:443
22. service apache2 reload 

That's It, Please check your SSL 

How to check commit log of the particular file on Git or Bitbucket using terminal

How to check commit log of the particular file on Git or Bitbucket 


sudo git log -- app/code/local/jaydip/kansagra/controllers/CountryController.php