Jump to content

Recommended Posts

i am passing a number through a form and the input will be a range like "234204". I need to try and recursively locate a file named this in a directory called repository:

/repository

.../200

......000347872_f.jpg

......000347872_m.jpg

......000347872_v.jpg

......000112949_f.jpg

.../201

.../202

.../203

.../204

......000234204_f.jpg

......000234204_v_f1.jpg

.../205

.../206

 

and then copy the found files to a directory called ready-to-email:

/ready-to-email

...000234204_f.jpg

...000234204_v_f1.jpg

 

I know the file names will have different endings, but the center of the files will have the "client id" number. (yes the subfolders are named after the last 3 digits of the client id) I need to copy all files that match the 9 digit client id number.

 

I saw file_exists but am not sure if that will work because i need to move all files that match and i wouldn't know what comes after the client id. i mean . . . i don't have a way of specifying what the full name is and that seems to be the only way to get file_exists to work.

 

I am not too savvy in this and would love some help. thanks in advance to all the readers and submitters of this forum!

Link to comment
https://forums.phpfreaks.com/topic/126768-find-and-copy-files/
Share on other sites

Try something like this to get you started (I didn't test it):

 

<?php
function search($path, $string)
{
$dir = new DirectoryIterator($path);

foreach ($dir as $item) {
	if ($item->isDir() && !$item->isDot()) {
		search($item->getRealPath(), $string);
	}
	else if ($item->isFile()) {
		if (strpos($item->getFilename(), $string) !== false) {
			echo $item->getRealPath() . PHP_EOL;
		}
	}
}
}

search('/repository', '234204');
?>

 

It will search recursively through a directory, $path, and print the path to all the files that contain $string in their filenames.

Link to comment
https://forums.phpfreaks.com/topic/126768-find-and-copy-files/#findComment-655659
Share on other sites

^ If that doesn't work you might want to try:

 

#!/usr/bin/perl
# FTP file search by LudakoT, all rights given away ludakot[at]gmail.com
# Searches a filename until it's found or when all directories on the server are viewed
# Tested on my local ftp server on winxp and debian
use strict;
use Net::FTP;
use Getopt::Std;
my %args;
my ($host,$port,$user,$password,$filename);
getopts (":h:P:u:H:f:p:d", \%args);
if(!defined $args{h} || !defined $args{f}) {
usage();
}
if(!defined $args{P}) {
$port = 21;
}
else {
$port = $args{P};
}
if(!defined $args{u}) {
$user = 'anonymous';
}
else {
$user = $args{u};
}
if(!defined $args{p}) {
$password = 'foo@bar';
}
else {
$password = $args{p};
}
if(defined $args{H}) {
usage();
}
$host = $args{h};
$filename = $args{f};
my $ftp = Net::FTP->new($host, Port => $port, Debug => 0) or die $@;
$ftp->login($user,$password) or die $ftp->message;
my @start = $ftp->ls; # See what we have in the root of the ftp server
foreach (@start) {
if($_ eq $filename) {
# Perhaps the file is already here
print "File found in: ", $ftp->pwd(),"\n";
if(defined $args{d}) {
$ftp->get($filename) or die $ftp->message;
}
$ftp->quit;
exit; # Remove this if you want to search for other files with the same name
}
search($_);
}
sub search {
my $dir = shift;
my $startdir = $ftp->pwd(); # Remember where you came from
# In this environment, you can tell the difference between directories and files
$ftp->cwd($dir) or next;
my @names = $ftp->ls; # List everything we have in this directory
foreach my $name (@names) {
if($name eq $filename) { # Is our file here?
print "File found in: ", $ftp->pwd(),"\n";
if(defined $args{d}) {
$ftp->get($filename) or die $ftp->message;
}
$ftp->quit;
exit; # Remove this if you want to search for other files with the same name
}
search($name); # Search the next possible directory
}
$ftp->cwd($startdir); # Back where you came from
}
sub usage {
print qq~
Options:
-h hostname *
-f filename *
-u username [default anonymous]
-p password [default foo[at]bar]
-P portnumber [default 21]
-d download file when found
-H display help
Options marked with '*' are mandatory
Example: perl $0 -h 192.168.0.1 -f foo.zip
~;
exit;
}
# TODO: Search with wildcards and question marks
#EOF

 

Found it at https://www.elitehackers.info/forums/archive/index.php/t-8159.html, credit goes to "LudakoT".

Link to comment
https://forums.phpfreaks.com/topic/126768-find-and-copy-files/#findComment-655661
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.