Saving Uploaded
Files on the File System with PHP (move_uploaded_file() Function)
Now
let's say we find that the error code given by the PHP engine is
UPLOAD_ERR_OK and we want to save the uploaded file on the
file system without concerning what the uploaded file contains. We
can use the move_uploaded_file()
function to move the temporary file to the location we wanted.
For example:
move_uploaded_file($_FILES['myFile']['tmp_name'],
'/upload_files/myFile.txt');
If
everything works fine, the temporary file is moved to
"/upload_files/myFile.txt" and the move_uploaded_file()
function returns true.
PHP will overwrite the original "myFile.txt" file if
it exists.
Before
moving a file, the move_uploaded_file()
function checks if the file is really a file uploaded from the
client-side using HTTP POST so as to prevent your PHP script from
moving password and system files that are not supposed to be moved.
If the file does not pass the check or any other errors occur, the
file is not moved and the move_uploaded_file()
function returns false.
Processing Contents
of Uploaded Files with PHP
If
you do not want to save the uploaded file directly but to process it,
the PHP functions file_get_contents()
and fread() can help
you. The file_get_contents()
function returns a string that contains all data of the uploaded
file:
if
(is_uploaded_file($_FILES['myFile']['tmp_name'])) $fileData =
file_get_contents($_FILES['myFile']['tmp_name']);
The
is_uploaded_file() function
helps us check if a file is really a file uploaded from the
client-side using HTTP POST. If yes, the is_uploaded_file()
function returns true,
otherwise it returns false.
Make
sure the file you are going to process passes the check of the
is_uploaded_file() function.
This is to prevent your PHP script from processing files (such as
password files and system files) that are not supposed to be
processed.
If
everything works fine, the $fileData variable will contain the data
of the uploaded file. (Otherwise it will contain the Boolean value
false.) You can then perform what you like to do with the
data. For example, to replace all occurrences of the "A"
character in the data with the "B" character, you can write
something like this:
$fileData
= str_replace("A", "B", $fileData);
If
the uploaded file is large in size, you will not want to load the
whole file into memory with the file_get_contents()
function. The fread()
function can help you in this case. It reads a number of bytes
from a stream. fread() is used together with PHP functions
such as fopen(), feof(),
fclose(), etc.
Here is an example PHP script:
if
(is_uploaded_file($_FILES['myFile']['tmp_name'])){ $filePointer
= fopen($_FILES['myFile']['tmp_name'], "rb");
if
($filePointer!=false){ while
(!feof($filePointer)){ $fileData = fread($filePointer,
4096);
// Process the contents of the uploaded file
here...
}
fclose($filePointer); } }
Before
we can read the contents of the uploaded file with fread(), we
have to open a stream on the file first. This is achieved using the
PHP function fopen(). The
fopen() function takes
two parameters -- a file name and a flag that controls the
type of access. In the above PHP script, we specify "rb" as
the flag. The "r" letter states that we want to open the
uploaded file for reading and the "b" letter states that we
want to open the file in binary mode. If the uploaded file is opened
successfully, fopen() returns
a file pointer (or it can be called a file handle), otherwise it
returns false.
The
PHP function feof() is
used to check whether the file pointer has reached the EOF
(end-of-file) character. If yes, the feof()
function returns true,
otherwise it returns false.
In the above PHP script, we use a while loop to repeatedly check
whether the end of the file has been reached. If the end of the file
has not been reached, we will read the file data with fread(),
otherwise we will close the file.
The
PHP function fread()
is used to read a certain number of bytes. It takes two parameters --
a file pointer and an integer specifying the number of bytes to read.
In the above PHP script, we read at most 4096 bytes of data
each time. For example, if the size of the uploaded file is 5000
bytes, fread() reads 4096
bytes of data when it is called the first time and 904 bytes of data
when it is called the second time. If fread()
fails, it returns false.
The
PHP function fclose() is used
to close the file stream.
For
more information, please refer to PHP's
documentation on filesystem
functions.
Retrieving Values
of Ordinary Form Fields with PHP
If
your HTML/XHTML form contains ordinary input fields like <input
type="text"> besides <input type="file">,
you can use the $_POST array to retrieve the form field values. The
format is like this:
$_POST[value_of_name_attribute]
In
the above line, value_of_name_attribute
represents the name
attribute value of the <input> element. For example, we
will use the PHP code below:
$_POST['filename']
to
retrieve the value of the input field:
<input
name="filename" type="text"/>
|
Feedback Form (ExpandCollapse)
|
|