1 / 14

PHP File Upload

PHP File Upload. ISYS 475. PHP File Upload References. W3School: http://www.w3schools.com/php/php_file_upload.asp http://www.tizag.com/phpT/fileupload.php Photo: http://blazonry.com/scripting/upload-size.php http://www.plus2net.com/php_tutorial/php_file_upload.php Upload with MySQL

dasan
Télécharger la présentation

PHP File Upload

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP File Upload ISYS 475

  2. PHP File Upload References • W3School: • http://www.w3schools.com/php/php_file_upload.asp • http://www.tizag.com/phpT/fileupload.php • Photo: • http://blazonry.com/scripting/upload-size.php • http://www.plus2net.com/php_tutorial/php_file_upload.php • Upload with MySQL • http://php.about.com/od/phpwithmysql/ss/Upload_file_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" <form enctype="multipart/form-data" action="uploader.php" method="POST"> Choose a file to upload: <input name="uploadedfile" type="file" /><br /> <input type="submit" value="Upload File" /> </form>

  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 <?php if ($_FILES["uploadedfile"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br>"; } else { echo "Upload: " . $_FILES["uploadedfile"]["name"] . "<br>"; echo "Type: " . $_FILES["uploadedfile"]["type"] . "<br>"; echo "Size: " . ($_FILES["uploadedfile"]["size"] / 1024) . " kB<br>"; echo "Temporarily stored in: " . $_FILES["uploadedfile"]["tmp_name"]; } ?>

  6. move_uploaded_file function • boolmove_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 <?php if ($_FILES["uploadedfile"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br>"; } 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 <?php $allowedExts = array("gif", "jpeg", "jpg", "png"); $temp = explode(".", $_FILES["uploadedfile"]["name"]); $extension = strtolower(end($temp)); if (in_array($extension, $allowedExts)) { if ($_FILES["uploadedfile"]["error"] > 0) echo "Error: " . $_FILES["file"]["error"] . "<br>"; 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: <img border="0" src="/images/pulpit.jpg" alt="Pulpit rock" width="304" height="228">

  11. 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="" /><br><br> Choose a file to upload: <input name="uploadedfile" type="file" /><br /> <input type="submit" value="Upload File" /> </form>

  12. <?php $cid=$_POST["CID"]; $allowedExts = array("gif", "jpeg", "jpg", "png"); $temp = explode(".", $_FILES["uploadedfile"]["name"]); $extension = strtolower(end($temp)); if (in_array($extension, $allowedExts)) { if ($_FILES["uploadedfile"]["error"] > 0) echo "Error: " . $_FILES["file"]["error"] . "<br>"; 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 <?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>PhotoName</th></tr>"; foreach ($photos as $photo) { $cid=$photo["CID"]; $photoname=$photo["Photoname"]; echo "<tr><td>$cid</td><td><img border='0' src='Images/$photoname'</td></tr>"; } echo "</table>"; ?>

  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.

More Related