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

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

相关文章

PHP生成不重复标识符的方法

本文实例讲述了PHP生成不重复标识符的方法。分享给大家供大家参考。具体实现方法如下: 生成唯一不重复的标识我们主要是根据当前的一个时间time然后再转换在md5值,这样几乎是可以保证标签...

WordPress中登陆后关闭登陆页面及设置用户不可见栏目

WordPress中登陆后关闭登陆页面及设置用户不可见栏目

用户登录后关闭登录页面 WordPress默认的登录页面是:http://WP目录/wp-login.php,登录后会自动跳转到:http://WP目录/wp-admin。现在问题来了,...

Linux php 中文乱码的快速解决方法

在ubuntu下php网页输出乱码,在不涉及数据库编码的情况下: 修改“/etc/php5/apache2/php.ini”将 default_charset = "iso-8859-1...

php加速器eAccelerator的配置参数、API详解

它还能够对脚本进行优化以便加速它们的执行速度。利用eAccelerator可以减少服务器的负载并提高PHP代码的执行时间。 eAccelerator配置选项:复制代码 代码如下:zend...

简单谈谈 php 文件锁

锁机制之所以存在是因为并发导致的资源竞争,为了确保操作的有效性和完整性,可以通过锁机制将并发状态转换成串行状态。作为锁机制中的一种,PHP的文件锁也是为了应对资源竞争。假设一个应用场景,...