欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

PHP的ftp文件,多文件上傳操作類

原文博客地址http://xgs888.top/post/view?id=97;

公司主營業(yè)務(wù):網(wǎng)站建設(shè)、成都做網(wǎng)站、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出桐梓免費做網(wǎng)站回饋大家。

PHP針對ftp文件的操作方法,如果是只操作一個ftp,可以使用里面的單利模式,

不需要每次都去實例化,我的項目中需要去鏈接很多個ftp服務(wù)器

所以需要多次去連接和關(guān)閉;

<?php
/**
 * Created by PhpStorm.
 * ftp的文件操作類
 * User: xiaoxie
 * Date: 2018/5/7
 * Time: 17:44
 **/

namespace App\Tools;


class FtpFile
{
    static private $_instance=null;
     private $ftp = null;
     public $off;             // 返回操作狀態(tài)(成功/失敗)

    //私有的構(gòu)造方法
    public  function __construct($config){
        //實例化
       $this->ftp = @ftp_connect($config['ftp_ip'],$config['ftp_port']) or die("FTP connection fail");
       //登錄驗證
       @ftp_login($this->ftp,$config['ftp_username'],$config['ftp_password']);
       //是否開啟被動模式
        if (isset($config['ftp_pasv']))
        {
            @ftp_pasv($this->ftp,true);
        }
    }

    /**
     * Created by PhpStorm.
     * function: getInstance
     * Description:公有的靜態(tài)方法
     * User: Xiaoxie
     * Email 736214763@qq.com
     * @return FtpFile|null
     *
     */
    static public function getInstance($config){
        if(!(self::$_instance instanceof self)){
            self::$_instance = new FtpFile($config);
        }
        return self::$_instance;
    }

    /**
     * Created by PhpStorm.
     * function: up_file
     * Description:上傳文件
     * User: Xiaoxie
     * Email 736214763@qq.com
     * @param $path 本地路徑
     * @param $newpath  若目標目錄不存在則新建
     * @param bool $type
     *
     */
    function up_file($path,$newpath,$type=true)
    {
        if($type) $this->dir_mkdirs($newpath);
        $this->off = @ftp_put($this->ftp,$newpath,$path,FTP_BINARY);
        if(!$this->off)
        {
            return "文件上傳失敗,請檢查權(quán)限及路徑是否正確!";
        }else{
            //刪除文件
            unlink($path);
            return true;
        }
    }

    /**
     * Created by PhpStorm.
     * function: uploadFile
     * Description:多文件上傳
     * User: Xiaoxie
     * Email 736214763@qq.com
     * @param array $files
     * @param bool $type
     * @return bool|void
     *
     */
    public function uploadFile($files=[],$type=true)
    {
        if (is_array($files))
        {
            foreach ($files as $key=>$file)
            {
                if($type)
                {
                    $this->dir_mkdirs($file);
                }
                $this->off = @ftp_put($this->ftp,$file,$key,FTP_BINARY);
                if(!$this->off)
                {
                     logs('ftp.txt',date('Y-m-d H:i:s').$file."文件上傳錯誤");
                }else{
                    //刪除文件
                    unlink($key);
                   // return true;
                }
            }
        }
        if(!$this->off)
        {
            //logs函數(shù)自定義日志
            logs('ftp.txt',date('Y-m-d H:i:s').$file."文件上傳錯誤");
            return false;
        }else{
            return true;
        }
    }

    /**
     * Created by PhpStorm.
     * function: move_file
     * Description:移動文件 修改文件名
     * User: Xiaoxie
     * Email 736214763@qq.com
     * @param $path 原路徑
     * @param $newpath 若目標目錄不存在則新建
     * @param bool $type
     *
     */
    function move_file($path,$newpath,$type=true)
    {
        if($type) $this->dir_mkdirs($newpath);
        $this->off = @ftp_rename($this->ftp,$path,$newpath);
        if(!$this->off) {
            return "文件移動失敗,請檢查權(quán)限及原路徑是否正確!";
        }else{
            return true;
        }
    }

    /**
     * Created by PhpStorm.
     * function: copy_file
     * Description:復(fù)制文件
     * User: Xiaoxie
     * Email 736214763@qq.com
     * @param $path原路徑
     * @param $newpath 新路徑
     * @param bool $type 若目標目錄不存在則新建
     *
     */
    function copy_file($path,$newpath,$type=true)
    {
        $downpath = "/var/www/temp.txt";
        $this->off = @ftp_get($this->ftp,$downpath,$path,FTP_BINARY);// 下載 
        if(!$this->off) 
        {
            return "文件復(fù)制失敗,請檢查權(quán)限及原路徑是否正確!";
        }
        $this->up_file($downpath,$newpath,$type);
    }

    /**
     * Created by PhpStorm.
     * function: del_file
     * Description:刪除文件
     * User: Xiaoxie
     * Email 736214763@qq.com
     * @param $path
     *
     */
    function del_file($path)
    {
        $this->off = @ftp_delete($this->ftp,$path);
        if(!$this->off){
            return false;
        }
    }

    /**
     * Created by PhpStorm.
     * function: dir_mkdirs
     * Description:生成目錄
     * User: Xiaoxie
     * Email 736214763@qq.com
     * @param $path 路徑
     *
     */
    function dir_mkdirs($path)
    {
        $path_arr = explode('/',$path);       // 取目錄數(shù)組 
        $file_name = array_pop($path_arr);      // 彈出文件名
        $path_div = count($path_arr);        // 取層數(shù) 

        foreach($path_arr as $val)          // 創(chuàng)建目錄 
        {
            if(@ftp_chdir($this->ftp,$val) == FALSE)
            {
                $tmp = @ftp_mkdir($this->ftp,$val);
                if($tmp == FALSE)
                {
                    
                    exit;
                }
                @ftp_chdir($this->ftp,$val);
            }
        }

        for($i=1;$i<=$path_div;$i++)         // 回退到根 
        {
            @ftp_cdup($this->ftp);
        }
    }

    /**
     * Created by PhpStorm.
     * function: close
     * Description:關(guān)閉鏈接
     * User: Xiaoxie
     * Email 736214763@qq.com
     *
     */
    public function close()
    {
        @ftp_close($this->ftp);
    }

    /**
     * 關(guān)閉鏈接
     *單例模式打開析構(gòu)方法
     */

    public function __destruct()
    {
        // TODO: Implement __destruct() method.
        //@ftp_close($this->ftp);
    }

}

lavarel中直接調(diào)用;

單例模式調(diào)用:

FtpFile::getInstance($this->data)->up_file($location_file,$remote_file);

不是單例模式調(diào)用;

$ftp = new FtpFile($this->data);
$ftp->uploadFile($filearr);
$ftp->close();

本文標題:PHP的ftp文件,多文件上傳操作類
分享URL:http://www.chinadenli.net/article30/peippo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司網(wǎng)站內(nèi)鏈網(wǎng)站營銷面包屑導(dǎo)航企業(yè)網(wǎng)站制作服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)