PHP Move pdf's from one directory to another


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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bernard S.CTOCommented:
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 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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.