PHP实现ftp上传文件示例

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

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

相关文章

PHP写入WRITE编码为UTF8的文件的实现代码

<?php  $f=fopen("test.txt", "wb");  $text=utf8_encode("顨!");  // ...

php面向对象程序设计中self与static的区别分析

本文实例讲述了php面向对象程序设计中self与static的区别。分享给大家供大家参考,具体如下: 1、假设我们有个Car类,它有2个方法:model()和getModel()。...

php实现Mongodb自定义方式生成自增ID的方法

本文实例讲述了php实现Mongodb自定义方式生成自增ID的方法。分享给大家供大家参考。具体分析如下: 复制代码 代码如下://首先创建一个自动增长id集合 ids >db.id...

PHP中防止SQL注入方法详解

问题描述:   如果用户输入的数据在未经处理的情况下插入到一条SQL查询语句,那么应用将很可能遭受到SQL注入攻击,正如下面的例子: 复制代码 代码如下: $unsafe_variabl...

php 生成随机验证码图片代码

复制代码 代码如下:<?php /** 默认首页 **/ class DefaultController extends AppController { public functi...