Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Downloading all txt files in a folder

Status
Not open for further replies.

Naits

Programmer
Oct 10, 2001
90
NO
HELLO!

I am making a website called .: Game universE :. and i'm making a cheats script for user to upload there cheats to my server. And those file is in txt format, and are in folders named abc,def,ghi.... Is there an easy way for me to download all the txt files, because i want to store a backup on my harddrive in case something happens to the server.
 
A couple of options:
1. If you're running Windows you could just copy the files using Windows Explorer
(but maybe this is a blinding flash of the obvious, so ...)
2. Run an ftp server on your server, and use that, or ...
3. Use a php script which generates a batch file of lynx commands, use lynx to run the script, then execute the batch file, ie something like this (on Windows)...

snarf.php:
Code:
if ($dh=opendir("serverdir")) {
  while ($file=readdir($dh)) if (substr($file,-4)==".txt")) echo "lynx -s [URL unfurl="true"]http://yourserver/".$file."[/URL] > localpath\n";
  closedir($dh);
}

Then do:
Code:
lynx -s [URL unfurl="true"]http://yourserver/snarf.php[/URL] > runme.bat
runme.bat
-Rob
 
1. I'm running windows, but i cna't copy files from a server in a other contry with Explorer

2. The server does not have support for FTP

3. I didnt under stand the code

Okey let me ask again
I want to be able to download all files from a folder online to my harddrive in pure PHP is this posible?
 
1. What operating system is your server running? I don't see the problem, although admittedly doing things internationally will be a bit slow...
2. A pity. This would make things much easier.

If you are not doing 1 or 2, how are you placing your php scripts on the server?

In answer to your question, PHP (which runs on the server) doesn't have access to your client unless you set up some kind of connection - perhaps an ODBC or database link is what you need?

My code is a PHP script which steps through the entries of a directory on your server, and if the filename ends in .txt it outputs a line like:

Code:
lynx -s [URL unfurl="true"]http://yourserver/filename.txt[/URL] > localpath

(Lynx is a browser that can redirect output.)

The output that this script generates is a list of command lines like this which can then be run on your client machine to fetch the files you want.

-Rob
 
I couldnt log in so i am posting with the name Stian(my real name)

It isent mine server, and the server administartor has an browser uploading system. Here is some code i have been working on:

~~~~~~~~~~~~~~~~~~~~~~~~~
download.html:
<form action=&quot;action.php&quot; method=&quot;POST&quot;>
From: <input type=url name=remote>
To: <input type=url name=local>
<input type=submit>
</form>

action.php:
<?
if (empty($remote)) no();
elseif (empty($local)) no();
else copyFromRemote($remote, $local); //use it like this

function copyFromRemote($remote,$local){
$fp1 = fopen($remote,&quot;r&quot;);
$fp2 = fopen($local,&quot;w&quot;);
Print(&quot;Opened them...&quot;);
while(!feof($fp1)) {
$line = fread($fp1,1024);
fputs($fp2,$line,strlen($line));
}
Print(&quot;Got it&quot;);
include(&quot;index.html&quot;);
}
function no(){
print (&quot;You forgot to enter Remote or Local host&quot;);
include(&quot;index.html&quot;);
}
?>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This code works fine on 1 file at time that i give the full adress to, but can someone help me to change this script
to do this:
1. I just want to input the adress to a folder online
2. And just a folder on my harddrive

so the script will copy all the files from the folder i have given to the folder i have given on my harddrive.

Can anybody help me with this??
 
Stian

A loop which will step through all the
Code:
.txt
files in directory
Code:
$dir
looks like this:
Code:
if ($dh=opendir($dir)) {
  while ($f=readdir($dh)) {
    if (substr($f,-4)==&quot;.txt&quot;) {
      // put action to perform on each $f here
    }
  }
  closedir($dh);
}
However, since your files are spread across multiple directories (abc,def,ghi etc) then I suggest you use a recursive function like this:
Code:
$slash = &quot;/&quot;;

// The parent directory of abc,def,ghi etc
$rroot = &quot;remoteroot&quot;;

// Where to copy to
$lroot = &quot;localroot&quot;;

function fetchfromdir($dir) {

  global $rroot;
  global $lroot;
  global $slash;

  if ($dh=opendir($rroot.$dir)) {
    while ($f=readdir($dh)) if (($f!=&quot;.&quot;) AND ($f!=&quot;..&quot;)) {
      if (is_dir($f)) {
        if (!file_exists($lroot.$dir.$slash.$f)) mkdir($lroot.$dir.$slash.$f,0777);
        fetchfromdir($dir.$slash.$file);
      } else {
        if (substr($f,-4)==&quot;.txt&quot;) {
          copyFromRemote($rroot.$dir.$slash.$f,$lroot.$dir.$slash.$f);
      }
    }
    closedir($dh);
  }
}

I have tried this out, but it may at least provide some direction for you to take things further.

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top