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 class中public,private,protected的区别以及实例分析

一,public,private,protected的区别public:权限是最大的,可以内部调用,实例调用等。protected: 受保护类型,用于本类和继承类调用。private:...

php简单实现发送带附件的邮件

本文实例讲述了php简单实现发送带附件的邮件。分享给大家供大家参考。具体如下: 下面是静态html代码: <html> <head> <title&...

PHP几个数学计算的内部函数学习整理

round round - 对浮点数进行四舍五入。round 函数语法如下: round(float,precision) 其中参数 precision 表示小数点后面要保持的精度位数。...

PHP图片处理之使用imagecopyresampled函数裁剪图片例子

图片裁剪是指在一个大的背景图片中裁剪出一张指定区域的图片,常见的应用是在用户设置个人头像时,可以从上传的图片中,裁剪出一个合适的区域作为自己的个人头像图片。图像裁剪和图片缩放的相似,所以...

php的日期处理函数及uchome的function_coomon中日期处理函数的研究

复制代码 代码如下: <?php echo time(); echo mktime(11,25,0,9,5,2010);//和time一样的 echo microtime(); e...