Click to See Complete Forum and Search --> : list files on remote server


kanuski
11-02-2003, 06:20 PM
I have a mysql database listing several thousand images on one server. The corresponding images are located on another server. Over the years several records have been deleted from the database. I would like to know which images do not have a corresponding record in the database. I thought I could print a list of these files if I could get a list of all images in the remote server. Is this possible?

pyro
11-02-2003, 06:29 PM
No, you can't get a file listing on a remote server. What you'll want to do (if possible) is upload the PHP script to the remote server, and have it print out the list. You can then use that to compair with the database.

kanuski
11-02-2003, 06:44 PM
The owners of the server will not install php on the remote server. I have been trying to use ftp but I cannot get the connection to open:
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login("$conn_id", "$ftp_user_name", "$ftp_user_pass");

// check connection
if ((!$conn_id) || (!$login_result)) {
die("FTP connection has failed !");
}

// get contents of the images directory
$contents = ftp_nlist($conn_id, "images/");

// print each entry
foreach ($contents as $entry) {
echo $entry, "<br />\n";
}


I get two errors:
1. php_hostconnect: connect failed ...
2. ftp_login() expects parameter 1 to be resource, boolean given in var/www/...

pyro
11-02-2003, 07:01 PM
Try connecting like this, instead:

<?php

$ftp_server = "ftp.yourdomain.com";
$ftp_user = "username";
$ftp_pass = "password";

// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");

// try to login
if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
echo "Connected as $ftp_user@$ftp_server\n";
} else {
echo "Couldn't connect as $ftp_user\n";
}

?>

pyro
11-02-2003, 07:30 PM
While your at it, use this to get the file list... :D

<?php

$ftp_server = "ftp.yourdomain.com";
$ftp_user = "username";
$ftp_pass = "password";
$dir = "public_html/images"; #path to directory you want to read.

// set up a connection or die
$conn_id = @ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
$login_result = @ftp_login($conn_id, $ftp_user, $ftp_pass) or die("Couldn't connect as $ftp_user");

// try to login
$files = ftp_rawlist($conn_id, $dir);
foreach ($files as $file) {
$str = split(" ", $file);
$filelist[] = $str[count($str)-1];
}

sort ($filelist);
foreach ($filelist as $file) {
echo $file."<br>";
}

?>

kanuski
11-03-2003, 10:30 AM
Thanks Pyro,
I appreciate the help but I still can't make this work.
I can even read the current directory (ftp_pwd)but I cannot change directories (chdir) read file names (ftp_nlist) or get file info (ftp_rawlist).

$ftp_server = "wblrd.sk.ca";
$ftp_user = "username";
$ftp_pass = "password";
// I am not a genius but I did use the right username and password.
$dir = "images/"; #path to directory you want to read.

// set up a connection or die
$conn_id = @ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
$login_result = @ftp_login($conn_id, $ftp_user, $ftp_pass) or die("Couldn't connect as $ftp_user");
// my editing

echo "Current directory : ", ftp_pwd($conn_id), "<br>";

if (@ftp_chdir($conn_id, "images")) {
echo "Current directory is now : ", ftp_pwd($conn_id), "<br>";
} else {
echo "Couldn't change directory\n";
}

// try to login ( I only use one of the next two lines )
//$files = ftp_rawlist($conn_id, $dir);
$files = ftp_nlist($conn_id, $dir);
foreach ($files as $file) { // here I am getting an invalid argument error
$str = split(" ", $file);
$filelist[] = $str[count($str)-1];
}

sort ($filelist);
foreach ($filelist as $file) {
echo $file."<br>";
}


If there is not something obviously wrong I am going to try to get the file list another way. thanks again for teh help.

pyro
11-03-2003, 10:59 AM
Is your path correct? It probably won't look the same as it would if you use your browser to reach it. For instance, on most of my sites, the pages reside in a public_html directory. If I would just type images for the path, I would not get my files, thus the public_html/images. You might want to try setting $dir to a / to get the main directory listing. Other than that, could you perhaps tell us what errors you are getting?

kanuski
11-03-2003, 11:24 AM
I have tried every combination of directory options I could think of. I will add the error messages in the following code.

$ftp_server = "wblrd.sk.ca";
$ftp_user = "username";
$ftp_pass = "password";

$dir = "public/images/"; #path to directory you want to read.

// set up a connection or die
$conn_id = @ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
$login_result = @ftp_login($conn_id, $ftp_user, $ftp_pass) or die("Couldn't connect as $ftp_user");

echo "Current directory : ", ftp_pwd($conn_id), "<br>";
// This prints a "/"

if (@ftp_chdir($conn_id, "images")) {
echo "Current directory is now : ", ftp_pwd($conn_id), "<br>";
} else {
echo "Couldn't change directory\n"; // This prints "Couldn't change directory"
}

// try to login ( I only use one of the next two lines )
//$files = ftp_rawlist($conn_id, $dir);
$files = ftp_nlist($conn_id, $dir);
foreach ($files as $file) {
// ERROR: Warning: Invalid argument supplied for foreach() in /var/www/...


$str = split(" ", $file);
$filelist[] = $str[count($str)-1];
}

sort ($filelist);
// ERROR: Warning: sort() expects parameter 1 to be array, null given in /var/www...
foreach ($filelist as $file) {
echo $file."<br>";
}
// ERROR: Warning: Invalid argument supplied for foreach() in /var/www/...

pyro
11-03-2003, 11:50 AM
What does this give you?

<?php

$ftp_server = "ftp.yourdomain.com";
$ftp_user = "username";
$ftp_pass = "password";
$dir = "/"; #path to directory you want to read.

// set up a connection or die
$conn_id = @ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
$login_result = @ftp_login($conn_id, $ftp_user, $ftp_pass) or die("Couldn't connect as $ftp_user");

// try to login
$files = ftp_rawlist($conn_id, $dir);
foreach ($files as $file) {
$str = split(" ", $file);
$filelist[] = $str[count($str)-1];
}

sort ($filelist);
foreach ($filelist as $file) {
echo $file."<br>";
}

?>

kanuski
11-03-2003, 12:55 PM
It gives me this:
Warning: Invalid argument supplied for foreach() in /var/www/...

Warning: sort() expects parameter 1 to be array, null given in /var/www/...

Warning: Invalid argument supplied for foreach() in /var/www/...


Thanks for your persistence. I am about ready to give up!

pyro
11-03-2003, 01:02 PM
Not really sure... Makes it tough with the server in question not supporting PHP. All the code I posted works fine when used from my local server logging into my remote server.

kanuski
11-03-2003, 01:05 PM
Thanks for your help,
I am in touch with the server administrator about other possible paths I should be using. If this does not work He will try to create a list for me.
Thanks again.

pyro
11-03-2003, 01:15 PM
Sure thing. Hope something works out for you.