By
using this super global variable, we can get information of uploaded file. It
is a 2-dimensional array variable contains 5 elements. Each element is
providing information about uploaded files. Every elements 1st
dimension is name of upload control.
If user
uploads any file from browser to server, first that file transfers to temporary
memory location of server. We need to implement PHP script to move that file
from temporary memory location to permanent memory location. Server will provide
unique name at the time of storing file in temporary memory location.
Elements of $_FILES
1. $_FILES[‘name of uploaded control’][‘name’]
– This
element holds the name of uploaded file.
2. $_FILES[‘name of uploaded control’][‘size’]
– This
element holds the size of upload control in bytes.
3. $_FILES[‘name of uploaded control’][‘type’]
– It
holds MIME type of uploaded file.
4. $_FILES[‘name of uploaded control’][‘error’]
– To
get error number, if any occurred at the time of uploading file.
5. $_FILES[‘name of uploaded control’][‘tmp_name’]
– To
get temporary filename which is provided by server.
MIME – It stands for
Multipurpose Internet Mailing Extension. It is a type of extension used to
upload a file from browser to server. Every file contains different types of
MIME to upload into server from browser. Available MIME types are –
1. jpg – image/jpeg.
2. bmp – image/bmp.
3. exe – application/octet-stream
4. pdf – application/pdf
Note – Without MIME, we won’t
be able to upload file, only text controls will be supported.
multipart/form-data – It is a
common MIME type, can upload any type of file from browser to server.
is_uploaded_file – Using this
function, we can check whether file is uploaded or not from temporary location
to permanent location.
move_uploaded_file – To move
the uploaded file from temporary location to permanent memory location.
Arguments are temporary location filename and permanent location filename with
filepath.
Example –
form.html
<form method="post"
enctype="multipart/form-data" action="p1.php">
Browse File<input name="f1" type="file">
<br>
<input type="submit" name="Sub"
value="upload">
</form>
p1.php
<?php
$fname=$_FILES['f1']['name'];
if(move_uploaded_file($_FILES['f1']['tmp_name'],"upload/$fname"))
echo "File
moved successfully";
else
echo "OOPS
something went wrong";
?>
Note – upload is a new folder created in our
source project folder.
Configuration
settings to work with file upload concept
1. file_uploads
– Using this configuration setting we can
allow and stop file uploads. Default value is ‘ON’, by changing this value as ‘OFF’
we can stop file uploads.
2.
upload_tmp_dir – To change
temporary directory location of uploaded file. By default, all files will store
in tmp folder.
3.
upload_max_filesize – To increase
or to decrease maximum filesize to upload the file (By default 128 MB).
Program
to upload all files except .exe files
<?php
if ($_FILES['f1']['type']=="application/octet_stream")
{
echo "exe
files are not supported";
}
else
move_uploaded_file($_FILES['f1']['tmp_name'] ,"upload/".$_FILES['f1']['name']);
?>
$_FILES[‘name of uploaded control’][‘error’]
– To
get error number, if any error occurred at the time of uploading file. If
number is 0, there is no error. If
number is 1, then file size is
maximum than server configuration setting. If number is 2, then file size is maximum than browser configuration setting. If
number is 3, network problem at the
time of uploading file. If number is 4, user
select submit button without any file selection.
No comments:
Post a Comment