Home  >  Article  >  Backend Development  >  PHP file upload form ~~Study notes_PHP tutorial

PHP file upload form ~~Study notes_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:02:39923browse

PHP file upload form ~~Study Notes

PHP file upload
Through PHP, files can be uploaded to the server.
-------------------------------------------------- -------------------------------------------------- ---------------
Create a file upload form: useful when allowing users to upload files from a form;

Below is an html form for uploading files:

<html>
<body>



</body>
</html>

The enctype attribute of the
tag specifies which content type to use when submitting the form. When a form requires binary data, such as file content, use "multipart/form-data".
The type="file" attribute of the tag specifies that the input should be processed as a file. For example, when previewing in a browser, you'll see a browse button next to the input box.

Note: Allowing users to upload files is a huge security risk. Only allow trusted users to perform operations on files.
-------------------------------------------------- -------------------------------------------------- ---------------
Create upload script:
The "upload_file.php" file contains the code for uploading files:

<?php
<span style="white-space:pre">	</span>if($_FILES["file"]["error"] > 0) {
<span style="white-space:pre">		</span>echo "Upload Error: ". $_FILES["file"]["error"] . "<br />";
<span style="white-space:pre">	</span>}else {
<span style="white-space:pre">		</span>echo "Upload : ". $_FILES["file"]["name"] . "<br />";
<span style="white-space:pre">		</span>echo "Type : ". $_FILES["file"]["type"] . "<br />";
<span style="white-space:pre">		</span>echo "Size : ". $_FILES["file"]["size"]/1024 . " kb<br />";
<span style="white-space:pre">		</span>echo "Store in : ". $_FILES["file"]["tmp_name"] . "<br />" ;
<span style="white-space:pre">	</span>}
?>

By using PHP's global array $_FILES, you can upload files from the client computer to a remote server.

The first parameter is the input name of the form, the second parameter can be: "name", "type", "size", "tmp_name" or "error". just like this:
$_FILES["file"]["name"] - the name of the uploaded file
$_FILES["file"]["type"] - the type of file being uploaded
$_FILES["file"]["size"] - Size of the uploaded file, in bytes
$_FILES["file"]["tmp_name"] - The name of the temporary copy of the file stored on the server
$_FILES["file"]["error"] - error code caused by file upload

This is a very simple way to upload files. For security reasons, you should add restrictions on who has permission to upload files.

In this script, we add restrictions on file uploads. Users can only upload .gif or .jpeg files, and the file size must be less than 20 kb:

<?php

<span style="white-space:pre">	</span>if ((($_FILES["file"]["type"] == "image/gif")
<span style="white-space:pre">	</span>|| ($_FILES["file"]["type"] == "image/jpeg")
<span style="white-space:pre">	</span>|| ($_FILES["file"]["type"] == "image/pjpeg"))
<span style="white-space:pre">	</span>&& ($_FILES["file"]["size"] < 20000)) {
<span style="white-space:pre">		</span>if ($_FILES["file"]["error"] > 0) {
<span style="white-space:pre">			</span>echo "Error: " . $_FILES["file"]["error"] . "<br />";
<span style="white-space:pre">		</span>}else {
<span style="white-space:pre">			</span>echo "Upload: " . $_FILES["file"]["name"] . "<br />";
<span style="white-space:pre">			</span>echo "Type: " . $_FILES["file"]["type"] . "<br />";
<span style="white-space:pre">			</span>echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
<span style="white-space:pre">			</span>echo "Stored in: " . $_FILES["file"]["tmp_name"];
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}else {
<span style="white-space:pre">		</span>echo "Invalid file";
<span style="white-space:pre">	</span>}

?>

Note: For IE, the type to recognize jpg files must be pjpeg, and for FireFox, it must be jpeg.

<?php
<span style="white-space:pre">	</span>if ((($_FILES["file"]["type"] == "image/gif")
<span style="white-space:pre">	</span>|| ($_FILES["file"]["type"] == "image/jpeg")
<span style="white-space:pre">	</span>|| ($_FILES["file"]["type"] == "image/pjpeg"))
<span style="white-space:pre">	</span>&& ($_FILES["file"]["size"] < 20000)) {
<span style="white-space:pre">		</span>if ($_FILES["file"]["error"] > 0) {
<span style="white-space:pre">			</span>echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
<span style="white-space:pre">		</span>}else {
<span style="white-space:pre">			</span>echo "Upload: " . $_FILES["file"]["name"] . "<br />";
<span style="white-space:pre">			</span>echo "Type: " . $_FILES["file"]["type"] . "<br />";
<span style="white-space:pre">			</span>echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
<span style="white-space:pre">			</span>echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

<span style="white-space:pre">			</span>if (file_exists("upload/" . $_FILES["file"]["name"])) {
<span style="white-space:pre">				</span>echo $_FILES["file"]["name"] . " already exists. ";
<span style="white-space:pre">			</span>}else {
<span style="white-space:pre">				</span>move_uploaded_file($_FILES["file"]["tmp_name"],
<span style="white-space:pre">				</span>"upload/" . $_FILES["file"]["name"]);
<span style="white-space:pre">				</span>echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}else {
<span style="white-space:pre">		</span>echo "Invalid file";
<span style="white-space:pre">	</span>}
?>

The above script detects whether the file already exists, and if it does not exist, copies the file to the specified folder.

Note: This example saves the file to a new folder named "upload".

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/969604.htmlTechArticlePHP file upload form~~Study notes PHP file upload Through PHP, files can be uploaded to the server. -------------------------------------------------- --------------------------...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn