本文目錄一覽:
- 1、php怎麼刪除文件夾和文件夾下的所有文件
- 2、php 編寫 實現上傳圖片至伺服器的函數
- 3、PHP讀取目錄下所有文件內容並顯示
- 4、file_path.$file_name是什麼意思’ title=’php中$this->file_path.$file_name是什麼意思’>php中$this->file_path.$file_name是什麼意思
- 5、php怎麼獲得每個文件夾裡面的文件
php怎麼刪除文件夾和文件夾下的所有文件
正常的思路來說,先循環刪除文件夾下的所有文件,當沒有文件時再刪除文件夾,如果你要刪除文件夾和所有文件直接本地刪除就好了,但是一般沒有這種操作。刪除文件可以用unlink($filepath)函數,$filepath是文件路徑,然後還有一個封裝函數rm_empty_dir($path)刪除所有空目錄:
/** 刪除所有空目錄
* @param String $path 目錄路徑
*/
function rm_empty_dir($path){
if(is_dir($path) ($handle = opendir($path))!==false){
while(($file=readdir($handle))!==false){// 遍歷文件夾
if($file!=’.’ $file!=’..’){
$curfile = $path.’/’.$file;// 當前目錄
if(is_dir($curfile)){// 目錄
rm_empty_dir($curfile);// 如果是目錄則繼續遍歷
if(count(scandir($curfile))==2){//目錄為空,=2是因為.和..存在
rmdir($curfile);// 刪除空目錄
}
}
}
}
closedir($handle);
}
}
具體的還要你自己稍加修改,希望可以幫到你。
php 編寫 實現上傳圖片至伺服器的函數
?php
class FileUpload{
private $filepath; //指定上傳文件保存的路徑
private $allowtype=array(“gif”,”jpg”,”jpeg”,”png”);//允許上傳文件的類型
private $maxsize=1000000;//允許上傳文件的最大值
private $israndname=true;//是否隨機重命名,
private $originName;//源文件名字
private $tmpFileName;//臨時文件名字
private $fileType;//上傳後的文件類型,主要是文件後綴名
private $fileSize;//文件尺寸
private $newFileName;//新文件名字
private $errorName=0;//錯誤號
private $errorMess=””;//用來提供錯誤報告
//用於對上傳文件初始化
//指定上傳路徑 2·允許的類型 3·限制大小 4·是否使用隨機文件名稱
//讓用戶可以不用換位置傳參數,後面參數給值不用按照位置或者必須有值
function __construct($options=array()){
foreach($options as $key=$val){
$key = strtolower($key);
//查看用戶參數中的數組下標是否和成員屬性名相同
//get_class_vars(get_class($this))得到類屬性的數組
//如果$key下標不在這個類屬性的數組中,則退出for循環
if (!in_array($key,get_class_vars(get_class($this)))){
continue;
}
$this – setOption($key,$val);
}
}
private function setOption($key,$val){
//讓實例化後獲取過來的數組下標 = 數組下標的值,這裡即為構造函數初始化
//構造函數中調用,等於把所有屬性初始化,將來可以直接訪問
$this – $key=$val;
}
private function getError(){
$str=”上傳文件{$this-originName}時出錯”;
switch($this – errorNum){
case 4: $str.=”沒有文件被上傳”;
break;
case 3: $str.=”文件只有部分上傳”;
break;
case 2: $str.=”上傳文件超過了表單的值”;
break;
case 1: $str.=”上傳文件超過phpini的值”;
break;
case -1: $str.=”未允許的類型”;
break;
case -2: $str.=”文件過大上傳文件不能超過{$this-maxsize}位元組”;
break;
case -3: $str.=”上傳文件失敗”;
break;
case -4: $str.=”建立存放上傳文件目錄失效,請重新上傳指定目錄”;
break;
case -5: $str.=”必須指定上傳文件的路徑”;
break;
default: $str.=”未知錯誤”;
}
return $str.’br’;
}
//用來檢查文件上傳路徑
private function checkFilePath(){
if(empty($this – filepath)){
$this – setOption(“errorNum”,-5);
return false;
}
if(!file_exists($this – filepath) || !is_writable($this – filepath)){
if(!@mkdir($this – filepath,0755)){
$this – setOption(“errorNum”,-4);
return false;
}
}
return true;
}
//用來檢查上傳文件尺寸大小
private function checkFileSize(){
if($this – fileSize $this -maxsize){
$this – setOption(“errorNum”,-2);
return false;
}else{
return true;
}
}
//用來檢查文件上傳類型
private function checkFileType(){
if(in_array(strtolower($this-fileType),$this – allowtype)){
return true;
}else{
//如果$this-fileType這個類型 不在$this – allowtype這個數組中,則把錯誤號變成-1
$this – setOption(“errorNum”,-1);
return false;
}
}
private function setNewFileName(){
if($this – israndname){
$this – setOption(“newFileName”,$this-preRandName());
}else{
$this – setOption(“newFileName”,$this – originName);
}
}
//用於檢查文件隨機文件名
private function preRandName(){
$fileName=date(“Ymdhis”).rand(100,999);
return $fileName.”.”.$this – fileType;
}
//用來上傳一個文件
function uploadFile($fileField){
//檢查文件路徑
$return = true;
if(!$this – checkFilePath()){
$this – errorMess=$this – getError();
return false;
}//獲取文件信息
$name = $_FILES[$fileField][‘name’];
$tmp_name = $_FILES[$fileField][‘tmp_name’];
$size = $_FILES[$fileField][‘size’];
$error = $_FILES[$fileField][‘error’];
if(is_array($name)){//判斷獲取過來的文件名字是否為數組
$errors=array();//如果為數組則設置為一個數組錯誤號
for($i=0;$icount($name);$i++){
//循環每個文件即每個類屬性賦值或者說初始化屬性值 或者初始化構造函數
if($this-setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i])){
if(!$this-checkFileSize() || !$this-checkFileType()){
//如果上面尺寸或者類型不對,則調用這個錯誤信息
$errors[$i]=$this-getError();
$return=false;
}
}else{
//這裡是
$error[]=$this-getError();
$return=false;
}
if(!$return)
$this-setFiles();
}
if($return){
$fileNames=array();
for($i=0;$icount($name);$i++){
if($this-setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i])){
$this-setNewFileName();
if(!$this-copyFile()){
$errors=$this-getError();
$return=false;
}else{
$fileNames[$i]=$this-newFileName;
}
}
}
$this-newFileName=$fileNames;
}
$this-errorMess=$errors;
return $return;
}else{
//看看$name,$tmp_name,$size,$error這些是否賦值成功 否則返回FALSE
if($this – setFiles($name,$tmp_name,$size,$error)){
//看看文件大小尺寸是否匹配,不匹配返回FALSE
if($this – checkFileSize() $this – checkFileType()){
//獲取新文件名
$this-setNewFileName();
if($this-copyFile()){
return true;
}else{
return false;
}
}else{
$return=false;
}
}else{
$return=false;
}
if(!$return){
$this – errorMess = $this -getError();
return $return;
}
}
}
function copyFile(){//將文件從臨時目錄拷貝到目標文件夾
if(!$this-errorNum){
//如果傳遞來的路徑有斜杠,則刪除斜杠再加上斜杠
//./upload+./
$filepath=rtrim($this-filepath,’/’).’/’;
//./upload+./+加上隨機後的新文件名和後綴
//這裡指創建一個新的$filepath.這個文件 像佔位符但是為空的
$filepath.=$this-newFileName;
//嘗試著把臨時文件$this-tmpFileName移動到$filepath下哪裡覆蓋原來的這個文件
if(@move_uploaded_file($this-tmpFileName,$filepath)){
return true;
}else{
$this-setOption(‘errorNum’,-3);
return false;
}
}else{
return false;
}
}
//這裡是為了其他剩餘的屬性進行初始化操作!
private function setFiles($name=””,$tmp_name=””,$size=0,$error=0){
//這裡給錯誤號賦值
$this – setOption(“errorNum”,$error);
//如果這裡有錯誤,直接返回錯誤
if ($error){
return false;
}
$this – setOption(“originName”,$name);//複製名字為源文件名
$this – setOption(“tmpFileName”,$tmp_name);
$arrstr = explode(“.”,$name);//按點分割文件名,
//取分割後的字元串數組最後一個 並轉換為小寫,賦值為文件類型
$this – setOption(“fileType”,strtolower($arrstr[count($arrstr)-1]));
$this – setOption(“fileSize”,$size);
return true;
}
//用來獲取上傳後的文件名
function getNewFileName(){
return $this – newFileName;
}
//上傳失敗,後則返回這個方法,就可以產看報告
function getErrorMsg(){
return $this – errorMess;
}
}
?
============================調用====================================
?php
require(“FileUpload.class.php”);
//這裡實例化後賦值為數組,數組的下標要對應類中屬性的值,否則不能傳遞值,可以不分先後但是必須一致
$up = new FileUpload(array(‘israndname’=’true’,”filepath”=”./upload/”,’allowtype’=array(‘txt’,’doc’,’jpg’,’gif’),”maxsize”=1000000));
echo ‘pre’;
if($up – uploadFile(“pic”)){
print_r($up – getNewFileName());
} else{
print_r($up – getErrorMsg());
}
echo ‘pre’;
?
——————-HTML————————-
html
head
meta http-quive=”content-type” content=”text/html;charset=utf-8″ /meta
/head
body
form action=”upload.php” method=”post” enctype=”multipart/form-data”
shoppic:input type=”file” name=”pic[]”br
input type=”hidden” name=”MAX_FILE_SIZE” value=”1000000″
input type=”submit” name=”sub” value=”添加商品”
/form
/body
/html
——————-或者HTML————————-
html
head
meta http-quive=”content-type” content=”text/html;charset=utf-8″ /meta
/head
body
form action=”upload.php” method=”post” enctype=”multipart/form-data”
//區別在這裡
shoppic:input type=”file” name=”pic[]”br
shoppic:input type=”file” name=”pic[]”br
shoppic:input type=”file” name=”pic[]”br
input type=”hidden” name=”MAX_FILE_SIZE” value=”1000000″
input type=”submit” name=”sub” value=”添加商品”
/form
/body
/html
=====================================================================
以上是自己總結的 還沒有怎麼精簡加工過,僅供參考
以上不止可以上傳圖片,可以上自定義任何文件
PHP讀取目錄下所有文件內容並顯示
?php
function printFile($filepath)
{
//substr(string,start,length)函數返回字元串的一部分;start規定在字元串的何處開始 ;length規定要返回的字元串長度。默認是直到字元串的結尾。
//strripos(string,find,start)查找 “php” 在字元串中最後一次出現的位置; find為規定要查找的字元;start可選。規定開始搜索的位置
//讀取文件後綴名
//$filetype = substr ( $filename, strripos ( $filename, “.” ) + 1 );
//判斷是不是以txt結尾並且是文件
#if ($filetype == “txt” is_file ( $filepath . “/” . $filename ))
if ( is_file ( $filepath))
{
$filename=iconv(“gb2312″,”utf-8”,$filepath);
echo $filename.”內容如下:”.”br/”;
$fp = fopen ( $filepath, “r” );//打開文件
#while (! feof ( $f )) //一直輸出直到文件結尾
$i = 1;
while ($i 10)
{
$line = fgets ( $fp );
echo $line.”br/”;
$i = $i +1;
}
fclose($fp);
}
}
(此處空一行)
function readFileRecursive($filepath)
{
if (is_dir ( $filepath )) //判斷是不是目錄
{
$dirhandle = opendir ( $filepath );//打開文件夾的句柄
if ($dirhandle)
{
//判斷是不是有子文件或者文件夾
while ( ($filename = readdir ( $dirhandle ))!= false )
{
if ($filename == “.” or $filename == “..”)
{
//echo “目錄為「.」或「..」”.”br/”;
continue;
}
//判斷是否為目錄,如果為目錄遞歸調用函數,否則直接讀取列印文件
if(is_dir ($filepath . “/” . $filename ))
{
readFileRecursive($filepath . “/” . $filename);
}
else
{
//列印文件
printFile($filepath . “/” . $filename);
echo “br/”;
}
}
closedir ( $dirhandle );
}
}
else
{
printFile($filepath . “/” . $filename);
return;
}
}
(此處空一行)
header(“content-type:text/html;charset=utf-8”);
#echo “Hello World”.”br/”;
$filepath = “C:/phpStudy/PHPTutorial/WWW/test/results”; //想要讀取的目錄
readFileRecursive($filepath )
?
擴展資料:
php還可以讀取文件夾下所有圖片,方法如下
hostdir=dirname(__FILE__).’/data/upload/admin/20170517/’; //要讀取的文件夾
(此處空一行)
$url = ‘/data/upload/admin/20170517/’; //圖片所存在的目錄
(此處空一行)
$filesnames = scandir($hostdir); //得到所有的文件
(此處空一行)
// print_r($filesnames);exit;
//獲取也就是掃描文件夾內的文件及文件夾名存入數組 $filesnames
(此處空一行)
$www = ‘.***.com/’; //域名
(此處空一行)
foreach ($filesnames as $name) {
$aurl= “img width=’100′ height=’100′ src='”.$.”‘ alt = ‘”.$name.”‘”; //圖片
echo $aurl . “br/”; //輸出他
file_path.$file_name是什麼意思’>php中$this->file_path.$file_name是什麼意思
$this-file_path 是類裡面的一個對像,應該是保存著一個目錄地址
$file_name 是文件名,把這兩個組合起來就是一個完整的文件路徑。
相當於把兩個變數合成一個的意思。
php怎麼獲得每個文件夾裡面的文件
?php
$dir = “./images/”; //要獲取的目錄
echo “********** 獲取目錄下所有文件和文件夾 ***********hr/”;
//先判斷指定的路徑是不是一個文件夾
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh))!= false){
//文件名的全路徑 包含文件名
$filePath = $dir.$file;
echo “img src='”.$filePath.”‘/”;
}
closedir($dh);
}
}
?
原創文章,作者:QFFUV,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/313373.html