File rename function usage

Source: Internet
Author: User
Rename file function usage: img_1jpg, img_2jpg, and img_3jpg are saved in img_1, img_2, img_3, and largejpg respectively.

File rename function usage

Invalid.

  1. $ S = explode ("n", trim ('DIR/B E: 11-2caps'); // Obtain all file names in this path
  2. // Print_r ($ s );
  3. Foreach ($ s as $ rs)
  4. {
  5. $ Name = explode (".", $ rs );
  6. $ File_name = $ name [0]; // Obtain the name of the folder to be created
  7. Mkdir ("E:/11-2/caps/$ file_name"); // create a folder
  8. Rename ('E:/11-2/caps/'. $ rs, "E:/11-2/caps/$ file_name/large.jpg"); // move and rename the file
  9. }
  10. ?>

Definition and usage

Rename () function rename a file or directory.

If the call succeeds, the function returns true. if the call fails, false is returned.

Syntax

Rename (oldname, newname, context)

Parameter description

Oldname is required. Specifies the file or directory to be renamed.

Newname is required. Specifies the new name of the file or directory.

Context is required. Specifies the file handle environment. Context is a set of options for modifying stream behavior.

Tips and comments

Note: Before PHP 4.3.3, rename () cannot rename files across disk partitions in * nix-based systems.

Note: The Encapsulation Protocol used in oldname must match that used in newname.

Note: The support for context is added in PHP 5.0.0.

The PHP instance code is as follows:

  1. Rename ("images", "pictures ");
  2. ?>
  3.  
  4. $ File = "html/cache.txt ";
  5. $ Rename = "html/rename.txt ";
  6. If (rename ($ file, $ rename )){
  7. Echo "renamed ";
  8. } Else {
  9. Echo "renaming failed ";
  10. }
  11. Rename ("html/cache2", "html/cache3.txt ");
  12. Rename ("html", "cache ");
  13. Rename ("file", "html/files ");
  14. ?>

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.