Skip to main content

Previewing the CSV file being uploaded to database using PHP

<html> 
<style>
    table, th, td {
        border: 1px solid black;
        border-collapse: collapse;
    }
</style>
<body>
<form method="post" enctype="multipart/form-data">
    Upload a CSV File:- <input type="file" value="Upload CSV Format" name="csvfile" required />
    <input type="submit" value="Upload" name="submit" />
</form>
</body>
</html>
<?php 
error_reporting(1); 
$server = "localhost"; 
$user_name = "root"; 
$password = "root"; 
$database = "sample"; 
$connection = mysqli_connect("$server","$user_name","$password","$database") or die("Error " . mysqli_error($connection)); 
$csvfile = $_FILES['csvfile']['name']; 
$ext = pathinfo($csvfile, PATHINFO_EXTENSION); 
$base_name = pathinfo($csvfile, PATHINFO_BASENAME); 
if (isset($_POST['submit'])) 
{     
 if($ext == "csv") 
 { 
  $handle = fopen($_FILES['csvfile']['tmp_name'], "r"); 
  echo "<html><body><table><tr><th>Name</th><th>Email</th></tr>";
   while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
   { 
     echo "<tr>"; 
     foreach ($data as $cell) 
     { 
      echo "<td>" . htmlspecialchars($cell) . "</td>"; 
     } 
     echo "</tr>"; 
   }         
   fclose($handle); 
   echo "</table></body></html>";
 }     
 else 
 { 
  echo " Check Extension. your extension is ." . $ext; 
 }
}
 ?>
 
output:
After uploading a csv file,you can see the table as follows

Upload a CSV File:-  
NameEmail
akhilakhil@gmail.com
maheshmahesh@gmail.com
srinusrinu@gmail.com

Comments

Popular posts from this blog

Send Email(with subject,body,attachment) using SSMTP through Terminal in linux?

1.Install ssmtp as              $ sudo apt-get update && sudo apt-get install ssmtp 2.Configure smtp file by editing the following file as, $ sudo gedit /etc/ssmtp/ssmtp.conf Update file with the following: AuthUser=Sendermail@gmail.com AuthPass=SenderPasswordforGMAIL FromLineOverride=YES mailhub=smtp.gmail.com:587 UseSTARTTLS=YES Now ssmtp.conf file should look like as the following one, AuthUser=Sendermail@gmail.com AuthPass=SenderPasswordforGMAIL # # Config file for sSMTP sendmail # # The person who gets all mail for userids < 1000 # Make this empty to disable rewriting. root=postmaster # The place where the mail goes. The actual machine name is required no # MX records are consulted. Commonly mailhosts are named mail.domain.com mailhub=smtp.gmail.com:587 # Where will the mail seem to come from? #rewriteDomain= # The full hostname hostname=tele-desktop118 # Are users allowed to set their own From: address? # Y

Backup (or) Download MYSQL Database as a zip file Using PHP

NOTE:Please change below credentials  with respect to your Database and Run the file. <?php define ( "DB_USER" ,  'DatabaseUSERNAME' ); define ( "DB_PASSWORD" ,  'DatabasePASSWORD' ); define ( "DB_NAME" ,  'DatabaseNAME' ); define ( "DB_HOST" ,  'localhost' ); define ( "BACKUP_DIR" ,  'myphp-backup-files' );  // Comment this line to use same script's directory ('.') define ( "TABLES" ,  '*' );  // Full backup //define("TABLES", 'table1, table2, table3'); // Partial backup of required tables define ( "CHARSET" ,  'utf8' ); define ( "GZIP_BACKUP_FILE" ,  true );   // Set to false if you want plain SQL backup files (not gzipped) class  Backup_Database {     var  $host ;     var  $username ;     var  $passwd ;     var  $dbName ;     var  $charset ;     var  $conn ;     var  $backupDir ;     var  $backupFile ;     var  $

Upload and Retrieve an image from database

/*Create table in sql:*/ CREATE TABLE `images` ( `id` int(11) NOT NULL AUTO_INCREMENT, `image` longblob NOT NULL, `created` datetime NOT NULL, PRIMARY KEY (`id`) ); /*Uploading an image to Database*/ <?php if ( isset ( $_POST [ "submit" ])){ $check = getimagesize ( $_FILES [ "image" ][ "tmp_name" ]); if ( $check !== false ){ $image = $_FILES [ 'image' ][ 'tmp_name' ]; $imgContent = addslashes ( file_get_contents ( $image )); $dbHost = 'localhost' ; $dbUsername = 'root' ; $dbPassword = 'root' ; $dbName = 'sample' ; //Create connection and select DB $db = new mysqli( $dbHost , $dbUsername , $dbPassword , $dbName ); // Check connection if ( $db -> connect_error ){ die ( "Connection failed: " . $db -> connect_error ); } $dateTime = date ( "Y-m-d H:i:s" )