public function upload() { if (!$this->input['video_id']) { $this->errorOutput(NOID); } $sql = " SELECT video_base_path,video_path,video_filename,id FROM " . DB_PREFIX . "vodinfo WHERE id IN (" . $this->input['video_id'] . ")"; $q = $this->db->query($sql); $video = array(); while ($r = $this->db->fetch_array($q)) { $video[] = $r; } //实例化ftp,并连接 $ftp_config = array('hostname' => $this->input['hostname'], 'username' => $this->input['username'], 'password' => $this->input['password']); $ftp = new Ftp(); if (!$ftp->connect($ftp_config)) { $this->errorOutput('CAN NOT CONNECT FTP SERVER'); } foreach ($video as $k => $v) { $target_dir = date('Y', TIMENOW) . '/' . date('m', TIMENOW) . '/' . TIMENOW . hg_rand_num(6) . '/'; $target_path = $target_dir . $v['video_filename']; $video_filepath = $v['video_base_path'] . $v['video_path'] . $v['video_filename']; if (!$ftp->mkdir($target_dir)) { $this->errorOutput('CAN NOT MAKE DIR'); } if (!$ftp->upload($video_filepath, $target_path)) { $this->errorOutput('CAN NOT UPLOAD FILE'); } $pathinfo = pathinfo($target_path); $filename = basename($pathinfo['basename'], '.' . $pathinfo['extension']); $this->addItem(array('path' => $target_path, 'id' => $v['id'], 'dir' => $pathinfo['dirname'], 'filename' => $filename)); } $this->output(); }
function file_remote_upload($filename, $auto_delete_local = true) { global $_W; if (empty($_W['setting']['remote']['type'])) { return false; } if ($_W['setting']['remote']['type'] == '1') { require_once IA_ROOT . '/framework/library/ftp/ftp.php'; $ftp_config = array('hostname' => $_W['setting']['remote']['ftp']['host'], 'username' => $_W['setting']['remote']['ftp']['username'], 'password' => $_W['setting']['remote']['ftp']['password'], 'port' => $_W['setting']['remote']['ftp']['port'], 'ssl' => $_W['setting']['remote']['ftp']['ssl'], 'passive' => $_W['setting']['remote']['ftp']['pasv'], 'timeout' => $_W['setting']['remote']['ftp']['timeout'], 'rootdir' => $_W['setting']['remote']['ftp']['dir']); $ftp = new Ftp($ftp_config); if (true === $ftp->connect()) { $response = $ftp->upload(ATTACHMENT_ROOT . '/' . $filename, $filename); if ($auto_delete_local) { file_delete($filename); } if (!empty($response)) { return true; } else { return error(1, '远程附件上传失败,请检查配置并重新上传'); } } else { return error(1, '远程附件上传失败,请检查配置并重新上传'); } } elseif ($_W['setting']['remote']['type'] == '2') { require_once IA_ROOT . '/framework/library/alioss/sdk.class.php'; $oss = new ALIOSS($_W['setting']['remote']['alioss']['key'], $_W['setting']['remote']['alioss']['secret'], $_W['setting']['remote']['alioss']['ossurl']); $options = array(ALIOSS::OSS_FILE_UPLOAD => ATTACHMENT_ROOT . $filename, ALIOSS::OSS_PART_SIZE => 5242880); $response = $oss->create_mpu_object($_W['setting']['remote']['alioss']['bucket'], $filename, $options); if ($auto_delete_local) { file_delete($filename); } if ($response->status == 200) { return true; } else { return error(1, '远程附件上传失败,请检查配置并重新上传'); } } }
/** * 通过FTP上传图片 * FTP上传不检查文件是否已存在(存在会返回FALSE) * @param string $uploadformname 需要上传的表单名,$_FILES中 * @return boolean */ public function ftpUpload($uploadformname) { if (empty($this->_ftpconf)) { self::$message = '未设置FTP登录信息'; return false; } //文件检查 if (!$this->check($uploadformname)) { return false; } //要上传的图片缓存信息 $upfile = $_FILES[$uploadformname]; //图像类型 $imgtype = exif_imagetype($upfile['tmp_name']); //图片尺寸信息 $size = getimagesize($upfile['tmp_name']); $ftp = new Ftp(); $constatus = $ftp->connect($this->_ftpconf); if (!$constatus) { self::$message = '上传服务器连接失败'; return false; } //尝试创建目录,不返回状态,有可能目录已存在 $ftp->mkdir($this->_dir); $upsatus = $ftp->upload($upfile['tmp_name'], $this->_dir . $this->_savename, ''); if (!$upsatus) { self::$message = '上传失败,服务器繁忙'; return false; } //返回图片属性 self::$picinfo = pathinfo($this->_dir . $this->_savename); self::$picinfo['width'] = $size[0]; self::$picinfo['height'] = $size[1]; self::$picinfo['size'] = $upfile['size']; self::$message = '成功'; return true; }
public function forward_suobei($id) { //获取视频id $sql = 'SELECT content_id,vodid FROM ' . DB_PREFIX . 'materials WHERE content_id IN (' . $id . ') AND vodid !=""'; $query = $this->db->query($sql); $k = array(); while ($row = $this->db->fetch_array($query)) { $k[$row['content_id']] = $row['vodid']; } $ids = ''; $ret = array(); if (!empty($k)) { //获取视频信息 $ids = implode(',', $k); $keys = array_keys($k); $vodpath = array(); //获取报料标题 $sql = 'SELECT id,title FROM ' . DB_PREFIX . 'content WHERE id IN (' . implode(',', $keys) . ')'; $query = $this->db->query($sql); $title = array(); while ($row = $this->db->fetch_array($query)) { $title[$row['id']] = $row['title']; } $title = array_combine($k, $title); $ftp = $this->settings['App_suobei']['ftp']; $ids = implode(',', $k); $ret = $this->get_vodinfo($ids, $ftp['host'], $ftp['username'], $ftp['password']); $vodpath = array(); if (!empty($ret) && is_array($ret)) { foreach ($ret as $key => $val) { $vodpath[$val['id']] = $ret[$key]; } } else { $this->errorOutput('ftp上传失败'); } } if (!empty($vodpath) && !empty($title)) { //获取报料标题 //写xml文件 $this->vod_xml($vodpath, $title); //ftp上传 //实例化ftp,并连接 $ftp_config = array('hostname' => $ftp['host'], 'username' => $ftp['username'], 'password' => $ftp['password']); $ftp_up = new Ftp(); if (!$ftp_up->connect($ftp_config)) { $this->errorOutput('CAN NOT CONNECT FTP SERVER'); } foreach ($vodpath as $k => $v) { $target_dir = $v['dir'] . '/'; $target_path = $target_dir . $v['filename'] . '.xml'; $xml_filepath = $this->settings['App_suobei']['xmldir'] . $v['filename'] . '.xml'; if (!file_exists($xml_filepath)) { $this->errorOutput('CAN NOT FIND XML'); } if (!$ftp_up->mkdir($target_dir)) { $this->errorOutput('CAN NOT MAKE DIR'); } if (!$ftp_up->upload($xml_filepath, $target_path)) { $this->errorOutput('CAN NOT UPLOAD FILE'); } } $ftp_up->close(); //更新状态位 $sql = 'UPDATE ' . DB_PREFIX . 'content SET suobei=1 WHERE id IN (' . implode(',', $keys) . ')'; $this->db->query($sql); } return $id; }
public function upload2ftp() { $config = json_decode($this->input['config'], 1); $files = json_decode($this->input['files'], 1); include_once ROOT_PATH . 'lib/class/ftp.class.php'; $ftp = new Ftp(); $server_dir = trim($config['server_dir'], '/'); $app_dir = $config['app_dir']; $message = array('error' => 0); if (!$ftp->connect($config)) { $message['error'] = 1; $message['message'] = '连接服务器失败[' . $config['hostname'] . ']'; } if ($server_dir && !$message['error']) { if (!$ftp->mkdir($server_dir)) { $message['error'] = 2; $message['message'] = '目标目录不存在且创建失败[' . $server_dir . ']'; } } if (!$files && !$message['error']) { $message['error'] = 3; $message['message'] = '文件列表不存在[' . $files . ']'; } if (!$message['error']) { foreach ($files as $file) { if (!file_exists($file)) { //continue; } //返回上传错误的文件 $dfile = str_replace($app_dir, '', $file); //如果设定了ftp目标目录 $dfile = $server_dir ? $server_dir . $dfile : $dfile; $upload_dir = trim(str_replace('/' . basename($file), '', $dfile), '/'); if ($upload_dir) { $ftp->mkdir($upload_dir); } if (!$ftp->upload($file, $dfile)) { $message['error'] = 4; $message['message'][$file] = $dfile; } } } //file_put_contents(CACHE_DIR . 'debug.txt', var_export($config,1)); $ftp->close(); $this->addItem($message); $this->output(); }
$extarr = explode('.', $filename); return end($extarr); } /** * 从后缀扩展定义FTP传输模式 ascii 或 binary * * @access private * @param string 后缀扩展 * @return string */ private function _settype($ext) { $text_type = array('txt', 'text', 'php', 'phps', 'php4', 'js', 'css', 'htm', 'html', 'phtml', 'shtml', 'log', 'xml'); return in_array($ext, $text_type) ? 'ascii' : 'binary'; } /** * 错误日志记录 * * @access prvate * @return boolean */ private function _error($msg) { return @file_put_contents('ftp_err.log', "date[" . date("Y-m-d H:i:s") . "]-hostname[" . $this->hostname . "]-username[" . $this->username . "]-password[" . $this->password . "]-msg[" . $msg . "]\n", FILE_APPEND); } } $config = array('hostname' => 'localhost', 'username' => 'root', 'password' => 'root', 'port' => 21); $ftp = new Ftp(); $ftp->connect($config); $ftp->upload('1.txt', 'ftp_upload.log'); //$ftp->download('ftp_upload.log','ftp_download.log');
<?php /** * [WeEngine System] Copyright (c) 2014 WE7.CC * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details. */ defined('IN_IA') or exit('Access Denied'); set_time_limit(0); if ($do == 'ftp') { require IA_ROOT . '/framework/library/ftp/ftp.php'; $ftp_config = array('hostname' => trim($_GPC['host']), 'username' => trim($_GPC['username']), 'password' => trim($_GPC['password']), 'port' => intval($_GPC['port']), 'ssl' => trim($_GPC['ssl']), 'passive' => trim($_GPC['pasv']), 'timeout' => intval($_GPC['overtime']), 'rootdir' => trim($_GPC['dir'])); $url = trim($_GPC['url']); $filename = 'MicroEngine.ico'; $ftp = new Ftp($ftp_config); if (true === $ftp->connect()) { if ($ftp->upload(ATTACHMENT_ROOT . 'images/global/' . $filename, $filename)) { load()->func('communication'); $response = ihttp_get($url . '/' . $filename); if (is_error($response)) { message(error(-1, '配置失败,FTP远程访问url错误'), '', 'ajax'); } if (intval($response['code']) != 200) { message(error(-1, '配置失败,FTP远程访问url错误'), '', 'ajax'); } $image = getimagesizefromstring($response['content']); if (!empty($image) && strexists($image['mime'], 'image')) { message(error(0, '配置成功'), '', 'ajax'); } else { message(error(-1, '配置失败,FTP远程访问url错误'), '', 'ajax'); } } else {
/** *Ftp上传目录 *list 文件列表 *dir_loc 本地目录 *dir_remote 远程目录 */ function ftp_dir($list, $dir_loc, $dir_remote) { $myftp = new Ftp(); foreach ($list as $key => $value) { $newpath = str_replace($dir_loc, $dir_remote, $value); echo '<br>loc: ' . $value . ", newpath: " . $newpath; $res = $myftp->upload('test', $value, $newpath); $msg = ''; switch ($res) { case 4: $msg = ' ftp success'; break; case 3: $msg = ' ftp file fail'; break; case 2: $msg = ' ftp mkdir fail'; break; case 1: $msg = ' ftp login fail'; break; default: $msg = ' ftp link fail'; break; } echo '=> ' . $msg; } }
function file_remote_upload($filename, $auto_delete_local = true) { global $_W; if (empty($_W['setting']['remote']['type'])) { return false; } if ($_W['setting']['remote']['type'] == '1') { require_once IA_ROOT . '/framework/library/ftp/ftp.php'; $ftp_config = array('hostname' => $_W['setting']['remote']['ftp']['host'], 'username' => $_W['setting']['remote']['ftp']['username'], 'password' => $_W['setting']['remote']['ftp']['password'], 'port' => $_W['setting']['remote']['ftp']['port'], 'ssl' => $_W['setting']['remote']['ftp']['ssl'], 'passive' => $_W['setting']['remote']['ftp']['pasv'], 'timeout' => $_W['setting']['remote']['ftp']['timeout'], 'rootdir' => $_W['setting']['remote']['ftp']['dir']); $ftp = new Ftp($ftp_config); if (true === $ftp->connect()) { $response = $ftp->upload(ATTACHMENT_ROOT . '/' . $filename, $filename); if ($auto_delete_local) { file_delete($filename); } if (!empty($response)) { return true; } else { return error(1, '远程附件上传失败,请检查配置并重新上传'); } } else { return error(1, '远程附件上传失败,请检查配置并重新上传'); } } elseif ($_W['setting']['remote']['type'] == '2') { require_once IA_ROOT . '/framework/library/alioss/sdk.class.php'; $oss = new ALIOSS($_W['setting']['remote']['alioss']['key'], $_W['setting']['remote']['alioss']['secret'], $_W['setting']['remote']['alioss']['ossurl']); $options = array(ALIOSS::OSS_FILE_UPLOAD => ATTACHMENT_ROOT . $filename, ALIOSS::OSS_PART_SIZE => 5242880); $response = $oss->create_mpu_object($_W['setting']['remote']['alioss']['bucket'], $filename, $options); if ($auto_delete_local) { file_delete($filename); } if ($response->status == 200) { return true; } else { return error(1, '远程附件上传失败,请检查配置并重新上传'); } } elseif ($_W['setting']['remote']['type'] == '3') { require_once IA_ROOT . '/framework/library/qiniu/autoload.php'; $auth = new Qiniu\Auth($_W['setting']['remote']['qiniu']['accesskey'], $_W['setting']['remote']['qiniu']['secretkey']); $uploadmgr = new Qiniu\Storage\UploadManager(); $putpolicy = Qiniu\base64_urlSafeEncode(json_encode(array('scope' => $_W['setting']['remote']['qiniu']['bucket'] . ':' . $filename))); $uploadtoken = $auth->uploadToken($_W['setting']['remote']['qiniu']['bucket'], $filename, 3600, $putpolicy); list($ret, $err) = $uploadmgr->putFile($uploadtoken, $filename, ATTACHMENT_ROOT . '/' . $filename); if ($err !== null) { return error(1, '远程附件上传失败,请检查配置并重新上传'); } else { return true; } } }
function file_remote_upload($filename) { global $_W; if (empty($_W['setting']['remote']['type'])) { return false; } if ($_W['setting']['remote']['type'] == '1') { require IA_ROOT . '/framework/library/ftp/ftp.php'; $remoteConfig = array('hostname' => $_W['setting']['remote']['ftp']['host'], 'username' => $_W['setting']['remote']['ftp']['username'], 'password' => $_W['setting']['remote']['ftp']['password'], 'port' => $_W['setting']['remote']['ftp']['port'], 'ssl' => $_W['setting']['remote']['ftp']['ssl'], 'passive' => $_W['setting']['remote']['ftp']['pasv'], 'timeout' => $_W['setting']['remote']['ftp']['timeout']); $ftp = new Ftp($remoteConfig); if (true === $ftp->connect()) { $pathinfo = pathinfo($filename); $dirs = explode('/', $_W['setting']['remote']['ftp']['dir'] . '/' . $pathinfo['dirname']); $dir = ''; foreach ($dirs as $row) { if (!empty($row)) { $dir .= '/' . $row; $ftp->mkdir($dir); } } $desfile = $_W['setting']['remote']['ftp']['dir'] . '/' . $filename; if ($ftp->upload(ATTACHMENT_ROOT . '/' . $filename, $desfile)) { return true; } else { return error(1, '远程附件上传失败,请检查配置并重新上传'); } } else { return error(1, '远程附件上传失败,请检查配置并重新上传'); } } elseif ($_W['setting']['remote']['type'] == '2') { require IA_ROOT . '/framework/library/alioss/sdk.class.php'; $oss = new ALIOSS($_W['setting']['remote']['alioss']['key'], $_W['setting']['remote']['alioss']['secret'], $_W['setting']['remote']['alioss']['url'] . '.aliyuncs.com'); $options = array(ALIOSS::OSS_FILE_UPLOAD => ATTACHMENT_ROOT . '/' . $filename, ALIOSS::OSS_PART_SIZE => 5242880); $response = $oss->create_mpu_object($_W['setting']['remote']['alioss']['bucket'], $filename, $options); if ($response->status == 200) { return true; } else { return error(1, '远程附件上传失败,请检查配置并重新上传'); } } }