SlideShare uma empresa Scribd logo
1 de 8
Uploading a file with PHP
Chapter 3 – Continue..
Uploading a file with PHP
• To upload a file you need a form for the user
to select the file. Create a new HTML page
called upload.php (it's actually HTML so you
don't need the PHP element):
Uploading a file with PHP
<form enctype="multipart/form-data"
method="post" action="upload2.php">
<p><input type="file" name="file01" /></p>
<p><input type="submit" /></p>
</form>
• The ENCTYPE sets the type of data to be sent
by the form. Setting the field TYPE to file gives
a button to launch the browser's file dialog.
Uploading a file with PHP
• Then create upload2.php to process the file:
echo "<pre>";
print_r($_FILES);
echo "</pre>";
$oldname=$_FILES["file01"]["name"];
$tempname=$_FILES["file01"]["tmp_name"];
move_uploaded_file($tempname, $oldname);
Uploading a file with PHP
• $_FILES is the super global which holds
information about all uploaded files. It is an
associative array which holds a number of other
arrays (each one of these holds information
about a single file). You need to tell PHP which of
the files you want using the appropriate key (the
name of the form file field). In this case there is
only one file called 'file01' (because that was the
name in the form). The PRINT_R is there for you
to see the structure of the array and is not
needed.
Uploading a file with PHP
• When the form is submitted to the server the file
is uploaded. It is placed in a temporary location
and information about it is stored in $_FILES. The
middle two lines set up some variables. The first
holds the name of the file which was uploaded.
The second one holds the name it has been given
temporarily.
• The built-in PHP function move_uploaded_file()
moves the temporary file to its intended location
and renames it. Normally that would be in a
special "uploads" directory for security.
Uploading a file with PHP
• You can improve this upload page a lot:
$type=$_FILES['file01']['type'];
$size=$_FILES['file01']['size'];
$oldname=$_FILES['file01']['name'];
$tempname=$_FILES['file01']['tmp_name'];
if($size<=50000 && $type=="text/html") {
if (move_uploaded_file($tempname, $oldname)){
echo "<p>The file was uploaded successfully</p>";
} else {
echo "<p>Sorry, no good</p>";
}
} else {
echo "<p>Sorry that file cannot be uploaded.</p>";
}
Uploading a file with PHP
• You should be able to spot:
– an IF statement which only allows the upload if a file
is less than a certain size and is a text file
– an IF ELSE statement which checks whether the
upload worked or not and gives a message in each
case
• Allowing uploads on your site is potentially
dangerous as there is no control over what is
uploaded or by who. By adding limits on the file
size or type and allowing only logged on users to
access the page risks can be reduced.

Mais conteúdo relacionado

Mais procurados (20)

Php introduction
Php introductionPhp introduction
Php introduction
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
 
PHP slides
PHP slidesPHP slides
PHP slides
 
PHP variables
PHP  variablesPHP  variables
PHP variables
 
Sessions in php
Sessions in php Sessions in php
Sessions in php
 
PHP Cookies and Sessions
PHP Cookies and SessionsPHP Cookies and Sessions
PHP Cookies and Sessions
 
Php technical presentation
Php technical presentationPhp technical presentation
Php technical presentation
 
Php
PhpPhp
Php
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)
 
Php Presentation
Php PresentationPhp Presentation
Php Presentation
 
What Is Php
What Is PhpWhat Is Php
What Is Php
 
Beginners PHP Tutorial
Beginners PHP TutorialBeginners PHP Tutorial
Beginners PHP Tutorial
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
 
Php sessions
Php sessionsPhp sessions
Php sessions
 
Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHP
 
php
phpphp
php
 
Sending Email Basics PHP
Sending Email Basics PHPSending Email Basics PHP
Sending Email Basics PHP
 

Destaque

Destaque (16)

Php File Operations
Php File OperationsPhp File Operations
Php File Operations
 
php file uploading
php file uploadingphp file uploading
php file uploading
 
Music Downloading Website (HTML,CSS,PHP Presentation)
Music Downloading Website (HTML,CSS,PHP Presentation)Music Downloading Website (HTML,CSS,PHP Presentation)
Music Downloading Website (HTML,CSS,PHP Presentation)
 
Php File Upload
Php File UploadPhp File Upload
Php File Upload
 
Php file handling in Hindi
Php file handling in Hindi Php file handling in Hindi
Php file handling in Hindi
 
Chapter 08 php advance
Chapter 08   php advanceChapter 08   php advance
Chapter 08 php advance
 
Pdf's in PHP
Pdf's in PHPPdf's in PHP
Pdf's in PHP
 
Introduction to FPDF
Introduction to FPDFIntroduction to FPDF
Introduction to FPDF
 
PHP - Introduction to PHP Fundamentals
PHP -  Introduction to PHP FundamentalsPHP -  Introduction to PHP Fundamentals
PHP - Introduction to PHP Fundamentals
 
Reporting using FPDF
Reporting using FPDFReporting using FPDF
Reporting using FPDF
 
P2P Networks
P2P NetworksP2P Networks
P2P Networks
 
The FPDF Library
The FPDF LibraryThe FPDF Library
The FPDF Library
 
MySQL Database with phpMyAdmin
MySQL Database with  phpMyAdminMySQL Database with  phpMyAdmin
MySQL Database with phpMyAdmin
 
MySql:Exporting And Importing Data In Mysql
MySql:Exporting And Importing Data In MysqlMySql:Exporting And Importing Data In Mysql
MySql:Exporting And Importing Data In Mysql
 
File Upload
File UploadFile Upload
File Upload
 
PHP Project PPT
PHP Project PPTPHP Project PPT
PHP Project PPT
 

Semelhante a Uploading a file with php

PHP fundamnetal in information technology CHapter -02.pptx
PHP fundamnetal in information technology CHapter -02.pptxPHP fundamnetal in information technology CHapter -02.pptx
PHP fundamnetal in information technology CHapter -02.pptxworldchannel
 
lecture 11.pptx
lecture 11.pptxlecture 11.pptx
lecture 11.pptxITNet
 
Web Development Course: PHP lecture 4
Web Development Course: PHP  lecture 4Web Development Course: PHP  lecture 4
Web Development Course: PHP lecture 4Gheyath M. Othman
 
Up.Php
Up.PhpUp.Php
Up.Phpwsoom
 
Php File Upload
Php File UploadPhp File Upload
Php File Uploadsaeel005
 
Web application, cookies and sessions
Web application, cookies and sessionsWeb application, cookies and sessions
Web application, cookies and sessionshamsa nandhini
 
RSS Application Using Dom
RSS Application Using Dom  RSS Application Using Dom
RSS Application Using Dom abdullah roomi
 
Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3Gheyath M. Othman
 
05 File Handling Upload Mysql
05 File Handling Upload Mysql05 File Handling Upload Mysql
05 File Handling Upload MysqlGeshan Manandhar
 
Web app development_php_07
Web app development_php_07Web app development_php_07
Web app development_php_07Hassen Poreya
 
Chapter 6 Getting Data from the Client (1).pptx
Chapter 6 Getting Data from the Client (1).pptxChapter 6 Getting Data from the Client (1).pptx
Chapter 6 Getting Data from the Client (1).pptxAhmedKafi7
 
Class 6 - PHP Web Programming
Class 6 - PHP Web ProgrammingClass 6 - PHP Web Programming
Class 6 - PHP Web ProgrammingAhmed Swilam
 
Uploadifyv2 1-0manual-100417052228-phpapp01
Uploadifyv2 1-0manual-100417052228-phpapp01Uploadifyv2 1-0manual-100417052228-phpapp01
Uploadifyv2 1-0manual-100417052228-phpapp01Nut Jaya
 
PHP File Handling
PHP File Handling PHP File Handling
PHP File Handling Degu8
 

Semelhante a Uploading a file with php (20)

PHP fundamnetal in information technology CHapter -02.pptx
PHP fundamnetal in information technology CHapter -02.pptxPHP fundamnetal in information technology CHapter -02.pptx
PHP fundamnetal in information technology CHapter -02.pptx
 
lecture 11.pptx
lecture 11.pptxlecture 11.pptx
lecture 11.pptx
 
Web Development Course: PHP lecture 4
Web Development Course: PHP  lecture 4Web Development Course: PHP  lecture 4
Web Development Course: PHP lecture 4
 
File system
File systemFile system
File system
 
Introduction to php web programming - get and post
Introduction to php  web programming - get and postIntroduction to php  web programming - get and post
Introduction to php web programming - get and post
 
Up.Php
Up.PhpUp.Php
Up.Php
 
Php advance
Php advancePhp advance
Php advance
 
Php BASIC
Php BASICPhp BASIC
Php BASIC
 
Php File Upload
Php File UploadPhp File Upload
Php File Upload
 
File upload php
File upload phpFile upload php
File upload php
 
Web application, cookies and sessions
Web application, cookies and sessionsWeb application, cookies and sessions
Web application, cookies and sessions
 
RSS Application Using Dom
RSS Application Using Dom  RSS Application Using Dom
RSS Application Using Dom
 
Php
PhpPhp
Php
 
Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3
 
05 File Handling Upload Mysql
05 File Handling Upload Mysql05 File Handling Upload Mysql
05 File Handling Upload Mysql
 
Web app development_php_07
Web app development_php_07Web app development_php_07
Web app development_php_07
 
Chapter 6 Getting Data from the Client (1).pptx
Chapter 6 Getting Data from the Client (1).pptxChapter 6 Getting Data from the Client (1).pptx
Chapter 6 Getting Data from the Client (1).pptx
 
Class 6 - PHP Web Programming
Class 6 - PHP Web ProgrammingClass 6 - PHP Web Programming
Class 6 - PHP Web Programming
 
Uploadifyv2 1-0manual-100417052228-phpapp01
Uploadifyv2 1-0manual-100417052228-phpapp01Uploadifyv2 1-0manual-100417052228-phpapp01
Uploadifyv2 1-0manual-100417052228-phpapp01
 
PHP File Handling
PHP File Handling PHP File Handling
PHP File Handling
 

Mais de Muhamad Al Imran

Perbezaan ASWJ dengan Syiah
Perbezaan ASWJ dengan SyiahPerbezaan ASWJ dengan Syiah
Perbezaan ASWJ dengan SyiahMuhamad Al Imran
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Muhamad Al Imran
 
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)Muhamad Al Imran
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Muhamad Al Imran
 

Mais de Muhamad Al Imran (10)

Perbezaan ASWJ dengan Syiah
Perbezaan ASWJ dengan SyiahPerbezaan ASWJ dengan Syiah
Perbezaan ASWJ dengan Syiah
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
 
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
 
Discussion 2 q
Discussion 2 qDiscussion 2 q
Discussion 2 q
 
Lab5 tips
Lab5 tipsLab5 tips
Lab5 tips
 
Php i basic chapter 4
Php i basic chapter 4Php i basic chapter 4
Php i basic chapter 4
 
Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
 
Discussion2 tips
Discussion2 tipsDiscussion2 tips
Discussion2 tips
 
Discussion 1 q
Discussion 1 qDiscussion 1 q
Discussion 1 q
 

Último

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 

Último (20)

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

Uploading a file with php

  • 1. Uploading a file with PHP Chapter 3 – Continue..
  • 2. Uploading a file with PHP • To upload a file you need a form for the user to select the file. Create a new HTML page called upload.php (it's actually HTML so you don't need the PHP element):
  • 3. Uploading a file with PHP <form enctype="multipart/form-data" method="post" action="upload2.php"> <p><input type="file" name="file01" /></p> <p><input type="submit" /></p> </form> • The ENCTYPE sets the type of data to be sent by the form. Setting the field TYPE to file gives a button to launch the browser's file dialog.
  • 4. Uploading a file with PHP • Then create upload2.php to process the file: echo "<pre>"; print_r($_FILES); echo "</pre>"; $oldname=$_FILES["file01"]["name"]; $tempname=$_FILES["file01"]["tmp_name"]; move_uploaded_file($tempname, $oldname);
  • 5. Uploading a file with PHP • $_FILES is the super global which holds information about all uploaded files. It is an associative array which holds a number of other arrays (each one of these holds information about a single file). You need to tell PHP which of the files you want using the appropriate key (the name of the form file field). In this case there is only one file called 'file01' (because that was the name in the form). The PRINT_R is there for you to see the structure of the array and is not needed.
  • 6. Uploading a file with PHP • When the form is submitted to the server the file is uploaded. It is placed in a temporary location and information about it is stored in $_FILES. The middle two lines set up some variables. The first holds the name of the file which was uploaded. The second one holds the name it has been given temporarily. • The built-in PHP function move_uploaded_file() moves the temporary file to its intended location and renames it. Normally that would be in a special "uploads" directory for security.
  • 7. Uploading a file with PHP • You can improve this upload page a lot: $type=$_FILES['file01']['type']; $size=$_FILES['file01']['size']; $oldname=$_FILES['file01']['name']; $tempname=$_FILES['file01']['tmp_name']; if($size<=50000 && $type=="text/html") { if (move_uploaded_file($tempname, $oldname)){ echo "<p>The file was uploaded successfully</p>"; } else { echo "<p>Sorry, no good</p>"; } } else { echo "<p>Sorry that file cannot be uploaded.</p>"; }
  • 8. Uploading a file with PHP • You should be able to spot: – an IF statement which only allows the upload if a file is less than a certain size and is a text file – an IF ELSE statement which checks whether the upload worked or not and gives a message in each case • Allowing uploads on your site is potentially dangerous as there is no control over what is uploaded or by who. By adding limits on the file size or type and allowing only logged on users to access the page risks can be reduced.