Presentation is loading. Please wait.

Presentation is loading. Please wait.

File uploading in PHP Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.

Similar presentations


Presentation on theme: "File uploading in PHP Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1."— Presentation transcript:

1 File uploading in PHP Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1

2 Summary of the previous lecture Creating database in MySQL using WAMP Connecting PHP with MySQL Inserting data in database CONNECTIONS: user registration Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan. 2

3 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 Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 3

4 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 Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 4

5 1. $_FILES: super-global variable… Keeps information about Name Size Type Tmp_name Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 5

6 1. $_FILES: super-global variable… FORM attributes required: Method should be post Enctype should be multipart/form-data Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 6

7 1. $_FILES: super-global variable… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 7 File type name Asad Choose File submit No File Chosen Mypci.jpg name Asad $_POST $_FILES pic [name]=>file name [type]=>file type [size]=>file size =>tmp name [tmp_name]

8 1. $_FILES: super-global variable… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 8 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’];

9 1. $_FILES: super-global variable… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 9 enctypemethod Input field Submit button

10 1. $_FILES: super-global variable… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 10 Display files array File name File type File size tmp name

11 1. $_FILES: super-global variable… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 11 Selected file Files array File name File type File size Temp name

12 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 Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 12

13 2. Uploading file… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 13 basename(): returns the filename from a path Example: <?PHP echo basename("/home/httpd/html/index.php"); ?> retutns index.php

14 2. Uploading file… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 14 File upload steps: – Identify the file to be uploaded tmp_name is used – Define destination Location + file name – Upload the file

15 2. Uploading file… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 15 File tmp nameFile name Folder + filename File uploaded

16 2. Uploading file… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 16 Restricting Users: – Size restriction – Type restriction – File rename

17 2. Uploading file… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 17 Retrieving file attributes File rename Size and type File uploaded

18 3. Storing reference to database Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 18 name email password picture Method is post Enctype=“multipart/form-data”

19 3. Storing reference to database… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 19

20 3. Storing reference to database… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 20 DB connection Executing query Insert query redirection

21 3. Storing reference to database… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 21

22 3. Storing reference to database… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 22 Record is added

23 3. Storing reference to database… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 23

24 4. CONNECTIONS: registration action Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 24 name email password post pic

25 4. CONNECTIONS: registration action… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 25 Input is retrieved File is uploaded DB connection

26 4. CONNECTIONS: registration action… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 26 Input is validated

27 4. CONNECTIONS: registration action… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 27 Data is inserted ‘$password’,

28 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 Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 28

29 References Chapter 30, “Beginning PHP and MySQL” by W. Jason Gilmore, Apress publisher, 4th edition; 2010, ISBN-13 (electronic): 978-1- 4302-3115-8. Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 29


Download ppt "File uploading in PHP Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1."

Similar presentations


Ads by Google