ftp在當地能往遠程伺服器上傳檔案,但是放到伺服器上就不好使了

來源:互聯網
上載者:User
ftp在本地能往遠程伺服器上傳檔案,但是放到伺服器上就不好使了
網站購買的萬網的虛擬機器主機,無法修改php.ini,也沒有辦法是用.htaccess來修改上傳檔案大小,故上傳大於10M的檔案只好使用ftp方式,代碼如下:
$ftp_server= "xxxx.xxxx.com";
$ftp_user_name= "xxxx";
$ftp_user_pass= "xxxxx";
$local_file = "D:/Lyb_0/zmsgx.mp4"; //本地檔案
$server_file = "htdocs/videos/zmsgx.mp4"; //待上傳後檔案所在路徑
$conn_id = ftp_connect($ftp_server); //聯結FTP
$login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass) or die("Login error"); //登入
$fp=fopen($local_file,"r");
ftp_pasv($conn_id, true); //開啟被動模式傳輸
//上傳參數
if(ftp_fput($conn_id,$server_file,$fp,FTP_BINARY)) {
echo "Successfully uploaded $server_file\n";
}else{
echo "There was a problem while uploading $file\n";
}
//關閉串連
ftp_close($conn_id);
fclose($fp);

但是這段代碼只在本地好使,放到伺服器上就好使了,fopen($local_file,"r");無法開啟本地檔案。請問如何解決?
------解決思路----------------------
你先確認 fopen 是否可用
在伺服器上所謂“本地檔案”是指伺服器上的檔案

所以你使用 ftp 繞過 upload 限制的做法,是不現實的
------解決思路----------------------
$local_file

這個無法取到本地檔案吧。
------解決思路----------------------
class Ftp{

public $ftpconn;
public static $_instance = array();
private function __construct($host,$username,$password) {
$this->ftpconn = ftp_connect($host);
$this->login($username, $password);
}
public function login($username, $password){
ftp_login($this->ftpconn, $username, $password);
}
public static function getInstance($host,$username,$password){
$key = md5($host.'-'.$username.'-'.$password);
if(!isset(self::$_instance[$key])
------解決思路----------------------
!(self::$_instance[$key] instanceof self)){
self::$_instance[$key] = new self($host, $username, $password);
}
return self::$_instance[$key];
}
public function __clone(){
trigger_error('該類禁止複製',E_USER_ERROR);
}
public function __destruct() {
ftp_close($this->ftpconn);
}
public function nlist($dir){
$filelist = ftp_nlist($this->ftpconn, $dir);
return $filelist;
}

//切換目前的目錄的父目錄
public function cdup(){
return ftp_cdup($this->ftpconn);
}
//在 FTP 伺服器上改變目前的目錄
public function chdir($dir){
return @ftp_chdir($this->ftpconn, $dir);
}
//修改檔案許可權
public function chmod ($dir,$mode){
return ftp_chmod($this->ftpconn, $mode, $dir);
}
//刪除檔案
public function delete($filename){
return ftp_delete($this->ftpconn, $filename);
}
//請求運行一條 FTP 命令(true,false)
public function exec($command){
return ftp_exec($this->ftpconn,$command);
}
//從 FTP 伺服器上下載一個檔案並儲存到本地一個已經開啟的檔案中
//mode傳輸模式只能為 (文字模式) FTP_ASCII 或 (二進位模式) FTP_BINARY 其中的一個。
public function fget ($handle,$remote_file,$mode,$resumepos = 0){
return ftp_fget($this->ftpconn,$handle,$remote_file,$mode,$resumepos);
}
//上傳一個已經開啟的檔案到 FTP 伺服器
//mode傳輸模式只能為 (文字模式) FTP_ASCII 或 (二進位模式) FTP_BINARY 其中的一個。
public function fput ($remote_file,$handle,$mode,$resumepos = 0 ){
return ftp_fput($this->ftpconn,$remote_file,$handle,$mode,$resumepos);
}
//返回當前 FTP 串連的各種不同的選項設定
public function get_option($option=FTP_TIMEOUT_SEC){
return ftp_get_option($this->ftpconn,$option);
}
//從 FTP 伺服器上下載一個檔案
public function get($local_file,$remote_file ,$mode,$resumepos=0){
return ftp_get($this->ftpconn,$local_file,$remote_file ,$mode,$resumepos);
}
//返回指定檔案的最後修改時間
public function mdtm ($remote_file){
return ftp_mdtm ($this->ftpconn,$remote_file);
}
//建立新目錄
public function mkdir($dir,$directory){
$this->chdir($dir);
return @ftp_mkdir($this->ftpconn,$directory);
}
//連續擷取/傳送檔案(non-blocking)
public function nb_continue(){
return ftp_nb_continue($this->ftpconn);
}
//Retrieves a file from the FTP server and writes it to an open file (non-blocking)
public function nb_fget ($handle,$remote_file,$mode,$resumepos = 0){
return ftp_nb_fget ($this->ftpconn,$handle,$remote_file,$mode,$resumepos);
}
//Stores a file from an open file to the FTP server (non-blocking)
public function nb_fput ($remote_file,$handle,$mode,$resumepos = 0 ){
return ftp_nb_fput ($this->ftpconn,$remote_file,$handle,$mode,$resumepos);
}
//從 FTP 伺服器上擷取檔案並寫入本地檔案(non-blocking)
public function nb_get ($local_file,$remote_file ,$mode,$resumepos=0){
return ftp_nb_get ($this->ftpconn,$local_file,$remote_file ,$mode,$resumepos);
}
//儲存一個檔案至 FTP 伺服器(non-blocking)
public function nb_put ($remote_file ,$local_file,$mode,$resumepos=0){
return ftp_nb_put ($this->ftpconn,$remote_file ,$local_file,$mode,$resumepos);
}
//返回當前 FTP 被動模式是否開啟
public function pasv($pasv){
return ftp_pasv($this->ftpconn,$pasv);
}
//上傳檔案到 FTP 伺服器
public function put ($remote_file ,$local_file,$mode,$resumepos=0){
return ftp_put ($this->ftpconn,$remote_file ,$local_file,$mode,$resumepos);
}
//返回目前的目錄名
public function pwd (){
return ftp_pwd ($this->ftpconn);
}
//Sends an arbitrary command to an FTP server
public function raw ($command){
return ftp_raw ($this->ftpconn,$command);
}
//返回指定目錄下檔案的詳細列表
public function rawlist($directory){
return ftp_rawlist($this->ftpconn,$directory);
}
//更改 FTP 伺服器上的檔案或目錄名
public function rename($oldname ,$newname){
return ftp_rename($this->ftpconn,$oldname , $newname);
}
//刪除 FTP 伺服器上的一個目錄
public function rmdir($directory){
return ftp_rmdir($this->ftpconn,$directory);
}
//設定各種 FTP 運行時選項
public function set_option ($option,$value){
return ftp_set_option ($this->ftpconn,$option,$value);
}
//向伺服器發送 SITE 命令
public function site ($cmd){
return ftp_site ($this->ftpconn,$cmd);
}
//返回指定檔案的大小
public function size ($remote_file){
return ftp_size ($this->ftpconn,$remote_file);
}
//Opens an Secure SSL-FTP connection
public function ssl_connect($host){
$this->ftpconn = ftp_ssl_connect($host);
}
//返回遠程 FTP 伺服器的作業系統類型
public function systype (){
return ftp_systype ($this->ftpconn);
}
}
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.