Solved

PHP copy only 10 files from one directory to another

Posted on 2008-09-29
2
1,219 Views
Last Modified: 2013-12-12
Linux server, running apache and PHP.

Have a directory on the server called START.  START has lots (thousands) of PDF files copied into it daily.

Have PHP script that is working that copies ALL pdf file from the START directory to END directory, based on a variable and the filename of the pdf.  What I need to do is edit my script so that UP TO 10 files are copies at once, but not more than 10.  However, it is possible that there would be less than 10, based on the variable.

Let me explain.

The PDF files are named with 3 number a hyphen and one number,  like this
144-3.pdf
144-5.pdf
144-8.pdf
145-1.pdf
145-3.pdf

So when I run my script, the PHP code has a value of the variable $name which may be a value such as 144.  So the PHP script looks at all of the documents in the START folder, and copies any pdf file to the END directory that has 144 before the "-" in the file name.  

This code is working now.

The problem is that I might have 100 PDF files that start with 144, but I only want to copy 10 of them each time PHP script runs.  Right now the PHP script copies all files that have 144 as the first part of the file name.

Please help me edit my existing code to only allow up to 10 copies based on matching the value of $name.  

Note that if there are only 7 filenames that match the value of $name, that is fine - I want all 7 to be copied to the END folder.  So up to 10 files copied each time the script runs, but no more than 10 matching files.



$files = glob("/start/*.pdf");

		foreach($files as $filename) {

                

                $base = basename($filename, ".pdf");

		$base2 = basename($filename);

		$file = explode("-", $base);

		

                if($name == $file[0]) {

		$newLocation = '/end/' . $base2;

  		copy($filename, $newLocation);

Open in new window

0
Comment
Question by:fastfind1
2 Comments
 
LVL 5

Accepted Solution

by:
crush83 earned 300 total points
ID: 22602023
The only problem I see here is that you aren't moving the files - just copying them. That means that after the ten are copied, they will still be there and copied over and over. So you should probably either move them, or delete them after being copied.

I'd just add a counter to my foreach, and when it == 10 break;

Seems like you didn't paste all of your code though.
$files = glob("/start/*.pdf");
 

$counter = 0;

foreach ($files as $filename) {

	if ($counter == 10)

		break;

		

	$base = basename($filename, ".pdf");

	$base2 = basename($filename);

	$file = explode("-", $base);
 

	if($name == $file[0]) {

		$newLocation = '/end/' . $base2;

		copy($filename, $newLocation);

		$counter++;

	}

}

Open in new window

0
 
LVL 48

Assisted Solution

by:hernst42
hernst42 earned 200 total points
ID: 22602962
You can also use a while loo pinstead of foreach and using an improved glob the code can be reduced to:
$numfiles = 0;

$files = glob("/start/$name-*.pdf");

while ($filename = each($files) && $numfiles++ < 10) {

            $base2 = basename($filename);

            $newLocation = '/end/' . $base2;

            copy($filename, $newLocation);

}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP/Magento not logging 2 35
what is best version of php to use 6 46
.php tree directory? 5 56
Make custom query_posts look show the excerpt only 9 26
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now