Rename function in PHP to move a file or folder

Source: Internet
Author: User
Tags parent directory


Accidentally do file moving time summed up a knowledge point: At the beginning, thought that with the upload file can use move_uploaded_file this function to move a file, after trying to find no, online search A, found that you can use rename this function to achieve file or folder movement, very strange, the manual is written in duplicate files or folders, did not expect to be used to move files

About function return values:

BOOL Rename (string $oldname, String $newname [, Resource $context])
Attempt to rename the Oldname to NewName and return true if the operation succeeds, and the failure returns false.


1. For files, rename can be moved between different disk characters.
2. For empty folders, rename can also move between different disk characters. However, the parent directory of the destination folder must exist.
3. For Non-empty folders, you can only move under the same letter.

Usage is as follows:

Rename ("/tmp/www.txt", "/home/my_file.txt");

The first parameter puts the original address, the second parameter puts the target address;

There is a way to use Copy+unlink online, but I heard that the speed of moving is not how, so also do not say, with rename on the line, simple, convenient, fast

may be accustomed to using Copy,unlink to achieve file movement, for large files, may take a long time, but the test found that performance is not.

Supplementary: Problems in Chinese documents and folders

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.