//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.";
本文介绍了使用CodeIgniter框架进行FTP配置的方法,并详细展示了如何通过CI实现文件上传功能,包括远程图片本地化处理。
1157

被折叠的 条评论
为什么被折叠?



