修改代码
请选择要上传的文件
function getExt($filename){
return strtolower(pathinfo($filename,PATHINFO_EXTENSION));
}
function getUniqidName($length=10){
//uniqid 如果 prefix 参数为空,则返回的字符串有 13 个字符串长。
//如果 more_entropy 参数设置为 true,则是 23 个字符串长
return substr(md5(uniqid(microtime(true),true)),0,$length);
}
function uploadFile($fileInfo, $path, $allowExt = array('gif','jpeg','jpg','png','txt'), $maxSize = 10485760) {
// 判断错误号
if ($fileInfo ['error'] == UPLOAD_ERR_OK) {
// 文件是否是通过HTTP POST方式上传上来的
if (is_uploaded_file ( $fileInfo ['tmp_name'] )) {
// 上传的文件的文件名,只允许上传jpeg png gif txt
$ext = getExt ( $fileInfo ['name'] );
$uniqid = getUniqidName ();
$destination = $path . "/" . pathinfo ( $fileInfo ['name'], PATHINFO_FILENAME ) . "_" . $uniqid . "." . $ext;
if (in_array ( $ext, $allowExt )) {
// 10M
if ($fileInfo ['size'] <= $maxSize) {
if (move_uploaded_file ( $fileInfo ['tmp_name'], $destination )) {
$mes = "文件上传成功";
} else {
$mes = "文件过大";
}
} else {
$mes = "文件过大";
}
} else {
$mes = "非法文件类型";
}
} else {
$mes = "文件不是通过HTTP POST方式上传上来的";
}
} else {
switch ($fileInfo ['error']) {
case 1 :
$mes = "超过了配置文件的大小";
break;
case 2 :
$mes = "超过了表单允许接收数据的大小";
break;
case 3 :
$mes = "文件部分被上传";
break;
case 4 :
$mes = "没有文件被上传";
break;
}
}
return $mes;
}
else if($act=='上传文件'){
/*
Array
(
[myFile] => Array
(
[name] => Git及SVN地址.txt
[type] => text/plain
[tmp_name] => C:\wamp\tmp\phpA6E9.tmp
[error] => 0
[size] => 205
)
)
*
*/
//print_r($_FILES);
$fileInfo=$_FILES['myFile'];
$mes=uploadFile($fileInfo,$path);
alertMes($mes, $redirect);
}
common.func.php
alert('$mes');location.href='$url'";
}
function getExt($filename) {
return strtolower ( pathinfo ( $filename, PATHINFO_EXTENSION ) );
}
function getUniqidName($length = 10) {
// uniqid 如果 prefix 参数为空,则返回的字符串有 13 个字符串长。
// 如果 more_entropy 参数设置为 true,则是 23 个字符串长
return substr ( md5 ( uniqid ( microtime ( true ), true ) ), 0, $length );
}
?>
file.func.php
1024 ) {
$size /= 1024;
$i ++;
}
return round ( $size, 2 ) . $arr [$i];
}
function createFile($filename) {
// file/1.txt
// 验证文件名的合法性,是否包含/,*,<,>,?,|
$pattern = "/[\/,\*,<>,\?\|]/";
// basename 返回路径中的文件名部分
if (! (preg_match ( $pattern, basename ( $filename ) ))) {
// 检测当前目录下是否存在同名文件
if (! file_exists ( $filename )) {
// 通过touch创建
if (touch ( $filename )) {
return "创建文件成功";
} else {
return "创建文件失败";
}
} else {
return "文件已存在";
}
} else {
return "包含非法字符";
}
}
function renameFile($oldname, $newname) {
// 验证文件名是否合法
if (checkFilename ( $newname )) {
// 检测当前目录下是否存在同名文件
$path = dirname ( $oldname );
if (! file_exists ( $path . "/" . $newname )) {
// 进行重命名
if (rename ( $oldname, $path . "/" . $newname )) {
return "重命名成功";
} else {
return "重命名失败";
}
} else {
return "存在同名文件,请重新命名";
}
} else {
return "非法文件名";
}
}
function checkFilename($filename) {
// 验证文件名的合法性,是否包含/,*,<,>,?,|
$pattern = "/[\/,\*,<>,\?\|]/";
// basename 返回路径中的文件名部分
if (preg_match ( $pattern, $filename )) {
return false;
} else {
return true;
}
}
function deleteFile($filename) {
if (unlink ( $filename )) {
$mes = "文件删除成功";
} else {
$mes = "文件删除失败";
}
return $mes;
}
function downFile($filename) {
header ( "content-disposition:attachment;filename=" . basename ( $filename ) );
header ( "content-length:" . filesize ( $filename ) );
readfile ( $filename );
}
function copyFile($filename, $dstname) {
if (file_exists ( $dstname )) {
if (! file_exists ( $dstname . "/" . basename ( $filename ) )) {
if (copy ( $filename, $dstname . "/" . basename ( $filename ) )) {
$mes = "文件复制成功";
} else {
$mes = "文件复制失败";
}
} else {
$mes = "存在同名文件";
}
} else {
$mes = "目标目录不存在";
}
return $mes;
}
function cutFile($filename, $dstname) {
if (file_exists ( $dstname )) {
if (! file_exists ( $dstname . "/" . basename ( $filename ) )) {
if (rename ( $filename, $dstname . "/" . basename ( $filename ) )) {
$mes = "文件剪切成功";
} else {
$mes = "文件剪切失败";
}
} else {
$mes = "存在同名文件";
}
} else {
$mes = "目标目录不存在";
}
return $mes;
}
function uploadFile($fileInfo, $path, $allowExt = array('gif','jpeg','jpg','png','txt'), $maxSize = 10485760) {
// 判断错误号
if ($fileInfo ['error'] == UPLOAD_ERR_OK) {
// 文件是否是通过HTTP POST方式上传上来的
if (is_uploaded_file ( $fileInfo ['tmp_name'] )) {
// 上传的文件的文件名,只允许上传jpeg png gif txt
$ext = getExt ( $fileInfo ['name'] );
$uniqid = getUniqidName ();
$destination = $path . "/" . pathinfo ( $fileInfo ['name'], PATHINFO_FILENAME ) . "_" . $uniqid . "." . $ext;
if (in_array ( $ext, $allowExt )) {
// 10M
if ($fileInfo ['size'] <= $maxSize) {
if (move_uploaded_file ( $fileInfo ['tmp_name'], $destination )) {
$mes = "文件上传成功";
} else {
$mes = "文件过大";
}
} else {
$mes = "文件过大";
}
} else {
$mes = "非法文件类型";
}
} else {
$mes = "文件不是通过HTTP POST方式上传上来的";
}
} else {
switch ($fileInfo ['error']) {
case 1 :
$mes = "超过了配置文件的大小";
break;
case 2 :
$mes = "超过了表单允许接收数据的大小";
break;
case 3 :
$mes = "文件部分被上传";
break;
case 4 :
$mes = "没有文件被上传";
break;
}
}
return $mes;
}
?>
index.php
";
// echo $filename;
if (! (isset ( $arr ['file'] )) && ! (isset ( $arr ['dir'] ))) {
echo "";
}
if ($act == '创建文件') {
$mes = createFile ( $path . "/" . $filename );
alertMes ( $mes, $redirect );
} else if ($act == 'showContent') {
// 查看文件内容
$content = file_get_contents ( $filename );
if (strlen ( $content )) {
// textarea能完整显示代码
// echo "";
// 高亮显示字符创中的PHP代码 true不自动输出(echo)
$newContent = highlight_string ( $content, true );
// 高亮显示文件中的PHP代码
// $content=highlight_file($filename,true);
$str = <<
{$newContent}
EOF;
echo $str;
} else {
alertMes ( "文件没有内容,请编辑再查看!", $redirect );
}
} else if ($act == 'editContent') {
$content = file_get_contents ( $filename );
$str = <<
EOF;
echo $str;
} else if ($act == "doEdit") {
// 修改文件内容的操作
$content = $_REQUEST ['content'];
// echo $content;
if (file_put_contents ( $filename, $content )) {
$mes = "文件修改成功";
} else {
$mes = "文件修改失败";
}
alertMes ( $mes, $redirect );
} else if ($act == "renameFile") {
// 完成重命名
$str = <<
请填写新文件名:
EOF;
echo $str;
} else if ($act == 'doRename') {
// 实现重命名操作
$newname = $_REQUEST ['newname'];
$mes = renameFile ( $filename, $newname );
alertMes ( $mes, $redirect );
} else if ($act == 'delFile') {
$mes = deleteFile ( $filename );
alertMes ( $mes, $redirect );
} else if ($act == 'downFile') {
// 完成下载的操作
$mes = downFile ( $filename );
} else if ($act == "copyFolder") {
$str = <<
将文件夹复制到:
EOF;
echo $str;
} else if ($act == 'doCopyFolder') {
$dstname = $_REQUEST ['dstname'];
$mes = copyFolder ( $dirname, $path . "/" . $dstname . "/" . basename ( $dirname ) );
alertMes ( $mes, $redirect );
} else if ($act == 'renameFolder') {
$str = <<
请填写新文件夹名称:
EOF;
echo $str;
} else if ($act == 'doRenameFolder') {
$newname = $_REQUEST ['newname'];
// imooc222
// file/aa/imooc1
// file/aa
// echo $newname." ".$dirname." ".$path;
$mes = renameFolder ( $dirname, $path . "/" . $newname );
alertMes ( $mes, $redirect );
} else if ($act == 'cutFolder') {
$str = <<
将文件剪切到:
EOF;
echo $str;
} else if ($act == "doCutFolder") {
$dstname = $_REQUEST ['dstname'];
$mes = cutFolders ( $dirname, $path . "/" . $dstname );
alertMes ( $mes, $redirect );
} else if ($act == "delFolder") {
$mes = delFolder ( $dirname );
alertMes ( $mes, $redirect );
} else if ($act == 'copyFile') {
$str = <<
将文件复制到:
EOF;
echo $str;
} else if ($act == 'doCopyFile') {
$dstname = $_REQUEST ['dstname'];
// echo $dirname." ".$path."/".$dstname;
$mes = copyFile ( $dirname, $path . "/" . $dstname );
alertMes ( $mes, $redirect );
} else if ($act == 'cutFile') {
$str = <<
将文件剪切到:
EOF;
echo $str;
} else if ($act == 'doCutFile') {
$dstname = $_REQUEST ['dstname'];
// echo $dirname." ".$path."/".$dstname;
$mes = cutFile ( $dirname, $path . "/" . $dstname );
alertMes ( $mes, $redirect );
} else if ($act == '上传文件') {
/*
* Array ( [myFile] => Array ( [name] => Git及SVN地址.txt [type] => text/plain [tmp_name] => C:\wamp\tmp\phpA6E9.tmp [error] => 0 [size] => 205 ) )
*/
// print_r($_FILES);
$fileInfo = $_FILES ['myFile'];
$mes = uploadFile ( $fileInfo, $path );
alertMes ( $mes, $redirect );
}
?>
Insert title here
慕课网-在线文件管理器