PHP Move pdf's from one directory to another

Posted on 2010-04-09
Medium Priority
Last Modified: 2013-12-12

New to PHP and looking for a script to move all pdf's from one directory or folder to another.  I found a script in the archives here I thought could be modified.  Any help would be greatly appreciated.

$sourceDir = "/";
$targetDir = "/";
if ( $dh = opendir($sourceDir) )
    while(false !== ($fileName = readdir($dh)))
        if (!in_array($fileName, array('.','..')))
            rename($sourceDir.$fileName, $targetDir.$fileName);

Open in new window

Question by:yyming10
1 Comment
LVL 29

Accepted Solution

fibo earned 2000 total points
ID: 30285589
This script looks fine if you place the right, DIFFERENT values at lines 3 and 4.

You might however stumble on lots of different problems, depending on your PHP versions and access rights.

To make it short:
- if both dirs are subdirs of the dir where the script runs, you will presumably avoid lots of troubles
- you might face access rights problem on both directories:
-- check manually that target dir exists and if not, either create it manually if your run is a one shot, or place a dir-creation function in your script and check that it works
-- check that you have the right to delete from the source dir and the right to write in the target dir

Finally, have a look at page http://www.php.net/manual/en/function.rename.php where it seems that there might be in some case a problem if you are moving files from one partition to the other... but that should not really be a problem if both subdirs are "under" the current dir.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question