Home > Article > Backend Development > PHP file management source code (1/2)_PHP tutorial
PHP file management source code // This file management source code can create, delete files, upload files, set file permissions, obtain remaining disk space, copy files, modify names, directory management, etc.
php tutorial file management source code
//This file management source code can create, delete files, upload files, set file permissions, obtain remaining disk space, copy files, modify names, directory management, etc. .
/***************************************
*---File Management
*Author: Caigen
*Homepage: http://www.phpvc.com/blog
*Statement: The function of this file refers to the phps tutorial 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();//Get the current working path
$freespaces = disk_free_space($cwddir);//Current remaining disk space
$totalspaces = disk_total_space($cwddir);//Current total space size
//header("content-type: text/html ; charset=utf-8");
//Create a new directory
if($_post['newdirname'])
{
$newdir = $_post['dirname']. $_post['newdirname'];
if(file_exists($newdir))
{
$msg = "The directory: $newdir already exists, please use another name";
}else{
$msg = "Create $newdir".(@mkdir($newdir,0777) ? 'Success' : 'Failure');
}
}
//File upload
if($_post['upload'])
{
$fname = $_files['upfilename'];
$msg = fileupload($fname,$_post['todir']);
}
//Change name
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 = 'Name changed successfully';
}else{
$ msg = 'Cannot be changed, please check relevant configurations and permissions';
}
}else{
$msg = 'The file already exists, please change it to another name! ';
}
}
//Copy file
if($_post['tofile'])
{
$tofile = addslashes($_post['tofile ']);
$oldname = addslashes($_post['oldname']);
if(file_exists($tofile))
{
$msg = 'The file already exists, please replace it with another name! ';
}else{
if(copy($oldname,$tofile))
{
$msg = "Copy file $oldname to $tofile successfully!";
}else{
$msg = "Failed to copy file $oldname to $tofile, please check relevant configuration and permissions!";
}
}
}
//Download file: source code from phpspy2008, because I know nothing about header(), haha
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;
}
}
//Delete a single file
if($_post['delfilename'])
{
$delfilename = addslashes($_post['delfilename']);
if( !file_exists($delfilename))
{
$msg = 'File does not exist! ';
}else{
if(unlink($delfilename))
{
$msg = 'Delete'.$delfilename.'Success';
}else{
$ msg = 'Delete'.$delfilename.'Failed, please check related configuration and permissions';
}
}
}
//Delete folder
if($_post ['deldirname'])
{
$delname = addslashes($_post['deldirname']);
if(deltree($delname))
{
$msg = 'Delete '.$delname.'Success';
}else{
$msg = 'Delete'.$delname.' failed, please check the relevant configuration and permissions';
}
}
//New file
if( $_post['createtofile'])
{
$createfilename = htmlspecialchars(addslashes($_post['createdirname'].$_post['createtofile']));
//echo $createfilename;
if(file_exists($createfilename))
{
$msg = 'File already exists! ';
$createfilename = 0;
}else{
if($creatfhandle = fopen($createfilename,'w'))
{
//echo 'dffdfd';
fclose($creatfhandle);
}else {
$msg = 'File creation failed, please check the relevant configuration and permissions! ';
}
}
}
//Edit file
if($_post['editfilename'])
{
$createfilename = addslashes($ _post['editfilename']);
if(!file_exists($createfilename))
{
$msg = 'File does not exist! ';
$createfilename = 0;
}
}
//Save the edited file
if($_post['editsubmit'])
{
$savefilecontent = htmlspecialchars($_post['fcontent']);
$savefilename = $_post['editfname'];
if($fhandle = fopen($savefilename,'wb'))
{
if(fwrite($fhandle,$savefilecontent))
{
$msg = 'Editing successful! ';
}else{
$msg = 'Editing failed! ';
}
fclose($fhandle);
}
}
//Modify attributes
if ($_post['newperm'])
{
$newperm = $_post['newperm'];
$pfile = addslashes($_post['pfile']);
if (!file_exists($pfile))
{
$msg = 'File does not exist';
} else {
$newperm = base_convert($newperm,8,10);
$msg = 'Modify file attributes'.(@chmod($pfile, $newperm) ? 'Success' : 'Failure');
}
}?>
文件管理
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(' | '.$drivetypedb[$drive->drivetype].'('.$drive->path.')');
} else {
echo(' | '.$drivetypedb[$drive->drivetype].'('.$drive->path.')');
}
}
}
}
?>
1 2
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn