Blobbing Data With PHP and MySQL - The grabfile.php script
(Page 4 of 7 )
Before we actually process the uploaded file as a BLOB, let's first make sure that PHP has the capabilities to handle uploaded files. Create a new PHP script named phpinfo.php and enter the following line into it:
<?php phpinfo(); ?>Run the script in your browser. It displays the details of your PHP configuration. There are three variables that we need to look for:
- file_uploads: Tells PHP whether or not to accept file uploads. Must have the value "On" to accept files.
- upload_max_filesize: Tells PHP the maximum file size that an file uploaded can be. Is specified as [Number of megabytes]M, for example 2M for two megabytes.
- upload_tmp_dir: The directory where PHP will store uploaded files until they are copied/moved. If empty, PHP stores them in the operating systems default temporary directory.
If you need to change any of these options, you can do so in PHP's configuration file. Anyhow, let's get back to our grabfile.php script. Create a new file named grabfile.php and paste each line of code shown below into it. We will run through the code line by line:
<?php
// GrabFile.php: Takes the details
// of the new file posted as part
// of the form and adds it to the
// myBlobs table of our myFiles DB.
global $strDesc;
global $fileUpload;
global $fileUpload_name;
global $fileUpload_size;
global $fileUpload_type;We start by declaring five global variables. When we pass any form values to a PHP script, PHP automatically makes the name of that element available to us as a global variable. So, for example, if I have a form element like this:
<input type="text" name="myName" value="Mitchell Harper">... then if I posted this form to a PHP script using the post method, PHP would automatically create a variable named $myName, containing the value "Mitchell Harper". Form values are also stored in arrays, depending on the method used to post the form (get/post). For forms posted using the get method, they are stored in the associative array, $HTTP_GET_VARS, so I would access my form element like this:
echo $HTTP_GET_VARS["myName"];For forms posted using the post method, that forms elements are stored in the $HTTP_POST_VARS associative array:
echo $HTTP_POST_VARS["myName"];The other four global variables that I've defined hold the details of our uploaded file. They are automatically created by PHP. Details of each of these variables are shown below:
- $fileUpload: Contains the full path to the temporary file that PHP has stored our uploaded file in. On my Windows 2000 server, it looks like this: "C:\WINNT\TEMP\php29F7.tmp".
- $fileUpload_name: The name of the file that we have uploaded, for example "myimage.gif".
- $fileUpload_size: The size of the file that we have uploaded, in bytes.
- $fileUpload_type: The content type of the file that we have uploaded, such as "image/gif" for a GIF image.
Next, we make sure that the user has entered both a file description and a file name (notice how the $fileUpload variable contains the value "none" when a file hasn't been uploaded):
// Make sure both a description and
// file have been entered
if(empty($strDesc) || $fileUpload == "none")
die("You must enter both a description and file");Obviously, we will be connecting to, and querying our database to add our file to the myBlobs table. We define the connection details for our database as variables:
// Database connection variables
$dbServer = "localhost";
$dbDatabase = "myFiles";
$dbUser = "admin";
$dbPass = "password";PHP has several built-in functions that allow us to open and read files. We use the fopen and fread methods to open the uploaded file from the local directory on the web server, and then read its contents into a variable. The addslashes method escapes any apostrophises and double quotes in the file:
$fileHandle = fopen($fileUpload, "r");
$fileContent = fread($fileHandle, $fileUpload_size);
$fileContent = addslashes($fileContent);We connect to our database using PHP's built-in MySQL functions in combination with our database connection variables that we defined above:
$sConn = mysql_connect($dbServer, $dbUser, $dbPass)
or die("Couldn't connect to database server");
$dConn = mysql_select_db($dbDatabase, $sConn)
or die("Couldn't connect to database $dbDatabase");Once connected to the MySQL database, we run an insert query to actually add the details of our uploaded file (as a blob) to the myFiles table:
$dbQuery = "INSERT INTO myBlobs VALUES ";
$dbQuery .= "(0, '$strDesc', '$fileContent', '$fileUpload_type')";
mysql_query($dbQuery) or die("Couldn't add file to database");If the mysql_query function didn't succeed, then we our script calls the die function, which stops the execution of our script and outputs "Couldn't add file to database" to the clients browser. On the other hand, if the mysql_query function succeeded, then we output the details of the uploaded file to the browser:
echo "<h1>File Uploaded</h1>";
echo "The details of the uploaded file are shown below:<br><br>";
echo "<b>File name:</b> $fileUpload_name <br>";
echo "<b>File type:</b> $fileUpload_type <br>";
echo "<b>File size:</b> $fileUpload_size <br>";
echo "<b>Uploaded to:</b> $fileUpload <br><br>";
echo "<a href='uploadfile.php'>Add Another File</a>";And that's all there is to our getfile.php script! This is output from the script after I have uploaded a GIF image named ztest.gif:

A file repository involves being able to both upload and download files, so let's create a PHP script that will list all of the files in our myBlobs table. The script will also give us the option to download each of these files.
Next: Displaying the files >>
More MySQL Articles
More By Mitchell Harper