Heim > Artikel > Backend-Entwicklung > 教你简单实现PHP文件管理_PHP教程
如何能够利用
众所周知,用FTP上传,复制,删除大量文件是相当慢的,传个wordpress就要半个小时以上,删掉它也要半小时。。。而用简单的php代码上传再打开网页,3秒不到即可搞定上述过程,实在很爽(尤其对于偶这种手机玩网站的…)
注:将文中代码复制保存为xxx.php文件上传到空间打开即可(需要修改的对应修改下)
PHP文件管理1.上传和解压代码
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li><span>exec("fetch http://cn.wordpress<br>.org/wordpress-2.8-zh_CN.zip"); </span></li> <li class="alt"><span>exec("tar xvfz wordpress-2.8-<br>zh_CN.zip"); </span></li> <li> <span class="tag">?></span><span> </span> </li> </ol>
这个就是wordpress的上传解压代码,也可以单独用以下代码来解压文件(同样地也可以获取网络文件)
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li><span>exec("tar xvfz wordpress<br>-2.8-zh_CN.zip"); </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
PHP文件管理2.复制代码
通常上面的解压会解压到一个新文件夹,而如果想复制到根目录的话,就可以用以下代码了(移动文件的也有,不过为作备份就不介绍了)(这里以神龙空间为例,路径需要自己改)
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ? </span></span></span></li> <li><span>exec("cp -Rf /usr/virtual/leo2351960<br>/flysh.co.tv/public_html/wordpress/<br> /usr/virtual/leo2351960/flysh.co.<br>tv/public_html/"); </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
上面这个就是把wp文件夹复制到根目录的代码,有两个路径,前面的是需要移动的文件(夹),后面是目标路径,路径须为绝对路径
PHP文件管理3.删除文件(夹)
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ? </span></span></span></li> <li><span>exec("rm -r /usr/virtual/<br>leo2351960/flysh.co.tv/<br>public_html/wordpress"); </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
注意,谨慎删除,此动作太快,后悔来不及(上次不小心删了个文件夹搞得重装一遍)
PHP文件管理4.修改文件(夹)权限
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li><span>exec("chmod -R 777 /v /usr/virtual<br>/leo2351960/flysh.co.tv/public_html/xxx.php"); </span></li> <li class="alt"><span>exec("chmod -R 777 /v /usr/virtual<br>/leo2351960/flysh.co.tv/public_html/up"); </span></li> <li> <span class="tag">?></span><span> </span> </li> </ol>
对应修改下即可完成大量文件文件夹的权限修改,方便!
PHP文件管理5.批量删除相同前缀数据库表
这个是free8的007兄弟找到的(他找到了还pm我,真是好人啊~),顺便贴上
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li><span>function deldata($dbname,$tableflag){ </span></li> <li class="alt"> <span>$</span><span class="attribute">db_host</span><span> = </span><span class="attribute-value">'localhost'</span><span>; </span> </li> <li> <span>$</span><span class="attribute">db_port</span><span> = </span><span class="attribute-value">'3306'</span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute">db_user</span><span> = </span><span class="attribute-value">'username'</span><span>; </span> </li> <li> <span>$</span><span class="attribute">db_pass</span><span> = </span><span class="attribute-value">'password'</span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute">connect</span><span> =</span><span class="attribute-value">mysql_connect</span><span>($db_host,<br>$db_user,$db_pass); </span> </li> <li><span>mysql_select_db($dbname); </span></li> <li class="alt"> <span>$</span><span class="attribute">result</span><span> = </span><span class="attribute-value">mysql_query</span><span>("show table <br>status from $dbname",$connect); </span> </li> <li> <span>$</span><span class="attribute">data</span><span>=</span><span class="attribute-value">mysql_fetch_array</span><span>($result); </span> </li> <li class="alt"> <span>while($</span><span class="attribute">data</span><span>=</span><span class="attribute-value">mysql_fetch_array</span><span>($result)) { </span> </li> <li> <span>$</span><span class="attribute">table</span><span>=</span><span class="attribute-value">mysubstr</span><span>($data[Name],"_"); </span> </li> <li class="alt"> <span>if($</span><span class="attribute">table</span><span>==$tableflag){ </span> </li> <li><span>//测试之用 </span></li> <li class="alt"><span>/*echo $data[Name]; </span></li> <li> <span>echo "</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span>"; </span></span> </li> <li class="alt"><span>echo $table; </span></li> <li> <span>echo "</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span>";*/ </span></span> </li> <li class="alt"><span>mysql_query("drop table $data[Name]"); </span></li> <li><span>} </span></li> <li class="alt"><span>} </span></li> <li><span>return true; </span></li> <li class="alt"><span>} </span></li> <li><span>/*截取某个特定字符前的所有字符函数 </span></li> <li class="alt"><span>*$str 为待截取字符串 </span></li> <li><span>*$flag 特定字符如“_” </span></li> <li class="alt"><span>*/ </span></li> <li><span>function mysubstr($str,$flag){ </span></li> <li class="alt"> <span>$</span><span class="attribute">pos</span><span>=</span><span class="attribute-value">strpos</span><span>($str,$flag); </span> </li> <li><span>return substr($str,0,$pos); </span></li> <li class="alt"><span>} </span></li> <li> <span class="tag">?></span><span> </span> </li> </ol>
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php //test </span></span></span></li> <li> <span>$</span><span class="attribute">dbname</span><span>=</span><span class="attribute-value">"database"</span><span>; </span> </li> <li class="alt"><span>//数据库名 </span></li> <li> <span>$</span><span class="attribute">tableflag</span><span>=</span><span class="attribute-value">"wp"</span><span>; </span> </li> <li class="alt"><span>//要删除的表的前缀,有这个前缀的表才删除<br>,标志出是哪一个用户 </span></li> <li><span>deldata($dbname,$tableflag); </span></li> <li class="alt"> <span>//$</span><span class="attribute">test</span><span>=</span><span class="attribute-value">mysubstr</span><span>("cdb_account_log","_"); </span> </li> <li><span>//echo $test; </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
注意修改上面的username和password还有底下的database和表前缀
OK了,以上就是PHP文件管理一些实用的代码,还有很多大家自己找了,偶最喜欢快捷方便的玩意儿~