文件选择插件

简爱代码>JavaScript2016-5-21 18:4341900

在 使用 kindeditor 编辑器的时候

再设置单个图片的时候选择文件还是挺方便的,但在百度 UEditor 就没看到类似的调用方法, 肯定是自己没有看全,或者例子不完善吧


自己闲着没事就自己写了一个,图标来自 *云,在这里就不点名了




查看 list.js 源码


然后还有服务段的 PHP 代码

<?php

// 定义文件根目录
define('ROOT_PATH', dirname(__FILE__));

// 获取路径
$path = isset($_GET['path']) ? trim($_GET['path']) : '';
$callback = isset($_GET['callback']) ? trim($_GET['callback']) : '';

$files = getFiles($path, 'jpg|jpeg|gif|png|txt|js|html|htm');

// 转为 JSON 字符串
$files = json_encode($files);

// 如果为  jsonp 调用 就添加 回调函数
$files = !empty($callback) ? "$callback($files);" : $files;

die($files);





// 获取所有文件 目录
function getFiles($path, $allowFiles = 'jpg|jpeg|gif|png|zip|rar|txt'){
	$path = trim($path, '.\\/');
	$path = preg_replace('/\.+/', '.', $path);
	$path = !empty($path) ? ROOT_PATH .'/'. $path : ROOT_PATH;

	$path = realpath($path);
	if(!$path){
		return array();
	}

	$list = array();
	$files = scandir($path);
	foreach($files as $name){
		$file = $path . DIRECTORY_SEPARATOR . $name;
		// $name = IS_WIN ? iconv('gb2312', 'utf-8', $name) : $name;

		// 过滤 . 开头的文件
		if($name == '.' || $name == '..' || strpos($name, '.') === 0){
			continue;
		}elseif(is_dir($file)){
			$list[] = array(
				'name' => $name,
				'type' => 'dir',
			);
		}elseif((empty($allowFiles) || preg_match("/\.($allowFiles)$/i", $file)) && is_file($file)){
			$list[] = array(
				'name' => $name,
				'type' => 'file',
			);
		}
	}
	$data = array(
		'path' => strtr(substr($path, strlen(ROOT_PATH) + 1), '\\', '/'),
		'list' => $list,
	);
	return $data;
}


本文出自简爱博客,转载时请注明出处及相应链接。

    发表评论

    电子邮件地址不会被公开。必填项已用*标注