PHP Remote upload file class instance with FTP detailed

Source: Internet
Author: User
Tags ftp upload file
This article mainly introduces PHP using FTP remote Upload file class examples, interested in the friend's reference, I hope to help you.

PHP uses FTP to implement file upload code snippet:

<?php/** * FTP upload File class */class FTP {/** * Test server * * @var array */Private $testServer = Array (' Host ' = ' IP ', ' port ' = ' + ', ' user ' = ' userName ', ' pwd ' = ' password ')     ; /** * Open and log on to server * * @param string $flag server Identity Test * @return Mixed * 0: Server Connection Failed * 1: Server Login Failed * Resource Connection ID */Public Function openserver ($flag = ' Test ') {//select server $config = $this-       >getserverconfig ($flag);       Connection Server $connect = Ftp_connect ($config [' Host '], $config [' Port '];       if ($connect = = false) return 0;       Log on to the server if (!ftp_login ($connect, $config [' User '], $config [' pwd '])) return 1;       Turn on Passive mode, the transfer of data is initiated by the client, not the server start FTP_PASV ($connect, true);     Returns the connection identifier return $connect;  }/** * Create directory and navigate directory to directory * * @param resource $connect Connection ID * @param string $dirPath directory path * @return       Mixed * 2: Failed to create directory *True: Create directory Success */Public Function MakeDir ($connect, $dirPath) {//process directory $dirPath = '/'. Trim ($dirPath, '/')       );       $dirPath = explode ('/', $dirPath);         foreach ($dirPath as $dir) {if ($dir = = ") $dir = '/';  Determine if the directory exists if (@ftp_chdir ($connect, $dir) = = False) {//Determine if the directory was created successfully if (@ftp_mkDir ($connect, $dir)           = = False) {return 2;         } @ftp_chdir ($connect, $dir);     }} return true; }/** * Close Server * * @param resource $connect Connection ID */Public Function CloseServer ($connect) {i     F (!empty ($connect)) ftp_close ($connect); /** * Upload File * * @param string $flag Server Identity * @param string $local The local path of the uploaded file * @param string $remo     Te remote path to upload file * @return int * 0: Server Connection Failed * 1: Server Logon Failure * 2: Failed to create directory * 3: failed to upload file * 4: Upload successful */public function upload ($flag = ' Test ', $local, $remote) {//Connect and log in to serviceDevice $connect = $this->openserver ($flag); if ($connect = = = 0) | | ($connect = = = 1))       return $connect;       Upload file directory processing $MDR = $this->makedir ($connect, DirName ($remote));       if ($MDR = = = 2) return 2;       Upload file $result = ftp_put ($connect, basename ($remote), $local, ftp_binary);       Shut down the server $this->closeserver ($connect); Return the result return (! $result)?     3:4;       }/** * Delete file * * @param string $flag Server identity * @param remote path of string $remote file * @return int * 0: Server Connection Failed * 1: Server Logon Failure * 2: Delete failed * 3: Delete successful */Public function Delete ($flag = ' tes       T ', $remote) {//Connect and log on to the server $connect = $this->openserver ($flag); if ($connect = = = 0) | | ($connect = = = 1))       return $connect;       Delete $result = Ftp_delete ($connect, $remote);       Shut down the server $this->closeserver ($connect); Return the result return (! $result)?     2:3; }/** * Read file * * @param string $flag Server identity * @param a remote path to a string $remote file * @return Mixed * 0: Server Connection Failed * 1: Server logon failed       */Public function read ($flag, $remote) {//Connect and log on to the server $connect = $this->openserver ($flag); if ($connect = = = 0) | | ($connect = = = 1))       return $connect;       Read $result = Ftp_nlist ($connect, $remote);       Shut down the server $this->closeserver ($connect);       Returns the result foreach ($result as $key + $value) {if (In_array ($value, Array ('. ', '. '))) unset ($result [$key]);     } return Array_values ($result); /** * Get FTP Server Configuration * * @param string $flag server Identity Test * @return Array FTP Server Connection configuration */Private FU       Nction getserverconfig ($flag = ' Test ') {$flag = Strtolower ($flag);       Test server if ($flag = = ' Test ') return $this->testserver;     Returns the test server return by default $this->testserver; }}?>

Summary: The above is the entire content of this article, I hope to be able to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.