Link to home
Start Free TrialLog in
Avatar of bakum
bakum

asked on

curl/PHP file transfer problem.

Hi,

I'm trying to learn a bit of CURL for PHP.  I got a nice little script that will connect to any FTP directory you specify, look in that directory for files of type XML, and then retrieve those files locally.  It's a function I do here with a shell script, why not with a PHP script?  And it works!  Except one problem...the files that are retrieved have some screwed up thing with the filename.  If you look at them through XP they look fine, only trying to open them or delete them returns an error and some kind of text about permissions (not true, I'm positive) or "perhaps the file doesn't exist."  If I use Putty and check the files directly from the Linux shell, the files are there but the names have two ?? apended to them.  

e.g. file SHOULD be 200603230800PR_NEWS_USPR_____SFTH014.xml but in reality it is 200603230800PR_NEWS_USPR_____SFTH014.xml??, only XP can't see that.  WTF?!  Just wondering if you have any clues oh gurus of the web.  here's me script by the way:

$fileName = "dirList.txt";
//get a list of the remote directory contents
$ch = curl_init();
if (file_exists($fileName)) unlink ($fileName);
$filePointer = fopen($fileName,"w");

curl_setopt($ch, CURLOPT_URL, "$HOST");
curl_setopt($ch, CURLOPT_USERPWD, "$U:$P");
curl_setopt($ch,CURLOPT_FTPSSLAUTH , "CURLFTPAUTH_DEFAULT ");
curl_setopt($ch,CURLOPT_TRANSFERTEXT , "FALSE");
curl_setopt($ch,CURLOPT_FILE , $filePointer);

curl_exec($ch);
echo $error = curl_error($ch);
curl_close($ch);

//open the directory and find the XML files.  Put them in an array we will loop through later to retrieve each file individually.

$filePointer = fopen($fileName,"r");
$counter=0;

if ($filePointer)
{
   while (!feof($filePointer))
   {
       $buffer = fgets($filePointer);
         $wordHolder = explode (" ",$buffer);
         foreach ($wordHolder as $word)
         {
           if (stristr($word, ".xml")) $fileArray[$counter] = $word;
            }
         $counter++;
   }
   fclose ($filePointer);

//now loop through the array and retrieve the remote XML files.  
      foreach ($fileArray as $fn)
      {
        if (!$filePointer = fopen($fn,"x"))
        {
         echo "$fn already exists<br>";
        }
        else
        {
            echo "attempting to get file $fn<br>";
            $ch = curl_init();
            
            curl_setopt($ch, CURLOPT_URL, "$HOST".$fn);
            curl_setopt($ch, CURLOPT_USERPWD, "$U:$P");
            curl_setopt($ch,CURLOPT_FTPSSLAUTH , "CURLFTPAUTH_DEFAULT ");
            curl_setopt($ch,CURLOPT_TRANSFERTEXT , "FALSE");
            curl_setopt($ch,CURLOPT_FILE , $filePointer);
            
            curl_exec($ch);
            
            if ($error = curl_error($ch)) echo "$error<br>";
              else echo "transfer of $fn is complete.<br>";
            
            curl_close($ch);
        }
      }
        
}
else echo "My remote directory list $fileName does not exist.";  

ASKER CERTIFIED SOLUTION
Avatar of m1tk4
m1tk4
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bakum
bakum

ASKER

Brilliant.  Thanks!