Home Backend Development PHP Tutorial PHP利用curl下传文件到FTP服务器(无ftp扩展情况上)

PHP利用curl下传文件到FTP服务器(无ftp扩展情况上)

Jun 13, 2016 pm 01:06 PM
curl ftp static

PHP利用curl上传文件到FTP服务器(无ftp扩展情况下)

在一次需求中,需要一个FTP服务器作为中转站,程序定时在FTP服务器获取数据,定时上传数据库的数据到FTP服务器上,由于PHP没有安装ftp扩展,导致FTP操作很是麻烦,对于socket的理解不够深入,由于时间比较紧急,在同事指点下,想到了用curl方法,经过自己的整理,将curl方法整理为一个类:

?

<?php

/*
 * To change this template, choose Tools | Templates
 * ftp curl方法操作类
 */
class ftp{
    //FTP服务器地址
    public static $host = "127.0.0.1";
    //FTP端口
    public static $port = "2121";
    //上传的FTP目录
    public static $uploaddir = "upblod";
    //读取的FTP目录
    public static $readdir = "read";
    //FTP用户名
    public static $usrname = "user";
    //FTP密码
    public static $pwd = "pwd";
    /*
     * curl 方法将文件上传到FTP服务器
     * $filename上传到FTP的文件名,$uploadfile具体需要上传文件的地址(我用的绝对路径)
     */
    public static function ftp_upload($filename,$uploadfile)
    {
        $url = "ftp://".self::$host.":".self::$port."/".self::$uploaddir."/".$filename;
        //需要上传的文件
        $fp = fopen ($uploadfile, "r"); 
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_VERBOSE, 1);  //有意外发生则报道 
        curl_setopt($ch, CURLOPT_USERPWD, self::$usrname.':'.self::$pwd); //FTP登陆账号密码,模拟登陆 
        curl_setopt($ch, CURLOPT_URL, $url); 
        curl_setopt($ch, CURLOPT_PUT, 1); //用HTTP上传一个文件 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //不输出 
        curl_setopt($ch, CURLOPT_INFILE, $fp); //要上传的文件 
        $http_result = curl_exec($ch); //执行 
        $error = curl_error($ch); 
        curl_close($ch); 
        fclose($fp);
        //成功上传文件 返回true
        if (!$error) 
        { 
            return true;
        }
     }
    /*
     * curl 方法将读取FTP文件并保存在本地使用
     * $filenameFTP服务器文件名,$filepath 保存到本地(服务器)的目录
     */    
    public static function ftp_read($filename,$filepath)
    {       
        $curl = curl_init(); 
        
        $target_ftp_file = "ftp://".self::$host.":".self::$port."/".self::$readdir."/".$filename;//完整路径
        
        curl_setopt($curl, CURLOPT_URL,$target_ftp_file);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_VERBOSE, 1);
        curl_setopt($curl, CURLOPT_FTP_USE_EPSV, 0);
        curl_setopt($curl, CURLOPT_TIMEOUT, 300); // times out after 300s
        curl_setopt($curl, CURLOPT_USERPWD,self::$usrname.':'.self::$pwd);//FTP用户名:密码
        // Sets up the output file
        //本地保存目录
        if(is_dir($filepath)){
         $outfile = fopen($filepath.$filename, 'w');//保存到本地的文件名
         curl_setopt($curl,CURLOPT_FILE,$outfile);
         // Executes the cURL 
         $info = curl_exec($curl);
         fclose($outfile);
         $error_no = curl_errno($curl);
         curl_close($curl);
         //成功读取文件,返回 true
         if($info){
             return true;
         }
        }        
     }
}
?>
Copy after login

?这里只是做了上传与下载文件,删除文件的操作没有涉及,有兴趣的童鞋可以研究下

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to realize the mutual conversion between CURL and python requests in python How to realize the mutual conversion between CURL and python requests in python May 03, 2023 pm 12:49 PM

Both curl and Pythonrequests are powerful tools for sending HTTP requests. While curl is a command-line tool that allows you to send requests directly from the terminal, Python's requests library provides a more programmatic way to send requests from Python code. The basic syntax for converting curl to Pythonrequestscurl command is as follows: curl[OPTIONS]URL When converting curl command to Python request, we need to convert the options and URL into Python code. Here is an example curlPOST command: curl-XPOST https://example.com/api

Tutorial on updating curl version under Linux! Tutorial on updating curl version under Linux! Mar 07, 2024 am 08:30 AM

To update the curl version under Linux, you can follow the steps below: Check the current curl version: First, you need to determine the curl version installed in the current system. Open a terminal and execute the following command: curl --version This command will display the current curl version information. Confirm available curl version: Before updating curl, you need to confirm the latest version available. You can visit curl's official website (curl.haxx.se) or related software sources to find the latest version of curl. Download the curl source code: Using curl or a browser, download the source code file for the curl version of your choice (usually .tar.gz or .tar.bz2

How to set up nginx reverse proxy ftp server How to set up nginx reverse proxy ftp server May 17, 2023 am 09:31 AM

1. Install nginx 2. Install vsftpd 3. Modify the nginx configuration file nginx.conf 3.1 Add the ftp user userftpuser in the first line; 3.2 Configure the relevant path server{ listen80; #nginx proxy port server_namelocalhost; #ftp server address location/images{root /home/ftpuser; #The absolute path of the folder of the proxy ftp server indexftpuser; #Set the welcome page

Using FTP in Go: A Complete Guide Using FTP in Go: A Complete Guide Jun 17, 2023 pm 06:31 PM

With the rapid development of the Internet, File Transfer Protocol (FTP) has always been an important file transfer method. In Go language, using FTP to transfer files may be a need of many developers. However, maybe many people don't know how to use FTP in Go language. In this article, we will explore how to use FTP in Go language, from connecting to FTP server to file transfer, and how to handle errors and exceptions. Create FTP connection In Go language, we can use the standard "net" package to connect to FTP

PHP8.1 released: Introducing curl for concurrent processing of multiple requests PHP8.1 released: Introducing curl for concurrent processing of multiple requests Jul 08, 2023 pm 09:13 PM

PHP8.1 released: Introducing curl for concurrent processing of multiple requests. Recently, PHP officially released the latest version of PHP8.1, which introduced an important feature: curl for concurrent processing of multiple requests. This new feature provides developers with a more efficient and flexible way to handle multiple HTTP requests, greatly improving performance and user experience. In previous versions, handling multiple requests often required creating multiple curl resources and using loops to send and receive data respectively. Although this method can achieve the purpose

From start to finish: How to use php extension cURL to make HTTP requests From start to finish: How to use php extension cURL to make HTTP requests Jul 29, 2023 pm 05:07 PM

From start to finish: How to use php extension cURL for HTTP requests Introduction: In web development, it is often necessary to communicate with third-party APIs or other remote servers. Using cURL to make HTTP requests is a common and powerful way. This article will introduce how to use PHP to extend cURL to perform HTTP requests, and provide some practical code examples. 1. Preparation First, make sure that php has the cURL extension installed. You can execute php-m|grepcurl on the command line to check

PHP and FTP: realizing file sharing among multiple departments in website development PHP and FTP: realizing file sharing among multiple departments in website development Jul 28, 2023 pm 01:01 PM

PHP and FTP: Achieve file sharing among multiple departments in website development. With the development of the Internet, more and more companies are beginning to use website platforms for information release and business promotion. However, the problem that arises is how to achieve file sharing and collaboration among multiple departments. In this case, PHP and FTP become one of the most commonly used solutions. This article will introduce how to use PHP and FTP to achieve file sharing among multiple departments in website development. 1. Introduction to FTP FTP (FileTransferPr

How to compare directories and files on an FTP server via PHP How to compare directories and files on an FTP server via PHP Jul 28, 2023 pm 02:09 PM

How to compare directories and files on an FTP server through PHP In web development, sometimes we need to compare local files with files on the FTP server to ensure consistency between the two. PHP provides some functions and classes to implement this functionality. This article will introduce how to use PHP to compare directories and files on an FTP server, and provide relevant code examples. First, we need to connect to the FTP server. PHP provides the ftp_connect() function to establish an FTP server

See all articles