PHP File Upload ISYS 475 PHP File Upload

  • Slides: 14
Download presentation
PHP File Upload ISYS 475

PHP File Upload ISYS 475

PHP File Upload References • W 3 School: – http: //www. w 3 schools.

PHP File Upload References • W 3 School: – http: //www. w 3 schools. com/php_file_upload. asp • http: //www. tizag. com/php. T/fileupload. php • Photo: – http: //blazonry. com/scripting/upload-size. php – http: //www. plus 2 net. com/php_tutorial/php_file_upl oad. php • Upload with My. SQL – http: //php. about. com/od/phpwithmysql/ss/Upload_f ile_sql. htm

HTML File Upload Control: input type=file • Define a file-select field and a "Browse.

HTML File Upload Control: input type=file • Define a file-select field and a "Browse. . . " button (for file uploads): • Form attribute: enctype="multipart/formdata" <form enctype="multipart/form-data" action="uploader. php" method="POST"> Choose a file to upload: <input name="uploadedfile" type="file" /> <input type="submit" value="Upload File" /> </form>

$_FILES Array • A 2 D (two dimensional) associative array of items uploaded to

$_FILES Array • A 2 D (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”

Example: Testing $_FILES <? php if ($_FILES["uploadedfile"]["error"] > 0) { echo "Error: ". $_FILES["file"]["error"].

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

move_uploaded_file function • bool move_uploaded_file ( string $filename , string $destination ) • This

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.

PHP functions useful for manipulating file name • basename function: Given a string containing

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

Example: Upload an image file to the Images folder <? php if ($_FILES["uploadedfile"]["error"] >

Example: Upload an image file to the Images folder <? php if ($_FILES["uploadedfile"]["error"] > 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!"; } } ? >

Example: Restrict the upload file type <? php $allowed. Exts = array("gif", "jpeg", "jpg",

Example: Restrict the upload file type <? php $allowed. Exts = array("gif", "jpeg", "jpg", "png"); $temp = explode(". ", $_FILES["uploadedfile"]["name"]); $extension = strtolower(end($temp)); if (in_array($extension, $allowed. Exts)) { if ($_FILES["uploadedfile"]["error"] > 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

Example of Processing Pictures • Sales. DB database Photos table: – CID – Photo.

Example of Processing Pictures • Sales. DB database Photos table: – CID – Photo. Name: • Creating links to picture files • Insert pictures in web page – IMG tag example: <img border="0" src="/images/pulpit. jpg" alt="Pulpit rock" width="304" height="228">

Upload photos and save CID and photo name in Photos table <form enctype="multipart/form-data" action="uploader.

Upload photos and save CID and photo name in Photos table <form enctype="multipart/form-data" action="uploader. php" method="POST"> Enter CID: <input type="text" name="CID" value="" /> Choose a file to upload: <input name="uploadedfile" type="file" /> <input type="submit" value="Upload File" /> </form>

<? php $cid=$_POST["CID"]; $allowed. Exts = array("gif", "jpeg", "jpg", "png"); $temp = explode(". ",

<? php $cid=$_POST["CID"]; $allowed. Exts = array("gif", "jpeg", "jpg", "png"); $temp = explode(". ", $_FILES["uploadedfile"]["name"]); $extension = strtolower(end($temp)); if (in_array($extension, $allowed. Exts)) { if ($_FILES["uploadedfile"]["error"] > 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', ''); $query. INS = "insert into photos value('$cid', '$photoname')"; if( $db->exec($query. INS)==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"; ? >

Example: Show photos in the Images folder <? php $db = new PDO('mysql: host=localhost;

Example: Show photos in the Images folder <? php $db = new PDO('mysql: host=localhost; dbname=salesdb', 'root', ''); $query="select * from photos"; $photos = $db->query($query); echo "<table border=1><tr>". "<th>CID</th>". "<th>Photo. Name</th></tr>"; foreach ($photos as $photo) { $cid=$photo["CID"]; $photoname=$photo["Photoname"]; echo "<tr><td>$cid</td><img border='0' src='Images/$photoname'</td></tr>"; } echo "</table>"; ? >

Insurance Claim Example • Uploading claim pphotos for insurance cases. • Each case may

Insurance Claim Example • Uploading claim pphotos for insurance cases. • Each case may have many photos. • Database: – Case. Table: Case. ID, Case. Date, Agent – Case. Pics: Case. ID, Photo. Name • 1. Create a web page with a dropdown list of Case. ID, 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 Case. Pics file. • 2. Create a web page with a dropdown list of Case. ID, then display photos of the selected case on the same page.