PHP implements ftp File Upload example,

Source: Internet
Author: User
Tags ftp file learn php

PHP implements ftp File Upload example,

FTP upload is a common and important application technique for PHP implementation. Today, I will share with you a simple example of PHP to implement FTP file upload. I hope this will help you learn PHP.

The main code is as follows:

Function make_directory ($ ftp_stream, $ dir) {// if directory already exists or can be immediately created return true if ($ this-> ftp_is_dir ($ ftp_stream, $ dir) | @ ftp_mkdir ($ ftp_stream, $ dir) return true; // otherwise recursively try to make the directory if (! $ This-> make_directory ($ ftp_stream, dirname ($ dir) return false; // final step to create the directory return ftp_mkdir ($ ftp_stream, $ dir );} function ftp_is_dir ($ ftp_stream, $ dir) {// get current directory $ original_directory = ftp_pwd ($ ftp_stream ); // test if you can change directory to $ dir // suppress errors in case $ dir is not a file or not a directory if (@ ftp_chdir ($ ftp_stream, $ dir )) {// If it is a directory, then change the directory back to the original directory ftp_chdir ($ ftp_stream, $ original_directory); return true;} else {return false ;}} $ conn = ftp_connect ("ftp address") or die ("cocould not connect"); ftp_login ($ conn, "ftpname", "password "); // use ftp to create the directory make_directory ($ conn, $ path); // use ftp to select the directory ftp_chdir ($ conn, $ path ); // start to upload if (ftp_put ($ conn, $ info [0] ['savename'], getcwd (). $ upload-> savePath. $ info [0] ['savename'], FTP_BINARY) {unlink (getcwd (). $ upload-> savePath. $ info [0] ['savename']);} ftp_close ($ conn); // pay attention to the ftp permission settings on the upload end

If you are interested, you can test and run the code or rewrite the code described in this article to improve the Code functions.




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.