i have some code that opens an ftp server then inserts data into an database based on what files are in there
my problem is that it appears that $fname2
is just calling the first file as the variable and keeps using that instead of looping through all the filenames
$contents = ftp_nlist($conn_id, $dir);
$unalteredcontents = ftp_nlist($conn_id, $dir);
foreach( $contents as $fname )
{
if (($fname != '.') && ($fname != '..') )
{
foreach ($unalteredcontents as $fname2)
{
if (($fname2 != '.') && ($fname2 !='..'))
{
$url = "http://website/userid/".$uid."/".$fname2."";
}
}
the $fname2
is returning the first file as the variable for the whole loop any insight?
Works for me. Here's my test code:
<?php
// set up basic connection
$ftp_server = "ftp.yoursite.com";
$ftp_user_name = "username";
$ftp_user_pass = "password";
$dir = "./public_html/cgi-bin"; // just a sample directory
$uid = 1;
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$contents = ftp_nlist($conn_id, $dir);
$unalteredcontents = ftp_nlist($conn_id, $dir);
foreach( $contents as $fname )
{
if (($fname != '.') && ($fname != '..') )
{
foreach ($unalteredcontents as $fname2)
{
if (($fname2 != '.') && ($fname2 !='..'))
{
echo "http://website/userid/".$uid."/".$fname2."
";
}
}
}
}
I closed out your brackets and explicitly created the ftp-related variables.
It's hard to know what's going on in your particular instance. I assume your sure there is indeed more than one file in the directory in question?