0% found this document useful (0 votes)
3 views

WT PPT

The document provides an overview of handling file uploads in PHP, detailing the process from HTML form creation to server-side processing. It explains the use of the global variable $_FILES and necessary PHP configurations in the php.ini file for enabling file uploads. Additionally, it includes example code for an upload form and a PHP script to manage file uploads effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

WT PPT

The document provides an overview of handling file uploads in PHP, detailing the process from HTML form creation to server-side processing. It explains the use of the global variable $_FILES and necessary PHP configurations in the php.ini file for enabling file uploads. Additionally, it includes example code for an upload form and a PHP script to manage file uploads effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

HANDLING FILE UPLOADS

WEB TECHNOLOGIES

LIKITHA
BHOOMPALLY
237R1A0511
CSE-A
WHAT IS PHP?

• PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-


used open source general-purpose scripting language that is
especially suited for web development and can be embedded into
HTML.
• PHP is used for creating dynamic and interactive Web pages.
• PHP was introduced by Rasmus Lerdorf in 1994 .
WHAT IS A FILE IN PHP?

• A file is a collection of data stored on a computer or server. Files


can be text files (e.g., .txt, .csv) or binary files
(e.g., .jpg, .png, .pdf).
• PHP files can contain text, HTML, CSS, JavaScript, and PHP
code
• PHP code are executed on the server, and the result is
returned to the browser as plain HTML
• PHP files have extension ".php"
HANDLING FILE UPLOADS

• File uploads are a fundamental aspect of web applications, allowing


users to upload images, documents, videos, or any other type of
files.
• In web development, handling file uploads involves multiple aspects,
including HTML form design, server-side processing, security
measures, and storage management.

• A PHP script can be used with a HTML form to allow users to upload
files to the server.
• Initially files are uploaded into a temporary directory and then relocated to a target
destination by a PHP script.

• There is one global PHP variable called $_FILES.

• A file upload feature enables users to send files from their local system to a
web server. The process typically involves:

1.Frontend (Client-Side): A form with an <input type="file"> element to


allow file selection.
2.Backend (Server-Side): A server that processes the uploaded file and stores
it.
3.Storage: The file is either stored on the server itself or on cloud storage
• PHP would create following five variables −
• $_FILES['file']['tmp_name'] − the uploaded file in the temporary directory
on the web server.
• $_FILES['file']['name'] − the actual name of the uploaded file.
• $_FILES['file']['size'] − the size in bytes of the uploaded file.
• $_FILES['file']['type'] − the MIME type of the uploaded file.
• $_FILES['file']['error'] − the error code associated with this file upload.
Before handling file uploads in PHP, the server must be properly configured. This
is done using the php.ini configuration file.
•To enable file uploads, ensure the following line is present and set to On in
php.ini:
“ file_uploads = On “(Allows PHP scripts to accept
uploaded file)

File uploads must be sent via an HTML form using the POST method.
•The POST method is required because:
•It allows sending binary data (files).
•The GET method does not support file uploads as it is used for retrieving data.
upload_tmp_dir="C:\XAMPP\tmp“
• Specifies the temporary directory where uploaded files are stored before they are
moved to a permanent location.

upload_max_filesize=40M
•Defines the maximum file size allowed for an uploaded file.
•In this case, it is set to 40MB.

max_file_uploads=20
•Defines the maximum number of files that can be uploaded in a single request.
•Here, PHP allows up to 20 files to be uploaded at once.
EXAMPLE

Uploadform.html
<html><body>
<form action="uploader.php" method="post" enctype="multipart/form-
data">
Select File:
<input type="file" name="fileToUpload"/>
<input type="submit" value="Upload File" name="submit"/>
</form>
</body>
</html>
Uploader.php
<?php
$target_path = "e:/";
$target_path = $target_path.basename( $_FILES['fileToUpload']['name']);
if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_path))
{
echo "File uploaded successfully!";
} else{
echo "Sorry, file not uploaded, please try again!";
}
?>
BASIC HTML CODE

•The input type="file" allows


<form action="/upload" users to choose a file.
method="post"
enctype="multipart/form-data"> •The method="post" is used
<label for="file">Choose a because file uploads cannot be
file:</label> handled with GET requests.
<input type="file" name="file"
•The
id="file">
enctype="multipart/form-
<input type="submit" data" is necessary to send
value="Upload"> binary data.
</form>
CONCLUSION

• Handling file uploads in PHP is crucial for web


applications. It involves configuring PHP
settings, creating an upload form, processing
the file on the server, and implementing
security measures.
• Proper validation ensures secure and efficient
file management.
THANK YOU :)

You might also like