Welcome Guest, Not a member yet? Register   Sign In
ftp->list_files() Question
#1

[eluser]kyymehra[/eluser]
I had what I thought to be a super smooth transition from localhost to putting my website live. Just imported the database, uploaded the files via FTP, then changed the database.php and ftp.php config files to connect to the live database and server.

Instead what I had was a mess of code that needed changing because for some reason the value returned by ftp->list_files() varied from what was returned (and is still returned on localhost).

Localhost:
$this->ftp->list_files('/foo/bar/') returns '/foo/bar/file1.ext', '/foo/bar/file2.ext', etc

Live:
$this->ftp->list_file('/foo/bar/') returns 'file1.ext', 'file2.ext', etc

The array items on the live site make more sense, it returns the filename without the path. I'm wondering why they differ. What kind of setting would produce these different results?
#2

[eluser]TheFuzzy0ne[/eluser]
I created a script and just copied and pasted the following from php.net:

Code:
$ftp_server = '192.168.1.70';
$ftp_user_name = 'user';
$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 contents of the current directory
$contents = ftp_nlist($conn_id, "./Dropbox");

// output $contents
var_dump($contents);

CodeIgniter simply returns the raw return value of ftp_nlist(). I ran the script on a Windows machine (PHP 5.4.14) and on a Linux machine (PHP 5.3.3-7+squeeze15 with Suhosin-Patch), and I could not reproduce the problem. I suspect that this could be an issue with the server's you are connecting to, and how they are sending back the file paths. Are you connecting to the same FTP server each time, or do you have a different FTP server set up for testing?

What happens if you CD into the directory first?
Code:
$this->ftp->changedir('/foo/bar);
$files = $this->ftp->list_files(''); // Note the empty string being passed.
#3

[eluser]kyymehra[/eluser]
The first server I try it on for testing is localhost. It returns the files and includes the path name in each array item for some reason... The other is just a free hosting account from 000webhost.com.

I will try changing directories before requesting the list, but mostly I'm just puzzled by the inconsistency of the code's behavior.
#4

[eluser]TheFuzzy0ne[/eluser]
It could be a PHP bug. What version of PHP are you running in order to get the full paths of the files?
#5

[eluser]kyymehra[/eluser]
PHP 5.4.3 on localhost. Issue is consistent.

Code:
// code excerpts related to the problem
$this->ftp->connect();

$this->input_path = '/input/1/';
$this->input_files = $this->ftp->list_files($this->input_path);

arsort($this->input_files);

var_dump($this->input_files)

The var_dump prints:
Quote:array (size=2)
1 => string '/input/1/2013_05_01_u.xlsx' (length=26)
0 => string '/input/1/2013_05_01_d.xlsx' (length=26)

--EDIT:
I tried to incorporate $this->ftp->changedir(). It almost works...
Quote:array (size=2)
1 => string './2013_05_01_u.xlsx' (length=19)
0 => string './2013_05_01_d.xlsx' (length=19)

But I don't want the './' in there...

--EDIT2:

To be honest I could loop through and efficiently remove the path after checking if it is present with strpos(). But I really want it to just do this the right way like it should be xD. I don't understand why it won't just list the plain files i.e. file1.txt, file2.txt, etc.
#6

[eluser]TheFuzzy0ne[/eluser]
I think it would be wise to see if you can try another FTP server. I'm not sure if this is an issue with PHP, or an issue with the server you're connecting to.
#7

[eluser]TheFuzzy0ne[/eluser]
Also, if you want just a list of files, I'd suggest you extend the FTP library and do all of your string manipulation there. Something like this might work (untested, because I can't replicate the problem):

./application/libraries/MY_Ftp.php
Code:
class MY_FTP extends CI_FTP {

    function list_files($path= '.')
    {
        if ( ! $files = parent::list_files($path))
        {
            return FALSE;
        }
        
        // Only loop through the files if first filename contains a forward slash.
        if (strpos($files[0], '/') !== FALSE)
        {
            foreach ($files as &$file)
            {
                $file = pathinfo($file, PATHINFO_BASENAME);
            }
        }
        
        return $files;
    }
}

Hope this helps.




Theme © iAndrew 2016 - Forum software by © MyBB