How to manage PHP files

Source: Internet
Author: User

How to Use

As we all know, using FTP to upload, copy, and delete a large number of files is quite slow. It takes more than half an hour to upload a wordpress, and it takes half an hour to delete it... The above process can be completed in less than 3 seconds after the web page is uploaded with simple php code (especially for websites such as mobile phones ...)

Note: Copy and save the code in this article as a xxx. php file and upload it to the space to open it (modify the corresponding modification)

PHP File Management 1. Upload and decompress the code

 
 
  1. < ?php  
  2. exec("fetch http://cn.wordpress
    .org/wordpress-2.8-zh_CN.zip");  
  3. exec("tar xvfz wordpress-2.8-
    zh_CN.zip");  
  4. ?> 


This is the wordpress upload and decompress code. You can also use the following code to decompress the file (you can also obtain network files)

 
 
  1. < ?php  
  2. exec("tar xvfz wordpress
    -2.8-zh_CN.zip");  
  3. ?> 


PHP File Management 2. Copy the code

Generally, the above extraction will be decompressed to a new folder, and if you want to copy it to the root directory, you can use the following code (also available for mobile files, but this will not be introduced for backup) (here, the X-Dragon space is used as an example. You need to change the path yourself)

 
 
  1. < ?  
  2. exec("cp -Rf /usr/virtual/leo2351960
    /flysh.co.tv/public_html/wordpress/
     /usr/virtual/leo2351960/flysh.co.
    tv/public_html/");  
  3. ?> 


The above code copies the wp folder to the root directory. There are two paths in front of the file to be moved (folder), followed by the target path, and the path must be an absolute path.

PHP File Management 3. delete a file (folder)

 
 
  1. < ?  
  2. exec("rm -r /usr/virtual/
    leo2351960/flysh.co.tv/
    public_html/wordpress");  
  3. ?> 

Note: delete the folder with caution. This action is too fast and you have no time to regret it (You have to reload the folder accidentally deleted last time)

PHP File Management 4. Modify file (folder) Permissions

 
 
  1. < ?php  
  2. exec("chmod -R 777 /v /usr/virtual
    /leo2351960/flysh.co.tv/public_html/xxx.php");  
  3. exec("chmod -R 777 /v /usr/virtual
    /leo2351960/flysh.co.tv/public_html/up");  
  4. ?> 

Modify the permissions of a large number of file folders!

PHP file management 5. Batch delete database tables with the same prefix

This was found by the 007 brother of free8 (he found pm and I, really good guy ~), Paste it by the way

 
 
  1. <? Php
  2. Function deldata ($ dbname, $ tableflag ){
  3. $ Db_host = 'localhost ';
  4. $ Db_port = '000000 ';
  5. $ Db_user = 'username ';
  6. $ Db_pass = 'Password ';
  7. $ Connect = mysql_connect ($ db_host,
    $ Db_user, $ db_pass );
  8. Mysql_select_db ($ dbname );
  9. $ Result = mysql_query ("show table
    Status from $ dbname ", $ connect );
  10. $ Data = mysql_fetch_array ($ result );
  11. While ($ data = mysql_fetch_array ($ result )){
  12. $ Table = mysubstr ($ data [Name], "_");
  13. If ($ table = $ tableflag ){
  14. // Use for testing
  15. /* Echo $ data [Name];
  16. Echo "<br> ";
  17. Echo $ table;
  18. Echo "<br> ";*/
  19. Mysql_query ("drop table $ data [Name]");
  20. }
  21. }
  22. Return true;
  23. }
  24. /* Truncates all the character functions before a specific character
  25. * $ Str is the string to be truncated.
  26. * $ Flag specific characters such as "_"
  27. */
  28. Function mysubstr ($ str, $ flag ){
  29. $ Pos = strpos ($ str, $ flag );
  30. Return substr ($ str, 0, $ pos );
  31. }
  32. ?>

 
 
  1. <? Php // test
  2. $ Dbname = "database ";
  3. // Database Name
  4. $ Tableflag = "wp ";
  5. // The prefix of the table to be deleted. Only tables with this prefix can be deleted.
    Indicates the user
  6. Deldata ($ dbname, $ tableflag );
  7. // $ Test = mysubstr ("cdb_account_log ","_");
  8. // Echo $ test;
  9. ?>

Note that the above username and password have the following database and table prefix.

Okay. The above is some practical code for PHP file management. There are many other things that you may find yourself ~


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.