Experience summary: Sample PHP file upload code_PHP tutorial
I read a book about PHP today, which taught me how to upload files in PHP. The two most important functions are move_uploade_file (temporary file, target location and file name) and is_uploaded_file(), the former It is used to move files saved in the server cache area after uploading to the target file. The latter is used to determine whether the file is uploaded successfully. In addition to the above two functions, it is also necessary to explain that the value of enctype in the form tag should be as follows:
<ol class="dp-xml"><li class="alt"><span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>formenctype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>formenctype</FONT></STRONG></SPAN><SPAN>="multipart/form-data"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>method</FONT></SPAN><SPAN>="post"</SPAN><SPAN class=attribute><FONT color=#ff0000>name</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"upform"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span> </span></span></li></ol>
Only when its value is multipart/form-data can the file be uploaded in the correct encoding method. "file" in the input tag type attribute
<ol class="dp-xml"><li class="alt"><span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>inputname</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>inputname</FONT></STRONG></SPAN><SPAN>="upfile"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>type</FONT></SPAN><SPAN>="file"</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span> </span></span></li></ol>
PHP file upload code class saveupload.php
<ol class="dp-xml"><li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>if(is_uploaded_file($_FILES['upfile']['tmp_name'])){ </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>upfile</FONT></SPAN><SPAN>=$_FILES["upfile"];//如果已经选定了要上传的文件,将其索引保存在$upfile中 </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>//分别去上传文件的名字,类型等 </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>name</FONT></SPAN><SPAN>=$upfile["name"]; </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>type</FONT></SPAN><SPAN>=$upfile["type"]; </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>size</FONT></SPAN><SPAN>=$upfile["size"]; </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>tmp_name</FONT></SPAN><SPAN>=$upfile["tmp_name"]; </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>error</FONT></SPAN><SPAN>=$upfile["error"]; </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>//设定上传文件类型 </SPAN><LI class=alt><SPAN>switch($type){ </SPAN><LI class=""><SPAN>case'image/pjpeg': </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>ok</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>1</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=""><SPAN>break; </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>case'image/jpeg': </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>ok</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>1</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=""><SPAN>break; </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>case'image/png': </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>ok</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>1</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=""><SPAN>break; </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>case'image/gif': </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>ok</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>1</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=""><SPAN>break; </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>//如果文件类型合法并且$error返回值为0,说明上传成功 </SPAN><LI class=alt><SPAN>if($ok&&$</SPAN><SPAN class=attribute><FONT color=#ff0000>error</FONT></SPAN><SPAN>=='0'){ </SPAN></SPAN><LI class=""><SPAN>move_uploaded_file($tmp_name,'up/'.$name);//将保存在缓存的文件移动到指定目录下 </SPAN><LI class=alt><SPAN>echo"上传成功"; </SPAN><LI class=""><SPAN>} </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span> </li></ol>
PHP file upload code upload Page upload.php
<ol class="dp-xml"> <li class="alt"><span><span><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span><span> </span></span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>htmlxmlns</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>htmlxmlns</FONT></STRONG></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"http://www.w3.org/1999/xhtml"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""> <span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>metahttp-equiv</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>metahttp-equiv</FONT></STRONG></SPAN><SPAN>="Content-Type"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>content</FONT></SPAN><SPAN>="text/html;</SPAN><SPAN class=attribute><FONT color=#ff0000>charset</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>utf</FONT></SPAN><SPAN>-8"</SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></font></strong></span><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>title</SPAN><SPAN class=tag>></span></font></strong><span>upload</span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>title</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""> <span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>styletype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>styletype</FONT></STRONG></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"text/css"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span> </span> </li> <li class="alt"> <span><!-- </SPAN><LI class=""><SPAN>body{ </SPAN><LI class=alt><SPAN>background-color:#CFF; </SPAN><LI class=""><SPAN>text-align:center; </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN>--</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>style</SPAN><SPAN class=tag>></span><span class="tag"></</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""><span>文件上传 </span></li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>hr</SPAN><SPAN class=tag>/></span></font></strong><span> </span> </li> <li class=""> <span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>formid</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>formid</FONT></STRONG></SPAN><SPAN>="form1"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>name</FONT></SPAN><SPAN>="form1"</SPAN><SPAN class=attribute><FONT color=#ff0000>method</FONT></SPAN><SPAN>="post"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>action</FONT></SPAN><SPAN>="saveupload.php"</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 class=tag><STRONG><FONT color=#006699>></font></strong></span><span> </span> </li> <li class="alt"><span>上传文件: </span></li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>label</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>inputtype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>inputtype</FONT></STRONG></SPAN><SPAN>="file"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>name</FONT></SPAN><SPAN>="upfile"</SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></font></strong></span><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>label</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>label</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""> <span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>inputtype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>inputtype</FONT></STRONG></SPAN><SPAN>="submit"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>name</FONT></SPAN><SPAN>="button"</SPAN><SPAN class=attribute><FONT color=#ff0000>id</FONT></SPAN><SPAN>="button"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>value</FONT></SPAN><SPAN>="上传"</SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></font></strong></span><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>label</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""> <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> <li class="alt"><span> </span></li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> </ol>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
