SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
File Uploading in PHP
Shoaib Yasin
Idrees Hussain
Maqbool Ali
HTTP File Upload (RFC 1867)
<form action="file_upload.php" method="post"
enctype="multipart/form-data">
“No characters are encoded. This value is required when you are using
forms that have a file upload control”
<input type="hidden" name="MAX_FILE_SIZE"
value="30000" />
<input type="file" name="upload" />
<input type="submit" value="Upload" />
</form>
• Set attribute METHOD="POST"
• Set attribute ENCTYPE="multipart/form-data"
• Use INPUT element with TYPE="file" to create a file upload control (one
control per file)
• Hidden input field MAX_FILE_SIZE recommends to the web client the limit
of the size of the uploaded file.
The $_FILES Array
Index Meaning
name The original name of the file (as it was on the
user's computer).
type The MIME type of the file, as provided by the
browser.
size The size of the uploaded file in bytes.
tmp_name The temporary filename of the uploaded file
as it was stored on the server.
error The error code associated with any problem.
Processing the uploaded
items
// "upload" is the name assigned to the input element, as in
// <input type="file" name="upload" />
if (isset($_FILES['upload'])) {
if ($_FILES['upload']['error'] > 0)) {
// File upload fails. See next slide for detailed info about the
// meaning of the error code.
}
else {
// e.g., only allows JPEG image files to be uploaded
// Note: This is not a complete list of MIME types for JPEG images
$allowed = array('image/jpeg', 'image/jpg');
// Continue next page …
Processing the uploaded items
if (in_array($_FILES['upload']['type'], $allowed))
{
$tmp = $_FILES['upload']['tmp_name'];
$dst = "C:/uploads/{$_FILES['upload']['name']}";
if (move_upload_file($tmp, $dst)) {
// Success !
}
}
} // End of else
}
Error Messages Explained
UPLOAD_ERR_OK
Value: 0; There is no error, the file uploaded with success.
UPLOAD_ERR_INI_SIZE
Value: 1; The uploaded file exceeds the upload_max_filesize directive in
php.ini.
UPLOAD_ERR_FORM_SIZE
Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was
specified in the HTML form.
UPLOAD_ERR_PARTIAL
Value: 3; The uploaded file was only partially uploaded.
UPLOAD_ERR_NO_FILE
Value: 4; No file was uploaded.

More Related Content

PPTX
Uploading a file with php
PPTX
Php.ppt
PPTX
File upload php
PPT
JavaScript Tutorial
PDF
Chap 4 PHP.pdf
PPT
Js ppt
PPT
MYSQL - PHP Database Connectivity
Uploading a file with php
Php.ppt
File upload php
JavaScript Tutorial
Chap 4 PHP.pdf
Js ppt
MYSQL - PHP Database Connectivity

What's hot (20)

PPT
Php with MYSQL Database
PPT
Working with Databases and MySQL
ODP
Introduction of Html/css/js
PPTX
PPTX
Java loops for, while and do...while
PPTX
Document Object Model
PPTX
Dom(document object model)
PDF
4.2 PHP Function
PPT
Oops concepts in php
PPTX
Python/Flask Presentation
PDF
Object-oriented Programming-with C#
PPTX
Html5 tutorial for beginners
PPTX
PDF
Introduction to php
PPT
JavaScript Control Statements I
PDF
JavaScript Programming
PPT
Asp.net control
Php with MYSQL Database
Working with Databases and MySQL
Introduction of Html/css/js
Java loops for, while and do...while
Document Object Model
Dom(document object model)
4.2 PHP Function
Oops concepts in php
Python/Flask Presentation
Object-oriented Programming-with C#
Html5 tutorial for beginners
Introduction to php
JavaScript Control Statements I
JavaScript Programming
Asp.net control
Ad

Similar to File Uploading in PHP (20)

PPT
File Upload
PDF
Web Development Course: PHP lecture 4
PPTX
PHP fundamnetal in information technology CHapter -02.pptx
PPTX
lecture 11.pptx
PDF
File system
PDF
Introduction to php web programming - get and post
PPTX
BITM3730 10-24.pptx
PPTX
BITM3730 10-25.pptx
PPTX
Chapter 6 Getting Data from the Client (1).pptx
PPT
Chapter 08 php advance
DOCX
Php advance
ODT
PDF
Php File Upload
PPTX
Image upload in php MySql
ODT
PDF
WEB-MODULE 4.pdf
PPTX
2-Chapter Edit.pptx debret tabour university
PDF
PHP Web Development
PPTX
5. Formshcfsjhfajkjsfjsjfjksafjsfjkjfhjsafjsajkgfjskafkjas.pptx
ODP
Php File Upload
File Upload
Web Development Course: PHP lecture 4
PHP fundamnetal in information technology CHapter -02.pptx
lecture 11.pptx
File system
Introduction to php web programming - get and post
BITM3730 10-24.pptx
BITM3730 10-25.pptx
Chapter 6 Getting Data from the Client (1).pptx
Chapter 08 php advance
Php advance
Php File Upload
Image upload in php MySql
WEB-MODULE 4.pdf
2-Chapter Edit.pptx debret tabour university
PHP Web Development
5. Formshcfsjhfajkjsfjsjfjksafjsfjkjfhjsafjsajkgfjskafkjas.pptx
Php File Upload
Ad

File Uploading in PHP

  • 1. File Uploading in PHP Shoaib Yasin Idrees Hussain Maqbool Ali
  • 2. HTTP File Upload (RFC 1867) <form action="file_upload.php" method="post" enctype="multipart/form-data"> “No characters are encoded. This value is required when you are using forms that have a file upload control” <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> <input type="file" name="upload" /> <input type="submit" value="Upload" /> </form> • Set attribute METHOD="POST" • Set attribute ENCTYPE="multipart/form-data" • Use INPUT element with TYPE="file" to create a file upload control (one control per file) • Hidden input field MAX_FILE_SIZE recommends to the web client the limit of the size of the uploaded file.
  • 3. The $_FILES Array Index Meaning name The original name of the file (as it was on the user's computer). type The MIME type of the file, as provided by the browser. size The size of the uploaded file in bytes. tmp_name The temporary filename of the uploaded file as it was stored on the server. error The error code associated with any problem.
  • 4. Processing the uploaded items // "upload" is the name assigned to the input element, as in // <input type="file" name="upload" /> if (isset($_FILES['upload'])) { if ($_FILES['upload']['error'] > 0)) { // File upload fails. See next slide for detailed info about the // meaning of the error code. } else { // e.g., only allows JPEG image files to be uploaded // Note: This is not a complete list of MIME types for JPEG images $allowed = array('image/jpeg', 'image/jpg'); // Continue next page …
  • 5. Processing the uploaded items if (in_array($_FILES['upload']['type'], $allowed)) { $tmp = $_FILES['upload']['tmp_name']; $dst = "C:/uploads/{$_FILES['upload']['name']}"; if (move_upload_file($tmp, $dst)) { // Success ! } } } // End of else }
  • 6. Error Messages Explained UPLOAD_ERR_OK Value: 0; There is no error, the file uploaded with success. UPLOAD_ERR_INI_SIZE Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini. UPLOAD_ERR_FORM_SIZE Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form. UPLOAD_ERR_PARTIAL Value: 3; The uploaded file was only partially uploaded. UPLOAD_ERR_NO_FILE Value: 4; No file was uploaded.