利用php实现文件迁徙重命名
利用php实现文件迁移重命名
首先表明,这是一个悲伤的故事。
暑假来临,学校安排我们到某软件外包公司实习,想想不用面试也是蛮方便的,可以借此机会向大牛学习学习,虽然没有工资(据说学校还交了600块的保险),但想想还是蛮期待的,但真正到公司就懵逼了~技术人员都不在好不好!职位都安排好了,什么人力资源,经营管理,还有财务管理之类,做培训的,但没有一个写代码的啊!
我被分到了经营管理部门,每天接触的是各种合同审批,合同扫描,合同盖章,合同... 总之围着合同团团转,打杂远远多于学习。想想如果没有我们那些公司的人也要处理如此重复无聊的工作,真是让我对工作有点抗拒了。
不闲扯了,我遇到了这么一个问题。公司合同文本在存档前都要扫描到计算机上然后上传到公司系统。 所有扫描完的合同都存在一个文件夹里,当初扫描时为了方便统一命名为
"CD+年份+月份+合同编号.pdf";例如15年7月的第10份合同命名为“CD1507010.pdf”。每个月合同大概有300份左右。为了上传到系统,有两种方法,一种是一份一份上传到系统,一种是在本地重命名好打包发送到系统。 手工操作的话,无论哪一种都耗时在一个小时以上。如此重复无聊的工作,简直让人奔溃。对于第一种方法,由于不了解系统,我没有做更多深究,我着重考虑第二种方法。在本地重命名规则是将原来的pdf文件放置在"文件名+C"的文件夹下,并且重命名为HTWB.pdf。
本着重复工作尽量交给计算机去做的原则,我考虑用自己所学的php语言来解决问题。(不知道其他语言是否更加高效,但这明显要比纯手工更高效)。
公司电脑配置为win7系统,虽说是一家软件公司,但是非研发部门的计算机水平和一般公司的无异。考虑到操作人员的计算机相关知识,也考虑到实现的便捷性,我没有安装Apache服务器甚至也没有启动Windows自带的IIS,直接利用命令行进行操作。
名为htzy.php的脚本如下:
<span style="color: #000000;">php </span><span style="color: #800080;">$path</span>=<span style="color: #008080;">dirname</span>(<span style="color: #ff00ff;">__FILE__</span><span style="color: #000000;">);</span><span style="color: #800080;">$handle</span>=<span style="color: #008080;">opendir</span>(<span style="color: #800080;">$path</span><span style="color: #000000;">);</span><span style="color: #0000ff;">while</span> (!(<span style="color: #800080;">$item</span>=<span style="color: #008080;">readdir</span>(<span style="color: #800080;">$handle</span>))==<span style="color: #0000ff;">false</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$item</span>!='.'&&<span style="color: #800080;">$item</span>!='..'&&<span style="color: #800080;">$item</span>!='htzy.php'<span style="color: #000000;">) { </span><span style="color: #800080;">$arr</span>=<span style="color: #008080;">explode</span>('.', <span style="color: #800080;">$item</span><span style="color: #000000;">); </span><span style="color: #800080;">$dirname</span>=<span style="color: #800080;">$arr</span>[0].'C'<span style="color: #000000;">; </span><span style="color: #008080;">mkdir</span>(<span style="color: #800080;">$path</span>.'/'.<span style="color: #800080;">$dirname</span><span style="color: #000000;">); </span><span style="color: #008080;">rename</span>(<span style="color: #800080;">$path</span>.'/'.<span style="color: #800080;">$item</span>,<span style="color: #800080;">$path</span>.'/'.<span style="color: #800080;">$dirname</span>.'/HTWB.pdf'<span style="color: #000000;"> ); }}</span><span style="color: #008080;">unlink</span>('./htzy.php'<span style="color: #000000;">); </span>?>
将此文件放置在要处理的文件夹下,在文件夹打开命令行窗口,输入
php -f htzy.php
稍等几秒就好。
简单利用 mkdir() rename()函数完成文件转移,最后利用unlink删除脚本,所以,要重复利用此脚本的话需要保存好它~
至此,文件迁移重命名完成。
Knowledge is power~

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
