php檔案管理原始碼(1/2)_PHP教程

來源:互聯網
上載者:User
php檔案管理原始碼//這款檔案管理源碼是一款可以建立,刪除檔案,上傳檔案,設定檔案許可權,擷取磁碟剩餘空間,檔案複製,修改名稱,目錄管理,等。

php教程檔案管理原始碼
//這款檔案管理源碼是一款可以建立,刪除檔案,上傳檔案,設定檔案許可權,擷取磁碟剩餘空間,檔案複製,修改名稱,目錄管理,等。
/*************************************
*---檔案管理
*作者:菜根
*首頁:http://www.phpvc.com/blog
*聲明:此檔案功能參照於phps教程py,
************************************/

if(function_exists('date_default_timezone_set'))
{
@date_default_timezone_set('asia/shanghai');
}

define('is_win', directory_separator == '');
define('is_com', class_exists('com') ? 1 : 0 );
define('web_root', str_replace('', '/', dirname(__file__)).'/');
$phpself = htmlspecialchars($_server['php_self'] ? $_server['php_self'] : $_server['script_name']);
$msg = '';

if(!empty($_post['cwddir']))
{
$_post['cwddir'] = addslashes($_post['cwddir']);
if(is_dir($_post['cwddir']))
chdir($_post['cwddir']);
}

$cwddir = getcwddir();//擷取當前工作路徑

$freespaces = disk_free_space($cwddir);//當前磁碟剩餘空間
$totalspaces = disk_total_space($cwddir);//當前總空間大小
//header("content-type: text/html; charset=utf-8");

//建立目錄
if($_post['newdirname'])
{
$newdir = $_post['dirname'].$_post['newdirname'];
if(file_exists($newdir))
{
$msg = "該目錄:$newdir 已存在,請用其它名字";
}else{
$msg = "建立$newdir".(@mkdir($newdir,0777) ? '成功' : '失敗');
}
}

//檔案上傳
if($_post['upload'])
{
$fname = $_files['upfilename'];
$msg = fileupload($fname,$_post['todir']);
}

//更改名稱
if($_post['newname'])
{
$newname = addslashes($_post['newname']);
$oldname = addslashes($_post['oldname']);
$dirname = addslashes($_post['dirname']);
$newname = $dirname.$newname;
if(!file_exists($newname))
{
if(rename($oldname,$newname))
{
$msg = '更改名稱成功';
}else{
$msg = '無法更改,請檢查相關配置和許可權';
}
}else{
$msg = '檔案已存在,請更換其它名稱!';
}
}

//複製檔案
if($_post['tofile'])
{
$tofile = addslashes($_post['tofile']);
$oldname = addslashes($_post['oldname']);
if(file_exists($tofile))
{
$msg = '檔案已存在,請更換其它名稱!';
}else{
if(copy($oldname,$tofile))
{
$msg = "複製檔案 $oldname 到 $tofile 成功!";
}else{
$msg = "複製檔案 $oldname 到 $tofile 失敗,請檢查相關配置和許可權!";
}
}
}

//下載檔案:源碼來自phpspy2008,因為我對header()一竅不通,呵呵
if($_post['dfile'])
{
$thefile = addslashes($_post['dfile']);
//header("location:$dfile");
if (!@file_exists($thefile)) {
$errmsg = 'the file you want downloadable was nonexistent';
} else {
$fileinfo = pathinfo($thefile);
header('content-type: application/x-'.$fileinfo['extension']);
header('content-disposition: attachment; filename='.$fileinfo['basename']);
header('content-length: '.filesize($thefile));
@readfile($thefile);
exit;
}
}

//刪除單個檔案
if($_post['delfilename'])
{
$delfilename = addslashes($_post['delfilename']);
if(!file_exists($delfilename))
{
$msg = '檔案不存在!';
}else{
if(unlink($delfilename))
{
$msg = '刪除'.$delfilename.'成功';
}else{
$msg = '刪除'.$delfilename.'失敗,請檢查相關配置和許可權';
}
}
}

//刪除檔案夾
if($_post['deldirname'])
{
$delname = addslashes($_post['deldirname']);
if(deltree($delname))
{
$msg = '刪除'.$delname.'成功';
}else{
$msg = '刪除'.$delname.'失敗,請檢查相關配置和許可權';
}
}

//建立檔案
if($_post['createtofile'])
{
$createfilename = htmlspecialchars(addslashes($_post['createdirname'].$_post['createtofile']));
//echo $createfilename;
if(file_exists($createfilename))
{
$msg = '檔案已存在!';
$createfilename = 0;
}else{
if($creatfhandle = fopen($createfilename,'w'))
{
//echo 'dffdfd';
fclose($creatfhandle);
}else {
$msg = '檔案建立失敗,請檢查相關配置和許可權!';
}
}
}

//編輯檔案
if($_post['editfilename'])
{
$createfilename = addslashes($_post['editfilename']);
if(!file_exists($createfilename))
{
$msg = '檔案不存在!';
$createfilename = 0;
}
}

//儲存編輯過的檔案
if($_post['editsubmit'])
{
$savefilecontent = htmlspecialchars($_post['fcontent']);
$savefilename = $_post['editfname'];
if($fhandle = fopen($savefilename,'wb'))
{
if(fwrite($fhandle,$savefilecontent))
{
$msg = '編輯成功!';
}else{
$msg = '編輯失敗!';
}
fclose($fhandle);
}
}

//修改屬性
if ($_post['newperm'])
{
$newperm = $_post['newperm'];
$pfile = addslashes($_post['pfile']);
if (!file_exists($pfile))
{
$msg = '檔案不存在';
} else {
$newperm = base_convert($newperm,8,10);
$msg = '修改檔案屬性 '.(@chmod($pfile,$newperm) ? '成功' : '失敗');
}
}

?>




檔案管理






');">webroot
|
create directory
|
create file
if (is_win && is_com) {
//此代碼來源於phpspy
$obj = new com('scripting.filesystemobject');
if ($obj && is_object($obj)) {
$drivetypedb = array(0 => 'unknow',1 => 'removable',2 => 'fixed',3 => 'network',4 => 'cdrom',5 => 'ram disk');
foreach($obj->drives as $drive) {
if ($drive->drivetype == 2) {
echo(' | path.'/');" title="size:'.realsize($drive->totalsize).' free:'.realsize($drive->freespace).' type:'.$drivetypedb[$drive->drivetype].'">'.$drivetypedb[$drive->drivetype].'('.$drive->path.')');
} else {
echo(' | path.'/');" title="type:'.$drivetypedb[$drive->drivetype].'">'.$drivetypedb[$drive->drivetype].'('.$drive->path.')');
}
}
}
}
?>























1 2

http://www.bkjia.com/PHPjc/444875.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444875.htmlTechArticlephp檔案管理原始碼//這款檔案管理源碼是一款可以建立,刪除檔案,上傳檔案,設定檔案許可權,擷取磁碟剩餘空間,檔案複製,修改名稱,目...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.