Home Backend Development PHP Tutorial Using file functions to implement PHP file upload case analysis_PHP tutorial

Using file functions to implement PHP file upload case analysis_PHP tutorial

Jul 15, 2016 pm 01:26 PM
php upload Who are you function use accomplish document case analysis computer

If you are a computer enthusiast and don’t know PHP, you are too behind the times. If you want to know the relevant knowledge of PHP, let’s take a look at the implementation of PHP file upload. This code is divided into two files, one is upload.html and the other is upload.php.

<ol class="dp-xml">
<li class="alt">
<span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>form</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>enctype</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"multipart/form-data"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>action</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"upload.php"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>method</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"post"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span> </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>input</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>type</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"hidden"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>name</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"max_file_size"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>value</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"100000"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span> </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>input</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>name</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"userfile"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>type</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"file"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span>  </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>input</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>type</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"submit"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>value</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"上传文件"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span> </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>form</SPAN><SPAN class=tag>></span></font></strong><span> </span>
</li>
</ol>
Copy after login

Among them, please note that
This is a tag, If we want to upload files, we must specify multipart/form-data, otherwise the server will not know what to do. It is worth noting that the hidden value field of the form option MAX_FILE_SIZE in the file upload.html can limit the size of the uploaded file by setting its Value. The value of MAX_FILE_SIZE is just a suggestion for browsers, and it can be easily bypassed. So don't rely on this value to limit browser restrictions. In fact, the maximum value for PHP file upload in PHP settings will not be invalid. But it's better to include MAX_FILE_SIZE in the form, as it saves users the trouble of spending time waiting for a large file to be uploaded only to find out that the file is too big.
<ol class="dp-xml">
<li class="alt"><span><span>upload.php  </span></span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">f</font></span><span>=&$HTTP_POST_FILES['Myfile'];  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">dest_dir</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">'uploads'</font></span><span>;//设定上传目录  </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">dest</font></span><span>=$dest_dir.'/'.date("ymd")."_".$f['name'];//设置文件名为日期加上文件名避免重复  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">r</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">move_uploaded_file</font></span><span>($f['tmp_name'],$dest);  </span>
</li>
<li class=""><span>chmod($dest, 0755);//设定上传的文件的属性 </span></li>
</ol>
Copy after login

or

<ol class="dp-xml"><li class="alt">
<span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>copy</SPAN></FONT></STRONG><SPAN>($_FILES[MyFile][tmp_name],$_FILES[MyFile][name]);</SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span>
</li></ol>
Copy after login

The contents of the $_FILES array in the above example are as follows. We assume that the name of the file upload field is userfile (the name can be named arbitrarily)

<ol class="dp-xml">
<li class="alt"><span><span>$_FILES['userfile']['name']//客户端机器文件的原名称。  </span></span></li>
<li class=""><span>$_FILES['userfile']['type'] //文件的 MIME 类型,需要浏览器提供该信息的支持,例如“image/gif”。  </span></li>
<li class="alt"><span>$_FILES['userfile']['size']// 已上传文件的大小,单位为字节。  </span></li>
<li class=""><span>$_FILES['userfile']['tmp_name'] //文件被上传后在服务端储存的临时文件名。  </span></li>
<li class="alt"><span>$_FILES['userfile']['error'] //和该文件上传相关的错误代码 </span></li>
</ol>
Copy after login

PHP file upload value analysis:
◆Value: 0; No error occurs and the file upload is successful.
◆Value: 1; The uploaded file exceeds the limit of the upload_max_filesize option in php.ini.
◆Value: 2; The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.
◆Value: 3; Only part of the file was uploaded.
◆Value: 4; No files were uploaded.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446562.htmlTechArticleIf you are a computer enthusiast and don’t know PHP, you are too behind the times. If you want to know about PHP Knowledge, let’s take a look at the implementation of PHP file upload. This code is divided into...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to fine-tune deepseek locally How to fine-tune deepseek locally Feb 19, 2025 pm 05:21 PM

Local fine-tuning of DeepSeek class models faces the challenge of insufficient computing resources and expertise. To address these challenges, the following strategies can be adopted: Model quantization: convert model parameters into low-precision integers, reducing memory footprint. Use smaller models: Select a pretrained model with smaller parameters for easier local fine-tuning. Data selection and preprocessing: Select high-quality data and perform appropriate preprocessing to avoid poor data quality affecting model effectiveness. Batch training: For large data sets, load data in batches for training to avoid memory overflow. Acceleration with GPU: Use independent graphics cards to accelerate the training process and shorten the training time.

deepseek image generation tutorial deepseek image generation tutorial Feb 19, 2025 pm 04:15 PM

DeepSeek: A powerful AI image generation tool! DeepSeek itself is not an image generation tool, but its powerful core technology provides underlying support for many AI painting tools. Want to know how to use DeepSeek to generate images indirectly? Please continue reading! Generate images with DeepSeek-based AI tools: The following steps will guide you to use these tools: Launch the AI ​​Painting Tool: Search and open a DeepSeek-based AI Painting Tool (for example, search "Simple AI"). Select the drawing mode: select "AI Drawing" or similar function, and select the image type according to your needs, such as "Anime Avatar", "Landscape"

Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Mar 05, 2025 pm 05:57 PM

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

How to ask deepseek How to ask deepseek Feb 19, 2025 pm 04:39 PM

The search accuracy of DeepSeek depends on the way of asking questions. To achieve ideal results, the question should contain the following key elements: Clear topic: Specific specified information objectives. Specific angles: Cut into the topic from a specific perspective. Expected output form: define the desired result type (summary, analysis, list, etc.).

How to download deepseek How to download deepseek Feb 19, 2025 pm 05:45 PM

Make sure to access official website downloads and carefully check the domain name and website design. After downloading, scan the file. Read the protocol during installation and avoid the system disk when installing. Test the function and contact customer service to solve the problem. Update the version regularly to ensure the security and stability of the software.

How to use deepseek How to use deepseek Feb 19, 2025 pm 04:45 PM

DeepSeek is an efficient data search and analysis tool with an indexing mechanism at its core. Factors that affect their efficiency include: data type, data size, search strategy, and DeepSeek performance. High-quality data, refined search queries and sufficient computing resources are essential to optimize search results.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

See all articles