php的的ci框架下载文件,php ci框架ftp上传,下载远程文件(附ftp language.php)

本文介绍了使用CodeIgniter框架进行FTP配置的方法,并详细展示了如何通过CI实现文件上传功能,包括远程图片本地化处理。

//CI---FTP配置

private function ftpConf(){

//FTP上传文件的基本配置

$this->load->config('ftp');

$this->load->library('ftp');

$config = $this->config->config;

$conf['hostname'] = $config['FTPHOST'];

$conf['username'] = $config['FTPUSER'];

$conf['password'] = $config['FTPPASS'];

$conf['port'] = $config['FTPPORT'];

$conf['debug'] = $config['FTPDEBUG'];

$this->ftp->connect($conf);

// return $conf['hostname'];

}

//FTP---根据路径上传

private function ftpUpload($url,$path = 'logo'){

$imgname = getImgName().substr($url,strripos($url,'.'));

$this->ftpConf();

$rempath = $url;

$picpath = 'xcy/'.$path.'/'.date('Ym');

$this->ftp->rec_mkdir($picpath); //递归创建目录

$bool = $this->ftp->upload($rempath, $picpath.'/'.$imgname); //ftp核心

$imgPath = false;

if($bool){

$imgPath = 'xcy/'.$path.'/'.date('Ym').'/'.$imgname;

}

/*else{

echo $this->upload->display_errors();

}*/

$this->ftp->close();

return $imgPath;

}

//远程图片本地化

private function getRemoteFile($url,$path = 'logo'){

set_time_limit(0);

if($url == ""){ return false; }

$filename = getImgName().substr($url,strripos($url,'.'));

// 检查路径是否存在,如不存在则创建

// $dir = './upload/'.$path.'/'.date('Ym').'/';

// 检查路径是否存在,如不存在则创建(本目录指向根目录同级目录)

$dir = realpath(dirname(__FILE__).'/../').'/img.com/'.$path.'/'.date('Ym').'/';

checkdir($dir);

ob_start();//打开输出

readfile($url);//输出图片文件

$img = ob_get_contents();//得到浏览器输出

ob_end_clean();//清除输出并关闭

$size = strlen($img);//得到图片大小

$fp2 = @fopen($dir.$filename, "a");

if(fwrite($fp2,$img) === false){

log_message("error",'dolwload image falied. Error Info: 无法写入图片'); exit();

}

fclose($fp2);

return 'upload/'.$path.'/'.date('Ym').'/'.$filename;

}

//CI---FTP文件,自定义方法

function rec_mkdir($path)

{

$path_arr = explode('/',$path); // 取目录数组

$path_arr = array_filter($path_arr); //过滤空目录

$path_div = count($path_arr); // 取层数

foreach($path_arr as $val) // 创建目录

{

if(@ftp_chdir($this->conn_id,$val) == FALSE)

{

$tmp = @ftp_mkdir($this->conn_id,$val);

if($tmp == FALSE)

{

echo "目录创建失败,请检查权限及路径是否正确!";

exit;

}

@ftp_chdir($this->conn_id,$val);

}

}

for($i=1;$i<=$path_div;$i++) // 回退到根

{

@ftp_cdup($this->conn_id);

}

}

//CI-FTP-LANG(语言包)

$lang['ftp_no_connection'] = "Unable to locate a valid connection ID. Please make sure you are connected before peforming any file routines.";

$lang['ftp_unable_to_connect'] = "Unable to connect to your FTP server using the supplied hostname.";

$lang['ftp_unable_to_login'] = "Unable to login to your FTP server. Please check your username and password.";

$lang['ftp_unable_to_makdir'] = "Unable to create the directory you have specified.";

$lang['ftp_unable_to_changedir']= "Unable to change directories.";

$lang['ftp_unable_to_chmod'] = "Unable to set file permissions. Please check your path. Note: This feature is only available in PHP 5 or higher.";

$lang['ftp_unable_to_upload'] = "Unable to upload the specified file. Please check your path.";

$lang['ftp_unable_to_download'] = "Unable to download the specified file. Please check your path.";

$lang['ftp_no_source_file'] = "Unable to locate the source file. Please check your path.";

$lang['ftp_unable_to_rename'] = "Unable to rename the file.";

$lang['ftp_unable_to_delete'] = "Unable to delete the file.";

$lang['ftp_unable_to_move'] = "Unable to move the file. Please make sure the destination directory exists.";

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值