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 array
Php arrayPhp array
Php array
 
Php string function
Php string function Php string function
Php string function
 
php
phpphp
php
 
PHP
PHPPHP
PHP
 
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
PHP - Introduction to File Handling with PHP
PHP -  Introduction to  File Handling with PHPPHP -  Introduction to  File Handling with PHP
PHP - Introduction to File Handling with PHP
 
Php forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligetiPhp forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligeti
 
Intro to html 5
Intro to html 5Intro to html 5
Intro to html 5
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
 
PHP Form Validation Technique
PHP Form Validation TechniquePHP Form Validation Technique
PHP Form Validation Technique
 
PHP variables
PHP  variablesPHP  variables
PHP variables
 
Php operators
Php operatorsPhp operators
Php operators
 
PHP Basic & Variables
PHP Basic & VariablesPHP Basic & Variables
PHP Basic & Variables
 
PHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginnersPHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginners
 
Callback Function
Callback FunctionCallback Function
Callback Function
 

Destaque (17)

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
 
Php Presentation
Php PresentationPhp Presentation
Php Presentation
 

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

TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 

Último (20)

TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 

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.