PHP实现ftp上传文件示例

yipeiwu_com6年前PHP代码库

FTP上传是PHP实现的一个常见且非常重要的应用技巧,今天就来与大家分享一下PHP实现FTP上传文件的简单示例。希望对大家的PHP学习能带来一定的帮助。

主要代码如下:

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权限设置

感兴趣的朋友可以测试运行或改写本文所述代码,加深理解的同时可以让代码功能更加完善。

相关文章

Linux fgetcsv取得的数组元素为空字符串的解决方法

但服务器上,很多使用Linux服务器,源程序使用UTF-8,这样很容易产生字符编码的问题. 如果仅仅将CSV文件转码为UTF-8,这样在Windows服务器上没有问题, 而在RedHat...

深入php list()函数的详解

list()( PHP 4中, PHP 5中) list-指定变量,好象他们是一个数组描述void list ( mixed $varname [, mixed $... ] )像阵列(...

PHPExcel中文帮助手册|PHPExcel使用方法(分享)

下面是总结的几个使用方法 include 'PHPExcel.php'; include 'PHPExcel/Writer/Excel2007.php'; //或者include...

PHP pthread拓展使用和注意点

一. 线程的创建和使用1. Thread类基本的创建和使用:<?php //通过继承Thread类来实现自己的线程类MyThread class MyThrea...

mac pecl 安装php7.1扩展教程

鉴于brew突然间的不能使用,简直是好坑的,以前多方便的安装啊,现在却那啥,好尴尬啊 brew search php ==> Searching local taps... p...