Uploading a file with php

2,048 views
1,967 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,048
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
21
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Uploading a file with php

  1. 1. Uploading a file with PHPChapter 3 – Continue..
  2. 2. Uploading a file with PHP• To upload a file you need a form for the userto select the file. Create a new HTML pagecalled upload.php (its actually HTML so youdont need the PHP element):
  3. 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 sentby the form. Setting the field TYPE to file givesa button to launch the browsers file dialog.
  4. 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. 5. Uploading a file with PHP• $_FILES is the super global which holdsinformation about all uploaded files. It is anassociative array which holds a number of otherarrays (each one of these holds informationabout a single file). You need to tell PHP which ofthe files you want using the appropriate key (thename of the form file field). In this case there isonly one file called file01 (because that was thename in the form). The PRINT_R is there for youto see the structure of the array and is notneeded.
  6. 6. Uploading a file with PHP• When the form is submitted to the server the fileis uploaded. It is placed in a temporary locationand information about it is stored in $_FILES. Themiddle two lines set up some variables. The firstholds the name of the file which was uploaded.The second one holds the name it has been giventemporarily.• The built-in PHP function move_uploaded_file()moves the temporary file to its intended locationand renames it. Normally that would be in aspecial "uploads" directory for security.
  7. 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. 8. Uploading a file with PHP• You should be able to spot:– an IF statement which only allows the upload if a fileis less than a certain size and is a text file– an IF ELSE statement which checks whether theupload worked or not and gives a message in eachcase• Allowing uploads on your site is potentiallydangerous as there is no control over what isuploaded or by who. By adding limits on the filesize or type and allowing only logged on users toaccess the page risks can be reduced.

×