File uploading in PHP Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1
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
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
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
1. $_FILES: super-global variable… Keeps information about Name Size Type Tmp_name Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 5
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
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]
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’];
1. $_FILES: super-global variable… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 9 enctypemethod Input field Submit button
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
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
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
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
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
2. Uploading file… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 15 File tmp nameFile name Folder + filename File uploaded
2. Uploading file… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 16 Restricting Users: – Size restriction – Type restriction – File rename
2. Uploading file… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 17 Retrieving file attributes File rename Size and type File uploaded
3. Storing reference to database Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 18 name password picture Method is post Enctype=“multipart/form-data”
3. Storing reference to database… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 19
3. Storing reference to database… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 20 DB connection Executing query Insert query redirection
3. Storing reference to database… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 21
3. Storing reference to database… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 22 Record is added
3. Storing reference to database… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 23
4. CONNECTIONS: registration action Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 24 name password post pic
4. CONNECTIONS: registration action… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 25 Input is retrieved File is uploaded DB connection
4. CONNECTIONS: registration action… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 26 Input is validated
4. CONNECTIONS: registration action… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 27 Data is inserted ‘$password’,
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
References Chapter 30, “Beginning PHP and MySQL” by W. Jason Gilmore, Apress publisher, 4th edition; 2010, ISBN-13 (electronic): Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 29