<?php
header('content-type:text/html;charset=utf-8');
/*
* 注 touch 如文件已存在,設定文件的訪問和修改時間 如果文件不存在,則會被創建。
* touch(文件名,時間戳); 時間戳默認為當前時間 返回布爾值
* */
//
//創建文件 123.txt(如果文件不存在)
if(!file_exists('123.txt')){
touch('123.txt');
};
/*
* 刪除文件unlink(文件名)返回布爾值
* */
//刪除文件123.txt(如果文件存在)
if(file_exists('123.txt')){
unlink('123.txt');
};
/*
* rename重命名文件或剪切文件
* rename(文件名,新文件名) 返回布爾值
* */
if(!file_exists('aaa.txt')){//aaa.text文件不存則創建
touch('aaa.txt');
}
rename('aaa.txt','bbb.txt');//把aaa.text 重命名為bbb.text
if(!file_exists('test')){//當前目錄創建test文件夾
mkdir('test',777);
}
//DIRECTORY_SEPARATOR 目錄分隔符
rename('bbb.txt','test'.DIRECTORY_SEPARATOR.'bbb.txt');//將bbb.text移動到test文件夾*/
/*
* copy(路徑,目標路徑)復制文件 返回布爾值
* 如果目標文件已存在,將會被覆蓋。
*
* */
copy('test'.DIRECTORY_SEPARATOR.'bbb.txt','bbb.txt'); //將test目錄的bbb.txt文件拷貝到當前目錄
//拷貝遠程圖片 要在php.ini 中開啟 allow_url_fopen (默認是開啟的)
copy('http://c.hiphotos.baidu.com/baike/pic/item/91ef76c6a7efce1b27893518a451f3deb58f6546.jpg','./test/a.jpg');
/**
* 創建文件方法
* @method createFile
* @param string $filename 文件名
* @return bool
*/
function createFile($filename){
//檢測文件是否存在 不存在則創建
if(file_exists($filename)){
return false;
}
//檢測目錄是否存在不存在則創建
if(!is_dir(dirname($filename))){
mkdir(dirname($filename));
}
//創建文件 touch 創建 或用 file_put_contents 創建
if(touch($filename)){
return true;
}
/* if(file_put_contents($filename,'')!==false){
return true;
}*/
return false;
}
/**
* 刪除文件方法
* @method deleteFile
* @param string $filename 文件名
* @return bool
*/
function deleteFile($filename){
//檢測文件存在
if(!file_exists($filename) ){
return false;
}
if(unlink($filename)){
return true;
}
return false;
}
/**
* 復制文件方法
* @method copyFile
* @param string $filename 源文件名
* @param string $dest 目標目錄
* @return bool
*/
function copyFile($filename,$dest){
//檢測文件是否存在
if(!file_exists($filename)){
return false;
}
//檢測目標目錄是否存在 不存在則創建
if(!is_dir($dest)){
mkdir($dest,0777,true);
}
//復制后的文件路徑
$newFilePath=$dest.DIRECTORY_SEPARATOR.basename($filename);
//檢測目標路徑是否已存在同名文件
if(file_exists($newFilePath)){
return false;
}
//復制文件
if(copy($filename,$newFilePath)){
return true;
};
return false;
}
/**
* 剪切文件方法
* @method cutFile
* @param string $filename 源文件名
* @param string $dest 目標目錄
* @return bool
*/
function cutFile($filename,$dest){
//檢測文件是否存在
if(!file_exists($filename)){
return false;
}
//檢測目標目錄是否存在 不存在則創建
if(!is_dir($dest)){
mkdir($dest,0777,true);
}
//剪切后的文件路徑
$newFilePath=$dest.DIRECTORY_SEPARATOR.basename($filename);
//檢測目標路徑是否已存在同名文件
if(file_exists($newFilePath)){
return false;
}
//剪切文件
if(rename($filename,$newFilePath)){
return true;
};
return false;
}
/**
* 重命名文件方法
* @method renameFile
* @param string $oldName 原文件名
* @param string $newName 新文件名
* @return bool
*/
function renameFile($oldName ,$newName){
//檢測文件是否存在
if(!file_exists($oldName)){
return false;
}
//得到原文件路徑
$path=dirname($oldName);
//重命名后的文件路徑
$newFilePath=$path.DIRECTORY_SEPARATOR.$newName;
//檢測是否有重名文件
if(file_exists($newFilePath)){
return false;
}
//重命名(注意是$newName 不是$newFilePath)
if(rename($oldName,$newName)){
return true;
};
return false;
}