Home  >  Article  >  Backend Development  >  Use PHP to implement file migration and renaming, _PHP tutorial

Use PHP to implement file migration and renaming, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:46:16795browse

Use PHP to migrate and rename files,

First of all, this is a sad story. ​

The summer vacation is coming, and the school arranged for us to intern at a software outsourcing company. It is quite convenient to think about it without having to interview. We can take this opportunity to learn from the experts. Although there is no salary (it is said that the school also paid 600 yuan in insurance), But thinking about it, I was quite looking forward to it, but when I actually arrived at the company, I was confused ~ none of the technical staff were there! The positions are all arranged, including human resources, operation management, financial management, etc. There are people doing training, but no one can write code!

I was assigned to the operation and management department, and I was exposed to various contract approvals, contract scanning, contract sealing, contracts... In short, I was busy with contracts, doing chores more than studying. Thinking about how people in our company would have to deal with such repetitive and boring work makes me a little resistant to work.

No more chatting, I encountered such a problem. The text of company contracts must be scanned onto a computer and then uploaded to the company system before being archived. All scanned contracts are stored in a folder, which was named

for convenience when scanning.

"CD Year Month Contract Number.pdf"; For example, the 10th contract in July 2015 is named "CD1507010.pdf". There are about 300 contracts per month. In order to upload to the system, there are two methods, one is to upload them to the system one by one, and the other is to rename them locally and package them and send them to the system. Either way would take more than an hour if done manually. Repeating such boring work is simply frustrating. Regarding the first method, I did not do more in-depth research because I did not understand the system. I focused on the second method. The local renaming rule is to place the original pdf file in the folder of "File Name C" and rename it to HTWB.pdf .

In line with the principle of leaving repetitive tasks to computers as much as possible, I considered using the PHP language I learned to solve the problem. (I don’t know if other languages ​​are more efficient, but this is obviously more efficient than pure manual work).

The company’s computers are configured with win7 system. Although it is a software company, the computer level of the non-R&D department is the same as that of ordinary companies. Taking into account the operator's computer-related knowledge and the convenience of implementation, I did not install the Apache server or even start the IIS that comes with Windows. I directly used the command line to operate.

The script named htzy.php is as follows:

<?<span>php 

</span><span>$path</span>=<span>dirname</span>(<span>__FILE__</span><span>);
</span><span>$handle</span>=<span>opendir</span>(<span>$path</span><span>);

</span><span>while</span> (!(<span>$item</span>=<span>readdir</span>(<span>$handle</span>))==<span>false</span><span>) {
    </span><span>if</span> (<span>$item</span>!='.'&&<span>$item</span>!='..'&&<span>$item</span>!='htzy.php'<span>) {

        </span><span>$arr</span>=<span>explode</span>('.', <span>$item</span><span>);
        </span><span>$dirname</span>=<span>$arr</span>[0].'C'<span>;
        </span><span>mkdir</span>(<span>$path</span>.'/'.<span>$dirname</span><span>);
        </span><span>rename</span>(<span>$path</span>.'/'.<span>$item</span>,<span>$path</span>.'/'.<span>$dirname</span>.'/HTWB.pdf'<span> );
    }
}

</span><span>unlink</span>('./htzy.php'<span>);
 </span>?>

Place this file in the folder to be processed, open a command line window in the folder, and enter

      php -f htzy.php

Just wait a few seconds. ​

Simply use the mkdir() rename() function to complete the file transfer, and finally use unlink to delete the script. Therefore, if you want to reuse this script, you need to save it~

At this point, the file migration and renaming is completed.

Knowledge is power~

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1034807.htmlTechArticleUsing php to implement file migration and renaming, first of all, this is a sad story. Summer vacation is coming, and the school arranged for us to intern at a software outsourcing company. It’s quite convenient to think that we don’t have to interview...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn