[php] ftp get all files from a certain file type


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [php] ftp get all files from a certain file type
# 1  
Old 05-19-2009
[php] ftp get all files from a certain file type

Hi all,

I googled for this kind of function but didn't find anything. I have an FTP connection with a server, went to the dir. In this dir there are several TXT files. I would like to have function that downloads all this files, based on their .txt extension.

In bash, for example, simply: mget *.txt ...

Thanks in advance!
# 2  
Old 05-19-2009
[solved] to slow... !

Code:
$list = ftp_nlist($conn_id, '.');

function is_txt($file) {
    return preg_match('/.*\.txt/', $file) > 0;
}

$filtered = array_filter($list, is_txt);

print_r($filtered);

ftp_close($conn_id);

# 3  
Old 05-19-2009
Hi,

Another question, I need to have a certain functionality that jumps and combines data from 2 txt files. Earlier I had the same problem in bash:
https://www.unix.com/shell-programmin...e-another.html


Code:
line 1 = IP-address
line 2 = FTP-user
line 3 = FTP-pwd
line 4 = IP-address
line 5 = FTP-user
... etc ...
line 33 = FTP-user
line 34 = FTP-pwd
line 35 = IP-address
line 36 = FTP-user
line 37 = FTP-pwd
#-END

or the real file (little bit modified, for security reasons)
Code:
80.xx.xx.76              -> IP server
ftpmac28                 -> username
passO#lanL              -> password
212.xx.xx.52             -> IP server
ftpmac28                 -> username
passO#lanL              -> password
212.xx.xx.61
ftpmac28
passO#lanL
212.xx.xx.62
ftpmac28
passO#lanL
212.xx.xx.17
ftpmac28
passO#lanL
212.xx.xx.10
ftpmac28
passO#lanL
212.xx.xx.75
ftpmac28
passO#lanL

...etc...

In the same folder there are some other files named *IP*_folderlist. Where the first part is the IP address like xxx.xxx.xxx.xxx. These files contain the folders
The content of this files looks like this:
Code:
docshop.xx.be\files
extranetdocshop.xx.be\files
fleet.xx.be\doccshop\files

So what has to happen: a loop has to run through the ftpservers.txt and check if there is an IP_folderlist.txt in the currect dir, for the checked IP. If so it has to get them and put them in an array. After that, it should go to the 2nd line which is the $user, the 3rd line is the $passwd. If there is no IP_folderlist.txt, it has to skip to the next IP address line in the ftpserver.txt file, and check again, etc...

Thanks in advance!

Last edited by laurens; 05-19-2009 at 03:18 PM..
# 4  
Old 05-20-2009
No one? Smilie
# 5  
Old 05-20-2009
you are already had a solution in a shell script that franklin helped you with. so why don't you show some code and show us where you are stuck?
# 6  
Old 05-20-2009
This is the bash script I made with the kind help of Franklin, indeed.
Code:
set -xv
ftp -n -v immserv1.impactweb.imm.be << EOT
ascii
user ftpimpactstore VeneziA0211
prompt
cd BACKUP
#mget *.txt
ls
EOT
awk '{ sub("\r$", ""); print }' ftpservers.txt > unixfile.txt
awk '
NR%3==1{a[++i]=$0}
NR%3==2{u[i]=$0}
NR%3==0{p[i]=$0}
END{for(j=1;j<=i;j++){print a[j], u[j], p[j], a[j] "_folderlist.txt"}}' unixfile.txt |
while read ipaddress user passwd flist
do
while read dir
do
if [[ $ipaddress =~ ^192\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
echo "private IP - skipping"
else
ftp -n -v $ipaddress <<EOT
ascii
echo $user $passwd
user $user $passwd
prompt
echo $dir
mget $dir
EOT
fi
done < $flist
done
rm unixfile.txt

Although this could not really supply all the functionality we need, and no one here is really familiar with bash so in the end we choose to switch to PHP. I have to admit it programs better and there are lots of (build-in) functions available. Also the information on the internet is wider etc.

Code:
#!/usr/bin/php
<?php
// de server adressen, logins, paswoorden, paden, ... staan in een afzonderlijke config file
require "backupconfig.php";
require "backupfunctions.php";
// Het volgende levert ons een 
$files = directoryToArray($ipxbackup_path_dbfiles, true);
// Bereken datum van gisteren
$yesterday = date('d-m-Y', mktime(0, 0, 0, date("m") , date("d") - 1, date("Y")));
$arrFolders = array();
foreach ($files as $file) {
if (strpos($file, $yesterday . '.7z') !== false) {
//echo $file . "\n";
array_push($arrFolders, $file);
}
}
//print_r($arrFolders);
//---Voor FTP verbindingen: voorbeeld---//
// set up a connection or die
$conn_id = ftp_connect($impactweb_server) or die("Couldn't connect to $impactweb_server"); 
// try to login
if (@ftp_login($conn_id, $impactweb_user, $impactweb_pass)) {
echo "Connected as $impactweb_user@$impactweb_server\n";
} else {
echo "Couldn't connect as $impactweb_user\n";
}
// try to change the directory to backup dir
if (ftp_chdir($conn_id, $impactweb_path)) {
echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
} else { 
echo "Couldn't change directory\n";
}
//volgende lus overloopt de array van de 7z-bestanden en loadt ze up
/*
foreach($arrFolders as $folder) {
$stringArray = split('/',$folder);
$remote_file = $stringArray[4];
if (ftp_put($conn_id, $remote_file, $folder, FTP_BINARY)) {
echo "successfully uploaded $folder\n";
} else {
echo "There was a problem while uploading $folder\n";
}
}
*/
$conn_id = ftp_connect($ftpservers_server) or die("Couldn't connect to $ftpservers_server"); 
// try to login as impactstore
if (@ftp_login($conn_id, $ftpservers_user, $ftpservers_pass)) {
echo "Connected as $ftpservers_user@$ftpservers_server\n";
} else {
echo "Couldn't connect as $ftpservers_user\n";
}
if (ftp_chdir($conn_id, $ftpservers_path)) {
echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
} else { 
echo "Couldn't change directory\n";
}
$list = ftp_nlist($conn_id, '.');
function is_txt($file) {
return preg_match('/.*\.txt/', $file) > 0;
}
$filtered = array_filter($list, is_txt);
//print_r($filtered);
foreach($filtered as $txt) {
if (ftp_get($conn_id, $txt, $txt, FTP_BINARY)) {
echo "Successfully written to $txt\n";
} else {
echo "There was a problem\n";
}
}
ftp_close($conn_id);
exit;
 
// get contents of the current directory
$contents = ftp_nlist($conn_id, ".");
// output $contents
//var_dump($contents);
 
// close the connection
ftp_close($conn_id);
?>

This is what I have so far, this downloads files from another server, and it works. Now I have to download other files, those specified in the IP_folderlists, I wrote about.
I would like to have a funtion, in PHP, that does te same as:
Code:
awk '
NR%3==1{a[++i]=$0}
NR%3==2{u[i]=$0}
NR%3==0{p[i]=$0}
END{for(j=1;j<=i;j++){print a[j], u[j], p[j], a[j] "_folderlist.txt"}}' unixfile.txt |
while read ipaddress user passwd flist
do
while read dir
do
if [[ $ipaddress =~ ^192\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
echo "private IP - skipping"
else
ftp -n -v $ipaddress <<EOT
ascii
echo $user $passwd
user $user $passwd
prompt
echo $dir
mget $dir
EOT
fi
done < $flist
done

It checks every time the first line for an IP, and when it finds a folderlist, it jumps to that file and reads it out.

Thanks in advance !
# 7  
Old 05-20-2009
in PHP, you can use file_get_contents() , fread() , fopen() to read files. Since you are more familiar with PHP, you can go to the manual to see examples of how to iterate a file.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Move Multiple Files adding date timestamp before file type

There are files in a directory and I have to move multiple files adding datetimestamp before the file type. /Data/ abc.csv def.csv ghi.csv I have to move this files to archive directory adding datatimestamp before .csv /archive/ abc_YYYYMMDDHHMMSS.csv def_YYYYMMDDHHMMSS.csv... (7 Replies)
Discussion started by: eskay
7 Replies

2. Shell Programming and Scripting

How to ftp multiple files by taking the file name from a input file.

Hi, I'm working on a script which has to copy multiple files from one server to another server. The list of files that are to be copied is present in a file say input.txt. vi input.txt abc.c welcome.c new.c welcome1.c for ftp'ing a single file say 'new.c' the following code... (2 Replies)
Discussion started by: i.srini89
2 Replies

3. Shell Programming and Scripting

Deleting all files recursively from directories while ignoring one file type

Hi, Seems like I need help again with a problem: I want to delete all files from my lets say "Music" Directory inkluding all of the subfolders except for .mp3 and .MP3 files. I tried it with globalignoring mp3 files, finding and deleting all other files, which resulted in all files... (3 Replies)
Discussion started by: pasc
3 Replies

4. Shell Programming and Scripting

write to multiple files depending on file type (solved)

I want a script that will add comments to a file before check-in files. comments depend on type of files. i have a script to list files in the directory that will be checked-in There are xml, js, html, vm files. vm will use comments like c/c++ ( // or /*..*/) can you help me add a comment line... (0 Replies)
Discussion started by: pradeepmacha
0 Replies

5. Programming

type command in PHP

Hello, I am having trouble getting the type command to work in PHP whereas it works fine on the command line with only certain functions... example from PHP script: echo system('type cp'); returns cp is /bin/cp whereas echo system('type mogrify'); returns nothing... whereas from the unix... (1 Reply)
Discussion started by: dave_c00
1 Replies

6. Windows & DOS: Issues & Discussions

Batch file to Get New Files Only through FTP

Hi there I want to create a batch file which Get (mget) the newly added Files only from specific directory on UNIX server to specific folder on windows server using FTP, and this batch file will be scheduled on the windows server to run daily to load the new files (load the newly added files... (1 Reply)
Discussion started by: alsharq
1 Replies

7. Windows & DOS: Issues & Discussions

Batch file to Get New Files Only through FTP

Hi there I want to create a batch file which Get (mget) the newly added Files only, from specific directory on UNIX server to local folder on windows server through FTP, and this batch file will be scheduled to run daily to load the new files. help me plz :confused: (1 Reply)
Discussion started by: alsharq
1 Replies

8. Linux

Output to 1 file from two files in FTP

I am currently in FTP. I want to transfer two files and need the out put in One file. Can you help me.... For eg: if i am doing mget from 1 server to other. (1 Reply)
Discussion started by: manish.s
1 Replies
Login or Register to Ask a Question