Filename:"> Filename:">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 7 How to Upload Files. A very useful aspect of PHP is its ability to manage file uploads to your server. Before you can use PHP to manage your uploads,

Similar presentations


Presentation on theme: "Unit 7 How to Upload Files. A very useful aspect of PHP is its ability to manage file uploads to your server. Before you can use PHP to manage your uploads,"— Presentation transcript:

1 Unit 7 How to Upload Files

2 A very useful aspect of PHP is its ability to manage file uploads to your server. Before you can use PHP to manage your uploads, you must first build an HTML form that lets users select a file to upload. See our HTML Form lesson for a more in- depth look at forms.

3 <form action="upload_file.php" method="post" enctype="multipart/form-data"> Filename:

4

5 enctype="multipart/form-data" - The enctype attribute of the tag specifies which content-type to use when submitting the form. input name="uploadedfile“ type=“file” - uploadedfile is how we will access the file in our PHP script.

6 When the upload_file.php file is executed, the uploaded file exists in a temporary storage area on the server. If the file is not moved to a different location it will be destroyed! To save our precious file we are going to need to make use of the $_FILES associative array. The $_FILES array is where PHP stores all the information about files. The $_FILES array contains an array of information about each file that is uploaded.

7 The handler script can access the information using the name of the uploaded file as the key. The $_FILES[‘file_name'] variable contains the following information for the uploaded file.

8 $_FILES['File_name']['tmp_name'] - directory on the web server where the file is temporarily stored. $_FILES['File_name']['name'] - name of the file on the user's system. $_FILES['File_name']['size'] - size of the file in bytes. $_FILES['File_name']['type'] - type of the file. $_FILES['File_name']['error'] - the error code associated with the file upload 0 - successful upload, 1 - file exceeds maximum upload size 2 - file exceeds maximum file size, 3 - file partially uploaded, 4 - no file uploaded

9 if ($_FILES["uploadedfile"]["error"] > 0) { echo "Error: ". $_FILES["uploadedfile"]["error"]. " "; } else { if (($_FILES["uploadedfile"]["type"] == "image/gif") || ($_FILES["uploadedfile"]["type"] == "image/jpeg") || ($_FILES["uploadedfile"]["type"] == "image/png" ) && ($_FILES["uploadedfile"]["size"] < 10000)) { move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], "C:/TEMP/". $_FILES["uploadedfile"]["name"]); echo "Upload: ". $_FILES["uploadedfile"]["name"]. " "; echo "Type: ". $_FILES["uploadedfile"]["type"]. " "; echo "Size: ". ($_FILES["uploadedfile"]["size"]/1024). " Kb "; echo "Stored in: ". $_FILES["uploadedfile"]["tmp_name"]; } else { echo "Files must be either JPEG, GIF, or PNG and less than 10,000 kb"; }

10 Move_uploaded_file():- The move_uploaded_file() function moves an uploaded file to a new location. Syntax:- move_uploaded_file(file,newloc) file:- Specifies the file to be moved newloc:- Specifies the new location for the file E.X: move_uploaded_file($_FILES["file_name"] ["tmp_name"], "C:/TEMP/". $_FILES["file_name"]["name"])


Download ppt "Unit 7 How to Upload Files. A very useful aspect of PHP is its ability to manage file uploads to your server. Before you can use PHP to manage your uploads,"

Similar presentations


Ads by Google