CSV file upload and save it to MySQL database using PHP Code

0saves

Hi friends, in this post I will guide you how to upload a csv file and to insert the values accordingly to MySQL database using PHP code.
The main functions used for the code are File handling functions which includes file_exists(), fopen(), filesize(), fread(), fclose(), is_writable() and fwrite(). Other than these functions mentioned the other functions used are explode(), implode(), str_replace() and trim().

You can download the source code here.

Explanations of Functions :

file_exists()

The file_exists() function checks whether or not a file or directory exists.

This function returns TRUE if the file or directory exists, otherwise it returns FALSE.

[ad code=3 align=center]

Syntax:

{code type=codetype}
file_exists(path)
{/code}

Example:

{code type=codetype}
Code :
<?php
echo file_exists(“test.txt”);
?>
Output :
1
{/code}

fopen()

The fopen() function opens a file or URL.

If fopen() fails, it returns FALSE and an error on failure. You can hide the error output by adding an ‘@’ in front of the function name.

Syntax:

{code type=codetype}
fopen(filename,mode,include_path,)
{/code}

Example:

{code type=codetype}
<?php
$file = fopen(“test.txt”,”r”);
?>
{/code}

filesize()

The filesize() function returns the size of the specified file.
This function returns the file size in bytes on success or FALSE on failure.

Syntax:

{code type=codetype}
filesize(filename);
{/code}

Example:

{code type=codetype}
Code:
<?php
echo filesize(“test.txt”);
?>

Output:
20
{/code}

fread()

The fread() reads from an open file.
The function will stop at the end of the file or when it reaches the specified length, whichever comes first.
This function returns the read string, or FALSE on failure.

Syntax :

{code type=codetype}
fread(file,length)
{/code}

Example : 1

Read 10 bytes from file:
{code type=codetype}
<?php
$file = fopen(“test.txt”,”r”);
fread($file,”10″);
fclose($file);
?>
{/code}

Example : 2

Read entire file:
{code type=codetype}
<?php
$file = fopen(“test.txt”,”r”);
fread($file,filesize(“test.txt”));
fclose($file);
?>
{/code}

fclose()

The fclose() function closes an open file.
This function returns TRUE on success or FALSE on failure.

Syntax :

{code type=codetype}
fclose(file)
{/code}

Example :

{code type=codetype}
<?php
$file = fopen(“test.txt”,”r”);
fclose($file);
?>
{/code}

is_writable()

The is_writable() function checks whether the specified file is writeable.
This function returns TRUE if the file is writeable.

Syntax :

{code type=codetype}
is_writable(file)
{/code}

Example :

{code type=codetype}
<?php
$file = “test.txt”;
if(is_writable($file))
{
echo (“$file is writeable”);
}
else
{
echo (“$file is not writeable”);
}
?>
Output:
test.txt is writeable
{/code}

[ad code=2]

fwrite()

The fwrite() writes to an open file.
The function will stop at the end of the file or when it reaches the specified length, whichever comes first.
This function returns the number of bytes written, or FALSE on failure.

Syntax :

{code type=codetype}
fwrite(file,string,length)
{/code}

Example :

{code type=codetype}
<?php
$file = fopen(“test.txt”,”w”);
fwrite($file,”Hello World. Testing!”);
fclose($file);
?>
{/code}

You can get more descriptions about the functions here

Source

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Related posts:

  1. Function : Converts a MySQL Timestamp to Unix
  2. PHP Page Counter for website using a flat file and file handlings without database
  3. 10 Most Important PHP Code Snippets for Web Developers
  4. A code for the new Rupee Symbol for the Internet By Symbiosis (SICSR) Students
  5. How to install MySQL and integrate it with PHP (Windows)

About the Author: Abhishek Sanghvi

Hi Friends, my name is Abhishek Sanghvi and I am the founder of this “open source script” site MYPHPSCRIPTZ. I have been learning and practising PHP from the last 3 years. You could always contact me if you wish to have some code for your need. I would surely try and solve them for you at abhishek(at)myphpscriptz(dot)com

2 Comments + Add Comment

Leave a comment


six + four =

CommentLuv badge