How to upload files using PHP?
Hypertext Pre-processor (PHP) is a server side scripting language and there are special server side commands that you have to place in your web pages. Once you have the commands ready, these commands are processed before the pages are sent from your server to the web browser of the visitor. Basically, all files in PHP have a command embedded in it which is executed in the server and which are a mixture of text and HTML tags.
One of the best features of a PHP website is that it is fast to work with and ultra responsive. Hence, today more and more new entrepreneurs and website owners prefer to have a PHP website rather than HTML website as there are many advantages of having a PHP website. For those who are already know C, C++, Perl or Java can pick up PHP in very little time as the scripting for all these languages are similar and hence it makes it very easy for beginners to use PHP for their websites and other applications.
If you are trying to upload some files into MySQL using PHP than you will need some web application. However, before you upload the file to MySQL, make sure your file has all the information you will need. It is recommended that you make a table about the file information for your future references. Make a table which has information about the unique ID of the file, file name, and file content type, file size and the content of the file itself. Once you have made the table we can make use of BLOB data type for uploading the file.
There are various types of BLOB data types in MySQL and you need to select wisely which BLOB data to select. Uploading a file to MySQL is a two-step process where first you need to upload the file to the server and than read the file and insert it to MySQL. For uploading a file we need the visitors to fill up a form to enter the file name and than browse their computer and select a file. The input type "file" is used for that purpose.
<form method="post" enctype="multipart/form-data"> <table width="350" border="0" cellpadding="1" cellspacing="1"> <tr> <td width="246"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <input name="userfile" type="file" id="userfile"> </td> <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </tr> </table> </form>
An upload form must have encytype="multipart/form-data" otherwise it won't work at all. After the form is submitted we need to read the autoglobal $_FILES.
$_FILES['userfile']['name']
The original name of the file on the client machine.
$_FILES['userfile']['type']
The mime type of the file, if the browser provided this information. An example would be "image/gif".
$_FILES['userfile']['size']
The size, in bytes, of the uploaded file.
$_FILES['userfile']['tmp_name']
The temporary filename of the file in which the uploaded file was stored on the server.
$_FILES['userfile']['error']
The error code associated with this file upload. ['error'] was added in PHP 4.2.0
Normally, PHP saves all uploaded file with a temporary name and save the name in $_FILES['userfile']['tmp_name']. Hence, we now need to read the content of this file and insert the content to database. Always make sure that you use addslashes() to escape the content. Using addslashes () to the file name is also recommended because you never know what the file name would be.
Posted by James on 2009-10-29 in the category " php "
