Home  >  Article  >  Backend Development  >  PHP implementation of ftp file upload example, _PHP tutorial

PHP implementation of ftp file upload example, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:20:28932browse

Example of PHP implementation of ftp upload file,

FTP upload is a common and very important application skill implemented in PHP. Today I will share with you a simple example of FTP uploading files in PHP. I hope it will be of some help to everyone's PHP learning.

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地址") or die("Could not connect");
ftp_login($conn,"ftpname","password");
//利用ftp创建目录
make_directory($conn,$path);
//利用ftp选择进入目录
ftp_chdir($conn,$path);
//开始上传
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);
//注意上传端的ftp权限设置

Interested friends can test run or rewrite the code described in this article to deepen their understanding and improve the function of the code.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/867242.htmlTechArticleExample of ftp upload file implemented in PHP. FTP upload is a common and very important application skill implemented in PHP. Learn it today Let me share with you a simple example of using PHP to upload files via FTP. Hope...
Statement:
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