Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Engineering Lecture-08.

Similar presentations


Presentation on theme: "Web Engineering Lecture-08."— Presentation transcript:

1 Web Engineering Lecture-08

2 Lecture Outline File Upload Saving the Uploaded File

3 Quiz 2 Discuss modeling dimensions of a Web Application? Time: 15min

4 File Upload

5 File Upload File upload is required in many scenarios to allow users of to select files from their local computers and upload to online servers. Files may include images uploads, pdfs, audio or video files – depending on the requirements.

6 File Upload Cont. To begin, one need an HTML file upload form with a method of "post" and a specific encoding type. <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name=“arid-file" size="50" maxlength="25"> <br>    <input type="submit" name="upload" value="Upload"> </form>

7 File Upload Cont. When a file is uploaded, it gets stored in a temporary area on the server until it is moved. The file has to be moved from that area, or else it will be destroyed. In the meantime, the $_FILES[] super global array is filled up with data about the uploaded file.

8 File Upload Cont. Super Global Description
$_FILES['arid-file']['name'] Original Name of File Before It Was Uploaded $_FILES['arid-file']['type'] The MIME Type of File, Provided By the Browser $_FILES['arid-file']['size'] Size of the File (In Bytes) $_FILES['arid-file']['tmp_name'] Location of Temporary File on Server $_FILES['arid-file']['error'] Any Error Codes Resulting From the File Upload

9 File Upload Cont. To begin the file upload script, use the is_uploaded_file() function as an alternative to the isset() and empty() functions to verify that a file has been uploaded to its temporary location.

10 File Upload Cont. <?php if (is_uploaded_file($_FILES[' arid-file ']['tmp_name']) && $_FILES[' arid-file ']['error']==0) { echo "The file was uploaded successfully but has not been saved.<br>"; echo "The file is temporarily stored: " . $_FILES[' arid-file ']['tmp_name'] . "<br>"; echo "The file name was: " . $_FILES[' arid-file e ']['name'] . "<br>"; echo "The file type is: " . $_FILES[' arid-file ']['type'] . "<br>"; echo "The file size is: " . $_FILES[' arid-file ']['size'] . "<br>"; } else { echo "The file was not uploaded successfully."; echo "(Error Code:" . $_FILES[' arid-file ']['error'] . ")"; } ?>

11 File Upload Cont. The next step, applying restrictions of file upload. For example file type, size etc. Check if the file being uploaded already exist in the directory. One can use the file_exists() function for this purpose.

12 File Upload Cont. <?php if (is_uploaded_file($_FILES[' arid-file ']['tmp_name']) && $_FILES[' arid-file ']['error']==0) { $path = 'testupload/' . $_FILES[' arid-file ']['name']; if (!file_exists($path)) { echo "File does not exist. It is safe to move the temporary file."; } else { echo "File already exists. Please upload another file."; } } else { echo "The file was not uploaded successfully."; echo "(Error Code:" . $_FILES[' arid-file ']['error'] . ")"; } ?>

13 File Upload Cont. Finally, use the move_uploaded_file() function to move the temporary file into its permanent location.

14 <?php if (is_uploaded_file($_FILES[' arid-file ']['tmp_name']) && $_FILES[' arid-file ']['error']==0) { $path = 'testupload/' . $_FILES[' arid-file ']['name']; if (!file_exists($path)) { if (move_uploaded_file($_FILES[' arid-file ']['tmp_name'], $path)) { echo "The file was uploaded successfully."; } else { echo "The file was not uploaded successfully."; } } else { echo "File already exists. Please upload another file."; } } else { echo "The file was not uploaded successfully."; echo "(Error Code:" . $_FILES[' arid-file ']['error'] . ")"; } ?>

15 File Upload Cont. Two common problems that you may run into, causing the upload process to fail, are the file size and directory permissions. PHP sets a default "upload_max_filesize" to limit the size of the file uploaded. The default is 2M (megabytes) and any file that exceeds this limit will not upload. Also, if the directory (folder) where you try to move the file must have certain permissions set, or you will not be allowed to move the file into that directory.


Download ppt "Web Engineering Lecture-08."

Similar presentations


Ads by Google