Jump to content

Recommended Posts

"every file in the FTP"?

 

Can't fully understand what you're asking here.

 

I've got it connecting to the ftp server.

 

Basically I just want to list every file in the ftp connection such as:

 

./file.php

./folder/file.php

./emptyfolder

./folder/file.php

./folder/emptyfolder

ftp Lots of functions there!

 

You do realize that RTFM can help you immensely, especially the user comments. I didn't just post that link for you to ignore it.

 

http://www.php.net/manual/en/function.ftp-rawlist.php#86780

 

Is something along the lines of what you want, a recursive directory list. If that is not what you want look at modifying it or look through other user comments to see if someone setup something you want. You may also want to check at ftp_nlist and look at those comments as well.

The link premiso gave you takes you to a user comment that has a script posted by the author that does exactly what you have asked for.

(There are other recursive scripts posted in the user comments for that page).

 

-cb-

 

Its not what Im looking for...

 

I want a simple little ftp_nlist i'm working on which shows directorys and the files inside (Recursive)

 

I can do it fine with local, But FTP is the worst one out of them all.

 

Ftp_rawlist with the recursive turned to true still does not show what the folders contain in them, so generally it fails.

Here is my remote listing function:

 

function RemoteListDirectory($Directory, $Recursive){
    global $Connection;
    if($Connection){
        if(ftp_chdir($Connection, $Directory)){
            if($BaseArray = ftp_nlist($Connection, ".")){
                for($I=0;$I<count($BaseArray);$I++){
                    if($BaseArray[$I] != ""){ ## If filename is not empty
                        $FileSize = ftp_size($Connection, $BaseArray[$I]);
                        if($FileSize == -1){
                            echo "It thinks $BaseArray[$I] is a directory...";
                            print_r($FileSize);
                            echo "Changing directory to: $BaseArray[$I]"; 
                            $Array = array_merge($Array, RemoteListDirectory($BaseArray[$I], True));
                            @ftp_cdup($Connection);
                        }else{
                            if($Recursive){
                                $File = "$Directory/$BaseArray[$I]";
                                $Array[] = preg_replace("/\/\//si", "/",$BaseArray[$I]);
                            }else{
                                $Array[] = preg_replace("/\/\//si", "/", $BaseArray[$I]);
                            }
                        }
                    }else{
                        echo "It thinks $BaseArray[$I] is empty...";
                    }
                }
            }
        }
    }else{
        return False;
    }
    $Array = array_values($Array);
    return $Array;
}

 

Could someone help me with this, it comes out like this:

 

It thinks ./graphs is a directory...-1Changing directory to: ./graphsIt thinks ./soundcache is a directory...-1Changing directory to: ./soundcacheArray

(

    [0] => ./gm_construct.bsp

    [1] => ./gm_flatgrass.bsp

    [2] => ./gm_construct.ain

    [3] => ./gm_flatgrass.ain

    [4] => ./sb_forlorn_sb3_r2l.ain

    [5] => ./sb_Forlorn_sb3_R2L.bsp

    [6] => ./sb_gooniverse.bsp

    [7] => ./sb_lostinspace_rc5.bsp

    [8] => ./sb_new_worlds_2.bsp

    [9] => ./sb_Spacewar_SB3_V1.bsp

    [10] => ./sb_twinsuns_fixed.bsp

    [11] => ./_other.cache

    [12] => ./_other_rebuild.cache

    [13] => ./_sharedprecache.cache

)

 

some files are meant to be in folders.

Here try mine (just wrote it):

<?php

$ftp_server = "127.0.0.1";
$ftp_user_name = "test";
$ftp_user_pass = "pass";

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// Get current directory
$current_directory = ftp_pwd($conn_id);

// Recursive function
function ftp_nlistr($resource, $thispath, $thelist=array(), $first=TRUE){
if(!ftp_is_dir($resource,$thispath)){
	// for Files (anything that isnt a readable directory)
	if($first == TRUE){ return array("Path doesn't Exist (".$thispath.")"); }
	$thelist[] = $thispath;
	return $thelist;
}else{
	$contents = ftp_nlist($resource, $thispath);

	// For empty folders
	if(count($contents) == 0){
		$thelist[] = $thispath;
		return $thelist;
	}

	// Recursive Part
	foreach($contents As $file){
		$thelist = ftp_nlistr($resource, $file, $thelist,FALSE);
	}

	return $thelist;
}

}

// Simple function, checks wether its a folder by trying to change the current directory to it, if it fails, returns to the top-level directory.
function ftp_is_dir($conn, $dir) {
$cdir = ftp_pwd($conn);
if (@ftp_chdir($conn, $dir)) {
	ftp_chdir($conn, $cdir);
	return true;
} else {
	return false;
}
}

print_r(ftp_nlistr($conn_id, "."));
// Dont use a trailing slash in the directory your specifying to look through
?>

 

-cb-

Now I have it as:

 

It thinks ./graphs is a directory...-1Changing directory to: ./graphsIt thinks ./soundcache is a directory...-1Changing directory to: ./soundcacheArray
(
    [0] => ./gm_construct.bsp
    [1] => ./gm_flatgrass.bsp
    [2] => ./graphs/./gm_construct.ain
    [3] => ./graphs/./gm_flatgrass.ain
    [4] => ./graphs/./sb_forlorn_sb3_r2l.ain
    [5] => ./sb_Forlorn_sb3_R2L.bsp
    [6] => ./sb_gooniverse.bsp
    [7] => ./sb_lostinspace_rc5.bsp
    [8] => ./sb_new_worlds_2.bsp
    [9] => ./sb_Spacewar_SB3_V1.bsp
    [10] => ./sb_twinsuns_fixed.bsp
    [11] => ./soundcache/./_other.cache
    [12] => ./soundcache/./_other_rebuild.cache
    [13] => ./soundcache/./_sharedprecache.cache
)

Here try mine (just wrote it):

<?php

$ftp_server = "127.0.0.1";
$ftp_user_name = "test";
$ftp_user_pass = "pass";

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// Get current directory
$current_directory = ftp_pwd($conn_id);

// Recursive function
function ftp_nlistr($resource, $thispath, $thelist=array(), $first=TRUE){
if(!ftp_is_dir($resource,$thispath)){
	// for Files (anything that isnt a readable directory)
	if($first == TRUE){ return array("Path doesn't Exist (".$thispath.")"); }
	$thelist[] = $thispath;
	return $thelist;
}else{
	$contents = ftp_nlist($resource, $thispath);

	// For empty folders
	if(count($contents) == 0){
		$thelist[] = $thispath;
		return $thelist;
	}

	// Recursive Part
	foreach($contents As $file){
		$thelist = ftp_nlistr($resource, $file, $thelist,FALSE);
	}

	return $thelist;
}

}

// Simple function, checks wether its a folder by trying to change the current directory to it, if it fails, returns to the top-level directory.
function ftp_is_dir($conn, $dir) {
$cdir = ftp_pwd($conn);
if (@ftp_chdir($conn, $dir)) {
	ftp_chdir($conn, $cdir);
	return true;
} else {
	return false;
}
}

print_r(ftp_nlistr($conn_id, "."));
// Dont use a trailing slash in the directory your specifying to look through
?>

 

-cb-

 

That worked, I altered it to my liking and now it is:

 

function RemoteListDirectory($Directory, $Array = array(), $First = True){
    global $Connection;
    if($Connection){
        if($First){
            ftp_chdir($Connection, $Directory);
        }
        if(ftp_size($Connection, $Directory) != -1){
            if($First){
                echo "Folder does not exist."; 
                return False; 
            }
            $Array[] = $Directory;
            return $Array;
       }else{
            $Contents = ftp_nlist($Connection, $Directory);
            if(!count($Contents)){
                $Array[] = $Directory;
                return $Array;
            }
            foreach($Contents as $File){
                $Array = RemoteListDirectory($File, $Array, False);
            }
            $Array = array_values($Array);
            return $Array;
        }
    }else{
        return False;
    }
}

 

The problem is, I get the whole Directory of were the files are located:

 

Remote Directory: Array
(
    [0] => /NMD-Spacebuild/orangebox/garrysmod/maps/gm_construct.bsp
    [1] => /NMD-Spacebuild/orangebox/garrysmod/maps/gm_flatgrass.bsp
    [2] => /NMD-Spacebuild/orangebox/garrysmod/maps/graphs/gm_construct.ain
    [3] => /NMD-Spacebuild/orangebox/garrysmod/maps/graphs/gm_flatgrass.ain
    [4] => /NMD-Spacebuild/orangebox/garrysmod/maps/graphs/sb_forlorn_sb3_r2l.ain
    [5] => /NMD-Spacebuild/orangebox/garrysmod/maps/sb_Forlorn_sb3_R2L.bsp
    [6] => /NMD-Spacebuild/orangebox/garrysmod/maps/sb_gooniverse.bsp
    [7] => /NMD-Spacebuild/orangebox/garrysmod/maps/sb_lostinspace_rc5.bsp
    [8] => /NMD-Spacebuild/orangebox/garrysmod/maps/sb_new_worlds_2.bsp
    [9] => /NMD-Spacebuild/orangebox/garrysmod/maps/sb_Spacewar_SB3_V1.bsp
    [10] => /NMD-Spacebuild/orangebox/garrysmod/maps/sb_twinsuns_fixed.bsp
    [11] => /NMD-Spacebuild/orangebox/garrysmod/maps/soundcache/_other.cache
    [12] => /NMD-Spacebuild/orangebox/garrysmod/maps/soundcache/_other_rebuild.cache
    [13] => /NMD-Spacebuild/orangebox/garrysmod/maps/soundcache/_sharedprecache.cache
)

Alright I've got the bug sorted above this post, I'm having some difficulty fixing this part:

 

function Download($Array){
    global $Connection;
    if($Connection){
        for($I=0;$I<count($Array);$I++){
            if($ArrayExplode = (explode(".", $Array[$I]))){
                if($ArrayExplode[1] != "dua" & $ArrayExplode[1] != "txt"){
                    Zip($Array[$I]);
                }
            }
            if($Array[$I] != ""){
                if(ftp_size($Connection,$Array[$I]) != -1){
                    ftp_get($Connection,$Array[$I],$Array[$I],FTP_BINARY);
                }
            }
        }
        return True;
    }else{
        return False;
    }
}

 

It doesn't download folders (mkdir) them and then download the folders crap ;)

 

<br /> 
<b>Warning</b>:  ftp_get(./graphs/gm_construct.ain) [<a href='function.ftp-get'>function.ftp-get</a>]: failed to open stream: No such file or directory in <b>/home4/nmdgamin/public_html/gmod1/fastdl/functions.php</b> on line <b>161</b><br /> 
<br /> 
<b>Warning</b>:  ftp_get() [<a href='function.ftp-get'>function.ftp-get</a>]: Error opening ./graphs/gm_construct.ain in <b>/home4/nmdgamin/public_html/gmod1/fastdl/functions.php</b> on line <b>161</b><br /> 
<br /> 
<b>Warning</b>:  ftp_get(./graphs/gm_flatgrass.ain) [<a href='function.ftp-get'>function.ftp-get</a>]: failed to open stream: No such file or directory in <b>/home4/nmdgamin/public_html/gmod1/fastdl/functions.php</b> on line <b>161</b><br /> 
<br /> 
<b>Warning</b>:  ftp_get() [<a href='function.ftp-get'>function.ftp-get</a>]: Error opening ./graphs/gm_flatgrass.ain in <b>/home4/nmdgamin/public_html/gmod1/fastdl/functions.php</b> on line <b>161</b><br /> 
<br /> 
<b>Warning</b>:  ftp_get(./graphs/sb_forlorn_sb3_r2l.ain) [<a href='function.ftp-get'>function.ftp-get</a>]: failed to open stream: No such file or directory in <b>/home4/nmdgamin/public_html/gmod1/fastdl/functions.php</b> on line <b>161</b><br /> 
<br /> 
<b>Warning</b>:  ftp_get() [<a href='function.ftp-get'>function.ftp-get</a>]: Error opening ./graphs/sb_forlorn_sb3_r2l.ain in <b>/home4/nmdgamin/public_html/gmod1/fastdl/functions.php</b> on line <b>161</b><br /> 
<br /> 
<b>Warning</b>:  ftp_get(./soundcache/_other.cache) [<a href='function.ftp-get'>function.ftp-get</a>]: failed to open stream: No such file or directory in <b>/home4/nmdgamin/public_html/gmod1/fastdl/functions.php</b> on line <b>161</b><br /> 
<br /> 
<b>Warning</b>:  ftp_get() [<a href='function.ftp-get'>function.ftp-get</a>]: Error opening ./soundcache/_other.cache in <b>/home4/nmdgamin/public_html/gmod1/fastdl/functions.php</b> on line <b>161</b><br /> 
<br /> 

Could someone help me with this bit, I have no clue how to do this:

 

Make the downloader checker explode with forward slashes =>
Make it loop (until theres no more folders left) =>
If folder doesn't exist in local then mkdir it =>
Get the contents of them folders from the ftp server and download it =>
Continue on with the rest of the script

 

The Downloader is above.

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.