0% found this document useful (0 votes)
11 views

Lect 24 File Uploading in PHP

The document discusses file uploading in PHP. It covers the $_FILES super global variable which contains information about uploaded files, how to move an uploaded file to a destination folder using move_uploaded_file(), and how to store the file reference in a database. It then provides an example of user registration in a web application that allows file upload and stores the file reference in the database.

Uploaded by

Aqsa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lect 24 File Uploading in PHP

The document discusses file uploading in PHP. It covers the $_FILES super global variable which contains information about uploaded files, how to move an uploaded file to a destination folder using move_uploaded_file(), and how to store the file reference in a database. It then provides an example of user registration in a web application that allows file upload and stores the file reference in the database.

Uploaded by

Aqsa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Lecture 24

File uploading in PHP

Mr. Mubashir Ali


Lecturer (Dept. of Computer Science)
[email protected]
1
Summary of the previous lecture
• Creating database in MySQL using WAMP
• Connecting PHP with MySQL
• Inserting data in database
• CONNECTIONS: user registration

Mubashir Ali - Lecturer (Department of


2
Computer Science).
Outline
• FILES super global variable
• File uploading in PHP
• Storing reference of uploaded file in database
• User registration in CONNECTIONS web
application with file upload

Mubashir Ali - Lecturer (Department of


3
Computer Science).
1. $_FILES: super-global variable
• $_FILES: contains any item uploaded to the
server when the post method is used
• an array type variable
• Created automatically
• Can be accessed on other pages

Mubashir Ali - Lecturer (Department of


4
Computer Science).
1. $_FILES: super-global variable…
• Keeps information about
• Name
• Size
• Type
• Tmp_name

Mubashir Ali - Lecturer (Department of


5
Computer Science).
1. $_FILES: super-global variable…
• FORM attributes required:
• Method should be post
• Enctype should be multipart/form-data
• <form enctype=“multipart/form-data”
method=“post” action=“upload.php”>

Mubashir Ali - Lecturer (Department of


6
Computer Science).
1. $_FILES: super-global variable…
<body> Asad
<form method=“post” File type
Choose File NoMypci.jpg
File Chosen
enctype=“multipart/form-data”
action=“action.php”> submit
<input type=“text” name=“name”> name
<input type=“file” name=“pic”> $_POST
<input type=“submit”> name Asad
</form></body>

$_FILES
[name] =>file name [type] =>file type [size] =>file size [tmp_name] =>tmp nam

pic

Mubashir Ali - Lecturer (Department of


7
Computer Science).
1. $_FILES: super-global variable…
• Accessing file information
– $_FILES[‘input-field name’][‘name’];
– $_FILES[‘pic’][‘name’];
– $_FILES[‘input-field name’][‘type’];
– $_FILES[‘pic’][‘type’];
– $_FILES[‘input-field name’][‘size’];
– $_FILES[‘pic’][‘size’];

Mubashir Ali - Lecturer (Department of


8
Computer Science).
1. $_FILES: super-global variable…
enctype method

Input
field

Submit
button

Mubashir Ali - Lecturer (Department of


9
Computer Science).
1. $_FILES: super-global variable…

Display files array

File name

File type tmp name


File size

Mubashir Ali - Lecturer (Department of


10
Computer Science).
1. $_FILES: super-global variable…
Selected file

File name
File type Files array
File size

Temp name

Mubashir Ali - Lecturer (Department of


11
Computer Science).
2. Uploading file
• move_uploaded_file():
• bool move_uploaded_file ( string $filename ,
string $destination );
• This function checks to ensure that the file
designated by filename is a valid upload file
(meaning that it was uploaded via PHP's
HTTP POST upload mechanism)
• If the file is valid, it will be moved to the
filename given by destination
Mubashir Ali - Lecturer (Department of
12
Computer Science).
2. Uploading file…
• basename():
• returns the filename from a path
Example:
<?PHP
echo
basename("/home/httpd/html/index.php");
?>
retutns index.php

Mubashir Ali - Lecturer (Department of


13
Computer Science).
2. Uploading file…
• File upload steps:
– Identify the file to be uploaded
• tmp_name is used
– Define destination
• Location + file name
– Upload the file

Mubashir Ali - Lecturer (Department of


14
Computer Science).
2. Uploading file…
File name File tmp name

Folder + filename

File uploaded

Mubashir Ali - Lecturer (Department of


15
Computer Science).
2. Uploading file…
• Restricting Users:
– Size restriction
– Type restriction
– File rename

Mubashir Ali - Lecturer (Department of


16
Computer Science).
2. Uploading file…

Retrieving
file
attributes

File
rename
Size and type

File uploaded

Mubashir Ali - Lecturer (Department of


17
Computer Science).
3. Storing reference to database
Method is post
Enctype=“multipart/form-data”

name
email
password

picture

Mubashir Ali - Lecturer (Department of


18
Computer Science).
3. Storing reference to database…

Mubashir Ali - Lecturer (Department of


19
Computer Science).
3. Storing reference to database…
DB connection

Insert
query

Executing query

redirection

Mubashir Ali - Lecturer (Department of


20
Computer Science).
3. Storing reference to database…

Mubashir Ali - Lecturer (Department of


21
Computer Science).
3. Storing reference to database…

Record is added

Mubashir Ali - Lecturer (Department of


22
Computer Science).
3. Storing reference to database…

Mubashir Ali - Lecturer (Department of


23
Computer Science).
4. CONNECTIONS: registration action

post
name

email

password
pic

Mubashir Ali - Lecturer (Department of


24
Computer Science).
4. CONNECTIONS: registration action…

Input is
retrieved

File is
uploaded

DB connection

Mubashir Ali - Lecturer (Department of


25
Computer Science).
4. CONNECTIONS: registration action…

Input is
validated

Mubashir Ali - Lecturer (Department of


26
Computer Science).
4. CONNECTIONS: registration action…

‘$password’,
Data is
inserted

Mubashir Ali - Lecturer (Department of


27
Computer Science).
Summary
• FILES super global variable
• File uploading in PHP
• Storing reference of uploaded file in database
• User registration in CONNECTIONS web
application with file upload

Mubashir Ali - Lecturer (Department of


28
Computer Science).
References
• Chapter 30, “Beginning PHP and MySQL” by
W. Jason Gilmore, Apress publisher, 4th
edition; 2010, ISBN-13 (electronic): 978-1-
4302-3115-8.

Mubashir Ali - Lecturer (Department of


29
Computer Science).

You might also like