1。一般上传文件都是通过form表单提交的,一般都是通过http协议。小编这里一定要试试ftp协议上传文件。2.Form表单上传,然后是form表单enctype="multipart/form-data"元素不可或缺3.上传//判断上传是否成功(是否使用post方式上传),if(is_uploaded_file($_FILES'myfile')){//将文件保存到你想要的目录(不要使用复制功能)$uploaded_file=$_FILES['myfile']['tmp_name'];//我们为每个用户动态创建一个文件夹$user_path=$_SERVER['DOCUMENT_ROOT']."/studyphp/file/up/".$username;//判断用户文件夹是否已有该文件夹if(!file_exists($user_path)){mkdir($user_path);}$file_true_name=$_FILES['myfile']['name'];$move_to_file=$user_path."/".time().rand(1,1000).substr($file_true_name,strrpos($file_true_name,"."));if(move_uploaded_file($uploaded_file,iconv("utf-8","gb2312",$move_to_file))){echo$_FILES['myfile']['name']."上传成功";}else{echo"上传失败";}}else{echo"上传失败";}4.要考虑的要素是文件大小,文件类型,如何防止用户的图片被覆盖,如何防止同一个用户有相同的文件名5.如果你使用的是lnmp架构,那么有哪些用于文件上传的nginx和php设置?php:file_uploads=on;是否允许通过HTTP上传文件的开关默认是ON的,即upload_tmp_dir是打开的;文件上传到服务器上存放临时文件的地方,如果不指定,将使用系统默认的临时文件夹upload_max_filesize=8m;网文业务,即上传文件的大小是允许的最大值。默认为2Mpost_max_size=8m;指的是通过表单POST到PHP可以接收到的最大值,包括表单中的所有值。默认8M,进一步配置如下参数max_execution_time=600;每个PHP页面运行的最大时间值(秒),默认为30秒max_input_time=600;每个PHP页面接收数据所需的最长时间,默认为60秒memory_limit=8m;每个PHP页面最大消耗内存默认为8M。修改以上参数后,在网络允许的正常情况下可以上传大容量文件。max_execution_time=600=32mnginx:找到http{}部分,在里面添加一行配置:client_max_body_size8m;这也是nginx的处理方式:413RequestEntityTooLarge以下例子使用ftp上传文件:'','username'=>'','password'=>'','port'=>''...);*/publicfunction__construct($config=array()){if(count($config)>0){$this->_init($config);}}/**FTP连接*@accesspublic@param数组配置数组@returnboolean*/publicfunctionconnect($config=array()){if(count($config)>0){$this->_init($config);}//判断ftp连接是否打开if(FALSE===($this->conn_id=@ftp_connect($this->hostname,$this->port))){if($this->debug===TRUE){$this->_error("ftp_unable_to_connect");}返回FALSE;}//判断是否登录成功if(!$this->_login()){if($this->debug===TRUE){$this->_error("ftp_unable_to_login");}returnFALSE;}//判断是否开启FTP被动模式if($this->passive===TRUE){ftp_pasv($this->conn_id,TRUE);}returnTRUE;}/**目录变化*@accesspublic@paramstring目录标识(ftp)@paramboolean@returnboolean*/publicfunctionchgdir($path='',$supress_debug=FALSE){if($path==''OR!$this->_isconn()){返回FALSE;}$result=@ftp_chdir($this->conn_id,$path);if($result===FALSE){if($this->debug===TRUEAND$supress_debug==FALSE){$this->_error("ftp_unable_to_chgdir:dir[".$path."]");}returnFALSE;}returnTRUE;}/**目录生成*@accesspublic@paramstring目录标识(ftp)@paramint文件权限列表@returnboolean*/publicfunctionmkdir($path='',$permissions=NULL){if($path==''OR!$this->_isconn()){returnFALSE;}$result=@ftp_mkdir($this->conn_id,$path);if($result===FALSE){if($this->debug===TRUE){$this->_error("ftp_unable_to_mkdir:dir[".$path."]");}returnFALSE;}if(!is_null($permissions)){$this->chmod($path,(int)$permissions);}returnTRUE;}/**upload*@accesspublic@paramstring本地目录标识符@paramstring远程目录ID(ftp)@paramstring上传模式auto||ascii@paramint上传文件权限列表@returnboolean*/publicfunctionupload($localpath,$remotepath,$mode='auto',$permissions=NULL){if(!$this->_isconn()){returnFALSE;}//判断本地文件是否存在if(!file_exists($localpath)){if($this->debug===TRUE){$this->_error("ftp_no_source_file:".$localpath);}returnFALSE;}//判断上传模式if($mode=='auto'){//获取文件后缀类型$ext=$this->_getext($localpath);//根据后缀类型判断上传方式是FTP_ASCII(文本方式)还是FTP_BINARY(二进制方式);$mode=$this->_settype($ext);}$mode=($mode=='ascii')?FTP_ASCII:FTP_BINARY;//上传$result=@ftp_put($this->conn_id,$remotepath,$localpath,$mode);//判断上传是否成功if($result===FALSE){if($this->debug===TRUE){$this->_error("ftp_unable_to_upload:localpath[".$localpath."]/remotepath[".$remotepath."]");}returnFALSE;}//判断是否重写文件权限if(!is_null($permissions)){$this->chmod($remotepath,(int)$permissions);}returnTRUE;}/**Download*@accesspublic@paramstringremotedirectoryID(ftp)@paramstring本地目录ID@paramstring下载模式auto||ascii@returnboolean*/publicfunctiondownload($remotepath,$localpath,$mode='auto'){if(!$this->_isconn()){返回FALSE;}if($mode=='auto'){$ext=$this->_getext($remotepath);$mode=$this->_settype($ext);}$mode=($mode=='ascii')?FTP_ASCII:FTP_BINARY;$result=@ftp_get($this->conn_id,$localpath,$remotepath,$mode);if($result===FALSE){if($this->debug===TRUE){$this->_error("ftp_unable_to_download:localpath[".$localpath."]-remotepath[".$remotepath."]");}returnFALSE;}returnTRUE;}/**rename/move*@accesspublic@paramstring远程目录ID(ftp)@paramstring新目录ID@paramboolean判断是否重命名(FALSE)或move(TRUE)@returnboolean*/publicfunctionrename($oldname,$newname,$move=FALSE){if(!$this->_isconn()){returnFALSE;}$result=@ftp_rename($this->conn_id,$oldname,$newname);if($result===FALSE){if($this->debug===TRUE){$msg=($move==FALSE)?"ftp_unable_to_rename":"ftp_unable_to_move";$this->_error($msg);}returnFALSE;}returnTRUE;}/**删除文件*@accesspublic@paramstringfileID(ftp)@returnboolean*/public函数delete_file($file){if(!$this->_isconn()){returnFALSE;}$result=@ftp_delete($this->conn_id,$file);if($result===FALSE){if($this->debug===TRUE){$this->_error("ftp_unable_to_delete_file:file[".$file."]");}返回FALSE;}returnTRUE;}/**删除文件夹*@accesspublic@paramstring目录标识符(ftp)@returnboolean*/publicfunctiondelete_dir($path){if(!$this->_isconn()){returnFALSE;}//在目录宏的'/'字符前加一个反斜杠''$path=preg_replace("/(.+?)/*$/","\1/",$path);//得到目录文件列表$filelist=$this->filelist($path);if($filelist!==FALSEANDcount($filelist)>0){foreach($filelistas$item){//如果我们不能删除,那么可能是一个文件夹//所以我们递归调用delete_dir()如果(!@delete_file($item)){$this->delete_dir($item);}}}//删除文件夹(空文件夹)$result=@ftp_rmdir($this->conn_id,$path);if($result===FALSE){if($this->debug===TRUE){$this->_error("ftp_unable_to_delete_dir:dir[".$path."]");}returnFALSE;}returnTRUE;}/**修改文件权限*@accesspublic@paramstring目录标识(ftp)@returnboolean*/publicfunctionchmod($path,$perm){if(!$this->_isconn()){returnFALSE;}//只有PHP5定义了修改权限的函数(ftp)if(!function_exists('ftp_chmod')){if($this->debug===TRUE){$this->_error("ftp_unable_to_chmod(function)");}returnFALSE;}$result=@ftp_chmod($this->conn_id,$perm,$path);if($result===FALSE){if($this->debug===TRUE){$this->_error("ftp_unable_to_chmod:path[".$path."]-chmod[".$perm."]");}returnFALSE;}returnTRUE;}/**获取目录文件列表*@accesspublic@paramstring目录ID(ftp)@returnarray*/publicfunctionfilelist($path='.'){if(!$this->_isconn()){returnFALSE;}returnftp_nlist($this->conn_id,$path);}/**关闭FTP*@accesspublic@returnboolean*/publicfunctionclose(){if(!$this->_isconn()){returnFALSE;}return@ftp_close($this->conn_id);}/**FTP成员变量初始化*@accessprivate@paramarray配置数组@returnvoid*/privatefunction_init($config=array()){foreach($configas$key=>$val){if(isset($this->$key)){$this->$key=$val;}}//特殊字符过滤$this->hostname=preg_replace('|.+?://|','',$this->hostname);}/**FTP登录*@accessprivate@returnboolean*/privatefunction_login(){return@ftp_login($this->conn_id,$this->username,$this->password);}/**判断con_id*@accessprivate@returnboolean*/privatefunction_isconn(){if(!is_resource($this->conn_id)){if($this->debug===TRUE){$this->_error("ftp_no_connection");}returnFALSE;}returnTRUE;}/**从文件名获取后缀扩展*@accessprivate@paramstring目录ID@returnstring*/privatefunction_getext($filename){if(FALSE===strpos($filename,'.')){return'txt';}$extarr=explode('.',$filename);returnend($extarr);}/**从后缀扩展ascii或二进制定义FTP传输模式*@accessprivate@param字符串后缀扩展@returnstring*/privatefunction_settype($ext){$text_type=array('txt','text','php','phps','php4','js','css','htm','html','phtml','shtml','log','xml');返回(in_array($ext,$text_type))?'ascii':'binary';}/**错误记录*@accessprvate@returnboolean*/privatefunction_error($msg){return@file_put_contents('ftp_err.log',"date[".date("Y-m-dH:i:s")."]-hostname[".$this->hostname."]-username[".$this->username."]-password[".$this->password."]-msg[".$msg."]n",FILE_APPEND);}}使用:'101.64.183.92',//服务器地址'username'=>'ftpadminuser',//FTP登录账号'password'=>'admin_user',//FTP登录密码'port'=>2112//端口号);$ftp=newFtp();//连接$ftp->connect($config);//先上传第一个参数为本地文件名,第二个参数为FTP文件名$rs=$ftp->upload('jsyh.flv','jsyh.flv');if($rs){echo1;}//$ftp->download('ftp_upload.log','ftp_download.log');
