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上传文件时文件过大$_FILES为空的解决方法

在做图片上传的时候突然发现一张gif图片上传失败 size为0,实际大小为4.66M。上传小文件时可以,传大文件就不行,看了下PHP.INI里面upload_max_filesize =...

解决wincache不支持64位PHP5.5/5.6的问题(提供64位wincache下载) 原创

解决wincache不支持64位PHP5.5/5.6的问题(提供64位wincache下载) 原创

这几天公司有台服务器需要配置,系统是Windows 2008 R2,在IIS上配置环境是64位的PHP5.5,要求支持wincache。原本心想无非就是去wincache的官网下载下来,...

php多用户读写文件冲突的解决办法

一般的方案会是:复制代码 代码如下:$fp = fopen("/tmp/lock.txt", "w+");if (flock($fp, LOCK_EX)) {  &n...

smarty的保留变量问题

以下是访问页面请求变量诸如get,post,cookies,server,enviroment和session变量的例子. 例如{$smarty.server.SERVER_NAME}取...

php获取数组元素中头一个数组元素值的实现方法

本文实例讲述了php获取数组元素中头一个数组元素值的实现方法。分享给大家供大家参考。具体如下: 在php的内置函数中,获取数组元素值的函数主要有 reset next current p...