Maison > Article > développement back-end > 关于FTP上传文件有关问题
关于FTP上传文件问题
各位在工作是如何通过FTP上传文件到服务器上,我在网上找了很多例子,没有一个成功的,要支持SFTP的,希望好心人能帮帮我
<?php<br /> class Ftp {<br /> var $host;<br /> var $user;<br /> var $pswd;<br /> var $connectId = null;<br /> var $loginId = null;<br /> <br /> function Ftp($host, $user, $pswd, $timeout=900){<br /> if($host) {<br /> $this->host = $host;<br /> }<br /> if($user) {<br /> $this->user = $user;<br /> }<br /> if($pswd) {<br /> $this->pswd = $pswd;<br /> }<br /> if(!$this->connectId = @ftp_connect($this->host)) {<br /> $this->error("");<br /> }<br /> else {<br /> //echo 'connect is ok<br />';<br /> }<br /> if(!$this->loginId = @ftp_login($this->connectId, $this->user, $this->pswd)){<br /> $this->error("");<br /> }<br /> else {<br /> if(@ftp_pasv($this->connectId, true)) {<br /> //echo 'pasv mode is open<br />';<br /> }<br /> //echo 'login successful<br />';<br /> }<br /> @ftp_set_option($this->connectId, FTP_TIMEOUT_SEC, $timeout);<br /> }<br /> <br /> function chdir($targetDir) {<br /> return @ftp_chdir($this->connectId, $targetDir);<br /> }<br /> <br /> function put($sourceFile, $targetFile, $ftpType=FTP_BINARY) {<br /> return @ftp_put($this->connectId, $targetFile, $sourceFile, $ftpType);<br /> }<br /> function chmod($file, $pr=0777){<br /> return @ftp_chmod($this->connectId, $pr, $file);<br /> }<br /> function mkdir($dirName=''){<br /> if(!$dirName) {<br /> return false;<br /> }<br /> else {<br /> return @ftp_mkdir($this->connectId, $dirName);<br /> }<br /> }<br /> <br /> function exec($cmd=''){ //for some unsafe system<br /> if(!$cmd){<br /> return false;<br /> }<br /> return @ftp_exec($this->connectId, $cmd);<br /> }<br /> <br /> function ls($pwd='./') {<br /> return @ftp_nlist($this->connectId, $pwd);<br /> }<br /> <br /> function pwd() {<br /> return @ftp_pwd($this->connectId);<br /> }<br /> <br /> function rmFile($file) {<br /> return @ftp_delete($this->connectId, $file);<br /> }<br /> <br /> function close(){<br /> return @ftp_close($this->connectId);<br /> }<br /> <br /> function error($errorMsg) {<br /> //echo "<pre class="brush:php;toolbar:false">";<br /> print_r($errorMsg);<br /> //echo "";
$ftp = new Ftp($row['ftpHost'], $row['ftpUser'], $row['ftpPswd']);<br> if($ftp->loginId) {<br> <br> $webRoot = WEB_ROOT;<br> <br> $res=$ftp->put(APP_ROOT. "upload". "/". $fileName, $webRoot. $fileName);<br> $res=$ftp->put(APP_ROOT."database.php", $webRoot."database.php");<br> $res=$ftp->put(APP_ROOT."wp-config.php", $webRoot."wp-config.php"); <div class="clear"> </div>