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:

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:

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:

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.

What do you think about this web page? It is very helpful. It is helpful, but some information I wanted is missing. It is not helpful. It has broken links. It has typos / grammatical mistakes. It has incorrect information. Others

(Optional) Please provide us more details. For example, suppose you select option 2 above, can you tell us specifically what information is missing? You can also suggest anything that can help us improve this web page.

(Optional) Your name:

(Optional) Your email address:

Due to the amount of messages we received, we may not be able to reply to all messages.