可以用遞歸的方式,還有別的方式能實(shí)現(xiàn)

在香坊等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站建設(shè)、網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作專業(yè)公司,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),全網(wǎng)營(yíng)銷推廣,外貿(mào)網(wǎng)站建設(shè),香坊網(wǎng)站建設(shè)費(fèi)用合理。
function my_dir($dir)
{
$files = array();
if (@$handle = opendir($dir)) {
while (($file = readdir($handle)) !== false) {
if ($file != ".." $file != ".") {
if (is_dir($dir . "/" . $file)) {
$files[$file] = my_dir($dir . "/" . $file);
} else {
$files[] = $file;
}
}
}
closedir($handle);
return $files;
}
}
?php
$origin_str = file_get_contents('路徑/文件.txt');
$update_str = str_replace('qwe=0', 'qwe=1', $orgin_str);
file_put_contents('路徑/文件.txt', $update_str);
?
例如這樣兩個(gè)文件:
/aa/bb/cc/test1.php
/aa/test2.php
用test1.php處理test2.php
給test2.php改個(gè)名字 改成 test3.php
? //test1.php
rename("../../test2.php","../../test3.php");
?
有那個(gè)文件的名字,和具體路徑(相對(duì)路徑和絕對(duì)路徑都可以的),直接改就好了,和同一個(gè)目錄沒有什么區(qū)別呀!
你如果是在什么環(huán)境,windows還是;linux下,如果是windows下$hostdir=dirname(__FILE__)."/7";得用反斜杠,記得轉(zhuǎn)義
PHP 中的 file_get_contents() 與 file_put_contents() 函數(shù)可以實(shí)現(xiàn)
file_get_contents() 函數(shù)把整個(gè)文件讀入一個(gè)字符串中。
file_get_contents() 函數(shù)是用于將文件的內(nèi)容讀入到一個(gè)字符串中的首選方法。
file_get_contents(path,include_path,context,start,max_length)
參數(shù)說明
path 必需。規(guī)定要讀取的文件。
include_path 可選。如果也想在 include_path 中搜尋文件的話,可以將該參數(shù)設(shè)為 "1"。
context 可選。規(guī)定文件句柄的環(huán)境。
context 是一套可以修改流的行為的選項(xiàng)。若使用 null,則忽略。
start 可選。規(guī)定在文件中開始讀取的位置。該參數(shù)是 PHP 5.1 新加的。
max_length 可選。規(guī)定讀取的字節(jié)數(shù)。該參數(shù)是 PHP 5.1 新加的。
對(duì) context 參數(shù)的支持是 PHP 5.0.0 添加的。
注釋:本函數(shù)可安全用于二進(jìn)制對(duì)象。
file_put_contents() 函數(shù)把一個(gè)字符串寫入文件中。
file_put_contents(file,data,mode,context)
參數(shù)說明
file 必需。規(guī)定要寫入數(shù)據(jù)的文件。如果文件不存在,則創(chuàng)建一個(gè)新文件。
data 可選。規(guī)定要寫入文件的數(shù)據(jù)。可以是字符串、數(shù)組或數(shù)據(jù)流。
注釋:本函數(shù)可安全用于二進(jìn)制對(duì)象。
例如:
需要修改的php文件 index.php (前提條件此文件需要有寫入的權(quán)限)
?php
$str = 'abc123';
?
處理的文件 update.php
?php
$conents = file_get_contents("index.php");
$conents = str_replace('abc','efg',$conents);
file_put_contents("index.php",$conents);
?
修改后的index.php 文件
?php
$str = 'efg123';
?
舉例如下:
創(chuàng)建userinfo_update.php頁(yè)面用于查詢用戶信息,先顯示信息,在修改:
先通過GET獲取用戶編號(hào)查詢用戶信息:
$sql = "select * from user_info where user_id='".$_GET['userId']."'";
$result = mysql_query($sql,$con);
if($row = mysql_fetch_array($result)){
}
頁(yè)面效果:
創(chuàng)建update.php文件,用于修改用戶信息:
使用到了mysql_affected_rows() 函數(shù)返回前一次 MySQL 操作所影響的記錄行數(shù)。
//通過post獲取頁(yè)面提交數(shù)據(jù)信息
$userId = $_POST[userId];
$userName = $_POST[userName];
$userAge = $_POST[userAge];
$sql = "update user_info set user_name='".$userName."',user_age=".$userAge." where user_id='".$userId."'";
mysql_query($sql,$conn);//執(zhí)行SQL
$mark? = mysql_affected_rows();//返回影響行數(shù)
$url = "userinf_select.php";
運(yùn)行結(jié)果
創(chuàng)建delete.php文件,完成刪除用戶信息功能:
$userId = $_GET['userId'];
include 'connection.php';
$sql = "delete from user_info where user_id='".$userId."'";
mysql_query($sql,$con);
$mark? = mysql_affected_rows();//返回影響行數(shù)
if($mark0){
echo "刪除成功";
}else{
echo? "刪除失敗";
}
mysql_close($con);
運(yùn)行結(jié)果:
當(dāng)前題目:php修改目錄子文件數(shù)據(jù) php修改文件夾名稱
分享鏈接:http://chinadenli.net/article26/dodpdcg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、標(biāo)簽優(yōu)化、定制開發(fā)、ChatGPT、網(wǎng)站策劃、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)