PHP については学ぶ価値のあることがたくさんあります。ここでは主に PHP アップロード ソリューションを紹介します。この記事から多くのことを学んでいただければ幸いです。 WEBページにPHPのコマンドコードを直接入力できるため、特別な開発環境は必要ありません。 WEB ページでは、すべての PHP コードは「」内に配置されます。さらに、ユーザーは などのフォームの使用を選択できます。 PHP エンジンは、ページ内の 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><LI class=alt><SPAN>*Fileuploadclass </SPAN><LI class=""><SPAN>*@version1.0.0(ThuAug1801:32:39CST2005) </SPAN><LI class=alt><SPAN>*@authorsanshi </SPAN><LI class=""><SPAN>*/ </SPAN><LI class=alt><SPAN>classupLoad </SPAN><LI class=""><SPAN>{ </SPAN><LI class=alt><SPAN>/** </SPAN><LI class=""><SPAN>* </SPAN><LI class=alt><SPAN>*@authorsanshi </SPAN><LI class=""><SPAN>*@version1.0.0ThuAug1801:00:18CST2005 </SPAN><LI class=alt><SPAN>*@paramstring$info文件内容 </SPAN><LI class=""><SPAN>*@paramstring$fileName生成的文件名 </SPAN><LI class=alt><SPAN>*@returnboolean建立成功返回true </SPAN><LI class=""><SPAN>*@deprecated </SPAN><LI class=alt><SPAN>*建立html文件 </SPAN><LI class=""><SPAN>*/ </SPAN><LI class=alt><SPAN>functioncreateHtml($info,$fileName) </SPAN><LI class=""><SPAN>{ </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN>/** </SPAN><LI class=alt><SPAN>* </SPAN><LI class=""><SPAN>*@authorsanshi </SPAN><LI class=alt><SPAN>*@version1.0.0ThuAug1801:03:09CST2005 </SPAN><LI class=""><SPAN>*@returnvoid </SPAN><LI class=alt><SPAN>*@deprecated </SPAN><LI class=""><SPAN>*构造函数 </SPAN><LI class=alt><SPAN>*/ </SPAN><LI class=""><SPAN>functiondownLoad() </SPAN><LI class=alt><SPAN>{} </SPAN><LI class=""><SPAN>/** </SPAN><LI class=alt><SPAN>* </SPAN><LI class=""><SPAN>*@authorsanshi </SPAN><LI class=alt><SPAN>*@version1.0.0ThuAug1801:03:55CST2005 </SPAN><LI class=""><SPAN>*@paramstring$fileField在表单中的字段名 </SPAN><LI class=alt><SPAN>*@paramstring$length限制的长度 </SPAN><LI class=""><SPAN>*@returnboolean成功返回true </SPAN><LI class=alt><SPAN>*@deprecated </SPAN><LI class=""><SPAN>*功能实现函数 </SPAN><LI class=alt><SPAN>*/ </SPAN><LI class=""><SPAN>functioninit($fileField,$</SPAN><SPAN class=attribute>length</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>''</SPAN><SPAN>) </SPAN></SPAN><LI class=alt><SPAN>{ </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>files</SPAN><SPAN>=$_FILES[$fileField]; </SPAN></SPAN><LI class=alt><SPAN>//用户名需要改动,根据自己的实际情况做改动 </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>userName</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>'sanshi'</SPAN><SPAN>; </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>fileName</SPAN><SPAN>=$files['name']; </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>fileType</SPAN><SPAN>=$files['type']; </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>fileTemp</SPAN><SPAN>=$files['tmp_name']; </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>fileSize</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>empty</SPAN><SPAN>($length)?($files['size']+10):$length; </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>fileError</SPAN><SPAN>=$files['error'];//这块也许php4中没有 </SPAN></SPAN><LI class=""><SPAN>//改为 </SPAN><LI class=alt><SPAN>//if($this-</SPAN><SPAN class=tag>></span><span>_isType($fileName)||$this-</span><span class="tag">></span><span>_isBig($length )) </span></span></li> <li class=""> <span>if(!$this-</span><span class="tag">></span><span>_isType($fileName)||$this-</span><span class="tag">></span><span>_isBig($length )||$fileError!=0) </span> </li> <li class="alt"><span>{ </span></li> <li class=""><span>//print_r($files); </span></li> <li class="alt"><span>returnfalse; </span></li> <li class=""><span>}else{ </span></li> <li class="alt"> <span>$</span><span class="attribute">path</span><span>=$this-</span><span class="tag">></span><span>_createDir($userName);//取得路径 </span> </li> <li class=""> <span>$</span><span class="attribute">createFileName</span><span>=$userName."_".time();//设置当前文件名 </span> </li> <li class="alt"> <span>$</span><span class="attribute">createFileType</span><span>=$this-</span><span class="tag">></span><span>getFileType($fileName);//设置文件类别 </span> </li> <li class=""><span>return@move_uploaded_file($fileTemp,$path.$createFileName.'.'.$createFileType)?true:false; </span></li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""><span>/** </span></li> <li class="alt"><span>* </span></li> <li class=""><span>*@authorsanshi </span></li> <li class="alt"><span>*@version1.0.0ThuAug1801:07:43CST2005 </span></li> <li class=""><span>*@paramint$length上传限制的大小 </span></li> <li class="alt"><span>*@returnboolean超过返回true </span></li> <li class=""><span>*@deprecated </span></li> <li class="alt"><span>*判断是否超过预定大小 </span></li> <li class=""><span>*/ </span></li> <li class="alt"><span>function_isBig($length) </span></li> <li class=""><span>{ </span></li> <li class="alt"> <span>$</span><span class="attribute">bigest</span><span>=</span><span class="attribute-value">''</span><span>; </span> </li> <li class=""> <span>return$big</span><span class="tag">></span><span>$bigest?true:false; </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>/** </span></li> <li class="alt"><span>* </span></li> <li class=""><span>*@authorsanshi </span></li> <li class="alt"><span>*@version1.0.0ThuAug1801:08:55CST2005 </span></li> <li class=""><span>*@paramstring$fileName文件名 </span></li> <li class="alt"><span>*@returnstring$fileType文件后缀 </span></li> <li class=""><span>*@deprecated </span></li> <li class="alt"><span>*取得文件后缀(只取得文件的最后一个后缀名) </span></li> <li class=""><span>*/ </span></li> <li class="alt"><span>functiongetFileType($fileName) </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>returnend(explode('.',$fileName)); </span></li> <li class=""><span>} </span></li> <li class="alt"><span>/** </span></li> <li class=""><span>* </span></li> <li class="alt"><span>*@authorsanshi </span></li> <li class=""><span>*@version1.0.0ThuAug1801:10:41CST2005 </span></li> <li class="alt"><span>*@paramstring$fileName文件名 </span></li> <li class=""><span>*@paramboolean$method是否检查多个后缀默认false </span></li> <li class="alt"><span>*@paramint$postFix后缀个数默认为2 </span></li> <li class=""><span>*@returnboolean存在返回true </span></li> <li class="alt"><span>*@deprecated </span></li> <li class=""><span>*检查文件的后缀是否在类别数组中,类别数组自己设置 </span></li> <li class="alt"><span>*如果$method设置为true则检查文件有几个后缀 </span></li> <li class=""><span>*/ </span></li> <li class="alt"> <span>function_isType($fileName,$</span><span class="attribute">method</span><span>=</span><span class="attribute-value">'false'</span><span>,$</span><span class="attribute">postFix</span><span>=</span><span class="attribute-value">2</span><span>) </span> </li> <li class=""><span>{ </span></li> <li class="alt"><span>//设置类别数组 </span></li> <li class=""> <span>$</span><span class="attribute">type</span><span>=</span><span class="attribute-value">array</span><span>('jpeg', </span> </li> <li class="alt"><span>'gif', </span></li> <li class=""><span>'bmp', </span></li> <li class="alt"><span>'exe'); </span></li> <li class=""> <span>$</span><span class="attribute">fileName</span><span>=</span><span class="attribute-value">strtolower</span><span>($fileName); </span> </li> <li class="alt"> <span>$</span><span class="attribute">fileTypeArray</span><span>=</span><span class="attribute-value">explode</span><span>('.',$fileName); </span> </li> <li class=""> <span>$</span><span class="attribute">fileType</span><span>=</span><span class="attribute-value">end</span><span>($fileTypeArray); </span> </li> <li class="alt"><span>//判断是否有一个文件有多个后缀 </span></li> <li class=""><span>if($method) </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span>if(count($fileTypeArray)</span><span class="tag">></span><span>(is_int($postFix)?$postFix:2)) </span> </li> <li class="alt"><span>{ </span></li> <li class=""><span>returnfalse; </span></li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"><span>returnin_array($fileType,$type); </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""><span>/** </span></li> <li class="alt"><span>* </span></li> <li class=""><span>*@authorsanshi </span></li> <li class="alt"><span>*@version1.0.0ThuAug1801:17:19CST2005 </span></li> <li class=""><span>*@paramstring$userName </span></li> <li class="alt"><span>*@returnstring$path </span></li> <li class=""><span>*@deprecated </span></li> <li class="alt"><span>*建立目录目录格式年/月/日/用户名/ </span></li> <li class=""><span>*权限为755 </span></li> <li class="alt"><span>*/ </span></li> <li class=""><span>function_createDir($userName) </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span>$</span><span class="attribute">root</span><span>=</span><span class="attribute-value">''</span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute">pathSign</span><span>=</span><span class="attribute-value">DIRECTORY_SEPARATOR</span><span>; </span> </li> <li class=""> <span>$</span><span class="attribute">y</span><span>=</span><span class="attribute-value">date</span><span>('Y').$pathSign; </span> </li> <li class="alt"> <span>$</span><span class="attribute">m</span><span>=</span><span class="attribute-value">date</span><span>('m').$pathSign; </span> </li> <li class=""> <span>$</span><span class="attribute">d</span><span>=</span><span class="attribute-value">date</span><span>('d').$pathSign; </span> </li> <li class="alt"> <span>$</span><span class="attribute">path</span><span>=$root.$y.$m.$d.$userName; </span> </li> <li class=""> <span>$</span><span class="attribute">dirArray</span><span>=</span><span class="attribute-value">explode</span><span>($pathSign,$path); </span> </li> <li class="alt"> <span>$</span><span class="attribute">tempDir</span><span>=</span><span class="attribute-value">''</span><span>; </span> </li> <li class=""><span>foreach($dirArrayas$dir) </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span>$</span><span class="attribute">tempDir.</span><span>=$dir.$pathSign; </span> </li> <li class="alt"> <span>$</span><span class="attribute">isFile</span><span>=</span><span class="attribute-value">file_exists</span><span>($tempDir); </span> </li> <li class=""><span>clearstatcache(); </span></li> <li class="alt"><span>if(!$isFile&&!is_dir($tempDir)) </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>@mkdir($tempDir,0755); </span></li> <li class=""><span>} </span></li> <li class="alt"><span>} </span></li> <li class=""><span>return$path.$pathSign; </span></li> <li class="alt"><span>} </span></li> <li class=""><span>/** </span></li> <li class="alt"><span>* </span></li> <li class=""><span>*@authorsanshi </span></li> <li class="alt"><span>*@version1.0.0ThuAug1801:19:32CST2005 </span></li> <li class=""><span>*@param string$dirName目录名 </span></li> <li class="alt"><span>*@return boolean可以操作返回true </span></li> <li class=""><span>*@deprecated </span></li> <li class="alt"><span>*判断操作是否在上传目录 </span></li> <li class=""><span>*/ </span></li> <li class="alt"><span>function_isDel($dirName) </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>//注意upLoadDir,一定要与真正使用目录相对应 </span></li> <li class=""> <span>$</span><span class="attribute">upLoadDir</span><span>=</span><span class="attribute-value">''</span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute">upLoadDir</span><span>=</span><span class="attribute-value">preg_replace</span><span>('/\//','/',$upLoadDir); </span> </li> <li class=""> <span>$</span><span class="attribute">format</span><span>=</span><span class="attribute-value">"/^{$upLoadDir}/"</span><span>; </span> </li> <li class="alt"><span>returnpreg_match($format,$dirName); </span></li> <li class=""><span>} </span></li> <li class="alt"><span>/** </span></li> <li class=""><span>* </span></li> <li class="alt"><span>*@authorsanshi </span></li> <li class=""><span>*@version1.0.0ThuAug1801:25:58CST2005 </span></li> <li class="alt"><span>*@paramstring$fileName文件名 </span></li> <li class=""><span>*@returnboolean删除文件成功返回true </span></li> <li class="alt"><span>*@deprecated </span></li> <li class=""><span>*删除文件 </span></li> <li class="alt"><span>*/ </span></li> <li class=""><span>functiondelFile($fileName) </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span>$</span><span class="attribute">cur_dir</span><span>=</span><span class="attribute-value">dirname</span><span>(trim($fileName)); </span> </li> <li class="alt"> <span>if($this-</span><span class="tag">></span><span>_isDel($cur_dir)) </span> </li> <li class=""><span>{ </span></li> <li class="alt"><span>return@unlink($fileName)?true:false; </span></li> <li class=""><span>}else{ </span></li> <li class="alt"><span>returnfalse; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>} </span></li> <li class=""><span>/** </span></li> <li class="alt"><span>* </span></li> <li class=""><span>*@authorsanshi </span></li> <li class="alt"><span>*@version1.0.0ThuAug1801:27:43CST2005 </span></li> <li class=""><span>*@paramstring$dieName目录名 </span></li> <li class="alt"><span>*@returnboolean删除成功返回true </span></li> <li class=""><span>*@deprecated </span></li> <li class="alt"><span>*删除目录目录下如果有文件不能删除 </span></li> <li class=""><span>*/ </span></li> <li class="alt"><span>functiondelDir($dirName) </span></li> <li class=""><span>{ </span></li> <li class="alt"> <span>if($this-</span><span class="tag">></span><span>_isDel($dirName)&&is_dir($dirName)) </span> </li> <li class=""><span>{ </span></li> <li class="alt"><span>return@rmdir($dirName)?true:false; </span></li> <li class=""><span>}else{ </span></li> <li class="alt"><span>returnfalse; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><span>} </span></li> <li class=""> <span></span><span class="tag">?></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN><SPAN> </SPAN></SPAN><LI class=""><SPAN>//使用 </SPAN><LI class=alt><SPAN>/* </SPAN><LI class=""><SPAN>include'upLoad.class.php'; </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>up</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>newupLoad</SPAN><SPAN>(); </SPAN></SPAN><LI class=""><SPAN>if($up-</SPAN><SPAN class=tag>></span><span>init("file")) </span> </li> <li class="alt"><span>{ </span></li> <li class=""><span>echo'success'; </span></li> <li class="alt"><span>}else{ </span></li> <li class=""><span>echo'failure'; </span></li> <li class="alt"><span>} </span></li> <li class=""><span>*/ </span></li> <li class="alt"> <span></span><span class="tag">?></span><span> </span> </li> </ol>