Home > Backend Development > PHP Tutorial > Principle analysis and code of PHP image file upload

Principle analysis and code of PHP image file upload

WBOY
Release: 2016-07-25 09:00:59
Original
1449 people have browsed it
PHP code implements the uploading of files or pictures. It analyzes the principle of uploading and gives specific codes. It is recommended that beginners learn it as a reference.

1. Create a file upload form 2. Allowing users to upload files from a form is very useful.

1. html form file

<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
Copy after login

Instructions:

The enctype attribute of the tag specifies which content type to use when submitting the form. Use "multipart/form-data" when your form requires binary data, such as file content. 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. Please allow only trusted users to perform file upload operations.

2. Upload files "upload_file.php" code for uploading files:

<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>
Copy after login

Instructions: 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, and the second subscript can be "name", "type", "size", "tmp_name" or "error". $_FILES["file"]["name"] - the name of the file being uploaded $_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

3. Increase upload limit Added restrictions on file uploads. Users can only upload .gif or .jpeg files, which must be less than 20 kb in size:

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
}
else
{
echo "Invalid file";
}
?>
Copy after login

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

4. Save the uploaded file The above example creates a temporary copy of the uploaded file in the server's PHP temporary folder. This temporary copy will disappear when the script ends. To save the uploaded file, we need to copy it to another location:

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
Copy after login


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template