CSV file upload and save it to MySQL database using PHP Code
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
Related posts:
- Function : Converts a MySQL Timestamp to Unix
- PHP Page Counter for website using a flat file and file handlings without database
- 10 Most Important PHP Code Snippets for Web Developers
- A code for the new Rupee Symbol for the Internet By Symbiosis (SICSR) Students
- How to install MySQL and integrate it with PHP (Windows)
2 Comments + Add Comment
Leave a comment
Recommended
Related Posts
- Function : Converts a MySQL Timestamp to Unix
- PHP Page Counter for website using a flat file and file handlings without database
- 10 Most Important PHP Code Snippets for Web Developers
- A code for the new Rupee Symbol for the Internet By Symbiosis (SICSR) Students
- How to install MySQL and integrate it with PHP (Windows)

An article by





[...] This post was mentioned on Twitter by Abhishek Sanghvi and Abhishek Sanghvi, Abhishek Sanghvi. Abhishek Sanghvi said: CSV file upload and save it to MySQL database using PHP Code http://bit.ly/cAoqBR #Code #php_scripts [...]
Seeing that i have been researching for a little bit for that sound study regarding this point . Browsing in Aol I finally saw this fabulous site. Reading this post So i’m pleased to enunciate that I have a nice impression I found everything that I was ready for. For certain i will be sure to don’t forget this website and visit on a constant basis.