After a long period of development of PHP, many users know PHP very well. Here I will express my personal understanding and discuss with you. The support for PHP upload files is becoming more and more perfect. Before PHP 4.0.3 We can only use the copy() and is_uploaded_file() methods to upload files. Haha, in fact, we will also feel that PHP is much more convenient to upload files than ASP. This is the small reason why I chose PHP. A place of pride.
Example of PHP file upload:
<ol class="dp-xml"> <li class="alt"><span><span>if (!copy($file, $file.’.bak’)) { </span></span></li> <li class=""> <span>print (”failed to copy $file…</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span></font></strong><span>n”); </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>is_uploaded_file(string filename) </span></li> </ol>
Returns TRUE if the file given by filename is uploaded via HTTP POST.
<ol class="dp-xml"><li class="alt"><span><span>move_uploaded_file(string filename, string destination) </span></span></li></ol>
This function checks and ensures that the file specified by filename is a legal PHP upload file (that is, uploaded through PHP's HTTP POST upload mechanism). If the file is legal, it is moved to the file specified by destination. A conclusion can be drawn from the explanation of the three functions. move_uploaded_file() is a combination of the copy() and is_uploaded_file() functions. After talking about it for so long, you may still be a little unfamiliar with using these three functions. Here are two examples. : First, create a front-end page named index.html.
<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>=”multipart/form-data” </SPAN><SPAN class=attribute><FONT color=#ff0000>action</FONT></SPAN><SPAN>=”_URL_” </SPAN><SPAN class=attribute><FONT color=#ff0000>method</FONT></SPAN><SPAN>=”post”</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span> </span> </li> <li class=""> <span>Send this file: </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>=”filename” </SPAN><SPAN class=attribute><FONT color=#ff0000>type</FONT></SPAN><SPAN>=”file”</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>type</FONT></SPAN><SPAN>=”submit” </SPAN><SPAN class=attribute><FONT color=#ff0000>value</FONT></SPAN><SPAN>=”Send File”</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>form</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> </ol>
1. The following is about the copy() and is_uploaded_file() methods for uploading files (save as: copy.php. To upload files, you need to change _URL_ in index.html to copy. 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>$</SPAN><SPAN class=attribute><FONT color=#ff0000>path</FONT></SPAN><SPAN>=”./uploadfiles/”;//上传文件的路径 </SPAN></SPAN><LI class=alt><SPAN>if(is_uploaded_file($filename)) </SPAN><LI class=""><SPAN>//特别注意这里,传递给is_uploaded_file的为$filename,可不要传递$_FILES[”filename”][”name”] </SPAN><LI class=alt><SPAN>{ </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>file1</FONT></SPAN><SPAN>=$_FILES[”filename”][”name”]; </SPAN></SPAN><LI class=alt><SPAN>//注意这里$_POST[]方法为PHP4.1.0及以上版本支持,PHP以下的版本要用$HTTP_POST_FILES[]方法 </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>file2</FONT></SPAN><SPAN>=$path.time().$file1; </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>flag</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>1</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=""><SPAN>} </SPAN><LI class=alt><SPAN>if($flag) $</SPAN><SPAN class=attribute><FONT color=#ff0000>result</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>copy</FONT></SPAN><SPAN>($file1,$file2); </SPAN></SPAN><LI class=""><SPAN>if($result) echo “上传成功!”; </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span> </li></ol>
2. The following is about the method of uploading files using the move_uploaded_file() method (save as: move.php. To upload files, you need to change the _URL_ in index.html to move.php)
<ol class="dp-xml"><li class="alt"><span><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN><SPAN> </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>path</SPAN><SPAN>=”./uploadfiles/”;//上传路径 </SPAN></SPAN><LI class=alt><SPAN>if($_FILES[”filename”][”name”]) </SPAN><LI class=""><SPAN>{ </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>file1</SPAN><SPAN>=$_FILES[”filename”][”name”]; </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>file2</SPAN><SPAN> = $path.time().$file1; </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>flag</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>1</SPAN><SPAN>; </SPAN></SPAN><LI class=""><SPAN>} </SPAN><LI class=alt><SPAN>if($flag) $</SPAN><SPAN class=attribute>result</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>move_uploaded_file</SPAN><SPAN>($_FILES[”filename”][”tmp_name”],$file2); </SPAN></SPAN><LI class=""><SPAN>//特别注意这里传递给move_uploaded_file的第一个参数为上传到服务器上的临时文件 </SPAN><LI class=alt><SPAN>if($result) echo “上传成功!”; </SPAN><LI class=""><SPAN></SPAN><SPAN class=tag>?></span><span> </span></span></li></ol>
That’s it for file uploading. These are just two small programs. In fact, the codes required to complete file uploading are not just these. There are generally file size limits, file extension selection, and other security solutions. That’s all I wrote, everyone is interested in writing a complete file upload system. Haha!!!