Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP File Upload ISYS 475. PHP File Upload References W3School: –http://www.w3schools.com/php/php_file_upload.asp

Similar presentations


Presentation on theme: "PHP File Upload ISYS 475. PHP File Upload References W3School: –http://www.w3schools.com/php/php_file_upload.asp"— Presentation transcript:

1 PHP File Upload ISYS 475

2 PHP File Upload References W3School: –http://www.w3schools.com/php/php_file_upload.asp Photo: –http://blazonry.com/scripting/upload-size.php –http://www.plus2net.com/php_tutorial/php_file_upl oad.phphttp://www.plus2net.com/php_tutorial/php_file_upl oad.php Upload with MySQL –http://php.about.com/od/phpwithmysql/ss/Upload_f ile_sql.htm

3 HTML File Upload Control: input type=file Define a file-select field and a "Browse..." button (for file uploads): Form attribute: enctype="multipart/form- data" Choose a file to upload:

4 $_FILES Array A 2D (two dimensional) associative array of items uploaded to the current script via the HTTP POST method. Members: –$_FILES['userfile']['name'] (Specifies the original name of the file being uploaded on the client computer). –$_FILES['userfile']['type'] (Specifies the MIME type of the file being uploaded, for instance, "image/jpeg"). –$_FILES['userfile']['size'] (Indicates the size in bytes of the file being uploaded). –$_FILES['userfile']['tmp_name'] (Indicates the temporary name used by the web server to store the uploaded file). –$_FILES['userfile']['error'] (Specifies the error code associated with a specific file upload). Value 0: no error Greater than 0: error occured Note:userfile is the name or id attribute of the html input tag name. For the form in the previous slide, it is named uploadedfile

5 Example: Testing $_FILES 0) { echo "Error: ". $_FILES["file"]["error"]. " "; } else { echo "Upload: ". $_FILES["uploadedfile"]["name"]. " "; echo "Type: ". $_FILES["uploadedfile"]["type"]. " "; echo "Size: ". ($_FILES["uploadedfile"]["size"] / 1024). " kB "; echo "Temporarily stored in: ". $_FILES["uploadedfile"]["tmp_name"]; } ?>

6 move_uploaded_file function 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.

7 PHP functions useful for manipulating file name basename function: Given a string containing the path to a file or directory, this function will return the trailing name component. – basename( $_FILES['uploadedfile']['name']) explode and end function: The explode() function breaks a string into an array based on the specified separator and the end() function moves the internal pointer to the last element in the array. –$temp = explode(".", $_FILES["file"]["name"]); –$extension = end($temp); strtolower, strtoupper

8 Example: Upload an image file to the Images folder 0) { echo "Error: ". $_FILES["file"]["error"]. " "; } else { $target_path = "Images/"; $target_path = $target_path. basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else { echo "There was an error uploading the file, please try again!"; } ?>

9 Example: Restrict the upload file type 0) echo "Error: ". $_FILES["file"]["error"]. " "; else { $target_path = "Images/"; $target_path = $target_path. basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; else echo "There was an error uploading the file, please try again!"; } else echo "Invalid file"; ?> Note: in_array() function

10 Example of Processing Pictures SalesDB database Photos table: –CID –PhotoName: Creating links to picture files Insert pictures in web page –IMG tag example:

11 Upload photos and save CID and photo name in Photos table Enter CID: Choose a file to upload:

12 0) echo "Error: ". $_FILES["file"]["error"]. " "; else { $target_path = "Images/"; $target_path = $target_path. basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { $photoname=basename( $_FILES['uploadedfile']['name']); echo "The file ". $photoname. " has been uploaded"; $db = new PDO('mysql:host=localhost;dbname=salesdb', 'root', ''); $queryINS = "insert into photos value('$cid','$photoname')"; if( $db->exec($queryINS)==1) echo "Adding photo record successfully"; else echo "Adding photo record not successful"; } else echo "There was an error uploading the file, please try again!"; } else echo "Invalid file type"; ?>

13 Example: Show photos in the Images folder query($query); echo " ". " CID ". " PhotoName "; foreach ($photos as $photo) { $cid=$photo["CID"]; $photoname=$photo["Photoname"]; echo " $cid "; } echo " "; ?>

14 Insurance Claim Example Uploading claim pphotos for insurance cases. Each case may have many photos. Database: –CaseTable: CaseID, CaseDate, Agent –CasePics: CaseID, PhotoName 1. Create a web page with a dropdown list of CaseID, a File Upload control to choose file, and a upload button. The uploaded picture will be saved in the Images folder and a record will be entered in CasePics file. 2. Create a web page with a dropdown list of CaseID, then display photos of the selected case on the same page.


Download ppt "PHP File Upload ISYS 475. PHP File Upload References W3School: –http://www.w3schools.com/php/php_file_upload.asp"

Similar presentations


Ads by Google