force download file using php



By default most of the file types (eg: txt, jpg, png, gif, html, pdf, etc.) displayed in browser instead of download. But we can force browser to download these files instead of showing them.This tutorial goes over how to force file download in php.

<?php
   //file path to download
    $file_name=$_GET['file'];
    $outputfilename = "<DOWNLOAD_FILE_NAME>";
    header('Content-Description: File Transfer');
    //set content type
    header('Content-Type: application/octet-stream');
    //file name to save it may be different from original filename
    header("Content-Disposition:  attachment; filename=\"" . basename($outputfilename) . "\";" );
    //sends file size header to browser
    header('Content-Length: ' . filesize($file_name));
    header('Content-Transfer-Encoding: binary');
    header('Cache-Control: public');
    header('Pragma: public');
    ob_clean();
    //outputs file content to download stream
    readfile($file_name);
    exit;
?>

0 comments :

Add Google Plus Share Button to your site



Simple Tutorial to Create Google Plus Share Popup Window.

function shareongplus()
{
 var url2='https://plus.google.com/share?url='+encodeURIComponent("<URL_TO_SHARE>")+'&title='+encodeURIComponent('<TITLE_OF_SHARE>');

    newwindow=window.open(url2,'<TITLE_OF_POPUP>','height=450,width=650');
    if (window.focus) {newwindow.focus()}

}

Note :- You can use encodeURIComponent('Text To Encode') javascript function to encode text into url format.

0 comments :

Import csv file in mysql using php


This tutorial will go over how to import CSV ( comma separated value ) file data into Mysql Database using Php . 

CSV file is should be like below



<!-- form to submit csv file -->
<form enctype="multipart/form-data" method="post" role="form">
    <div class="form-group">
        <label for="exampleInputFile">File Upload</label>
        <input type="file" name="file" id="file" size="150">
        <p class="help-block">Only CSV File Import.</p>
    </div>
    <button type="submit" class="btn btn-default" name="Import" value="Import">Upload</button>
</form>


//php code to process csv file and store data into mysql database
<?php
if(isset($_POST["Import"]))
{
    //First we need to make a connection with the database
    $host='localhost'; // Host Name.
    $db_user= ''; //DB User Name
    $db_password= '';  //DB Password
    $db= ''; // Database Name.

    //Create connection with databse
    $conn=mysql_connect($host,$db_user,$db_password) or die (mysql_error());
    mysql_select_db($db) or die (mysql_error());
    echo $filename=$_FILES["file"]["tmp_name"];

    if($_FILES["file"]["size"] > 0)
    {
        $file = fopen($filename, "r");
$count = 0;
        while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
        {
$count++;
                        // ignore first row for column names
if($count>1)
{
                                //insert into database
$sql = "INSERT into demo1(id,name) values ('$emapData[0]','$emapData[1]')";
mysql_query($sql);
}
        }
        fclose($file);
        echo 'CSV File has been successfully Imported';
    }
    else
        echo 'Invalid File:Please Upload CSV File';
}
?>

0 comments :