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

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

相关文章

ajax+php打造进度条 readyState各状态

用Ajax+php打造进度条,其实很简单。 readyState == 状态(0,1,2,3,4) 0:请求未初始化,还没调用open 1:请求已经建立,但还没有发送,还没调用send...

PHP面向对象精要总结

本文实例汇总了PHP面向对象程序设计的精要。分享给大家供大家参考。具体分析如下: 1 使用extends实现继承以及重载、魔术方法的含义 class B extends A 声明的时候B...

php 团购折扣计算公式

复制代码 代码如下: $price=$row['price']; //原价 $nowprice=$row['nowprice']; //现价 $jiesheng=$price-$nowp...

PHP易混淆函数的区别及用法汇总

本文实例分析了PHP易混淆函数的区别及用法。分享给大家供大家参考。具体分析如下: 1.echo和print的区别 PHP中echo和print的功能基本相同(输出),但是两者之间还是有细...

php中static和const关键字用法分析

php中static和const关键字用法分析

本文实例讲述了php中static和const关键字用法。分享给大家供大家参考,具体如下: static关键字在类中描述的成员属性和成员函数都是静态的。 static成员能限制外部的访问...