• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 559
  • Last Modified:

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.";  

0
bakum
Asked:
bakum
1 Solution
 
m1tk4Commented:
most likely you are getting linefeed (LF CR) characters appended to the file names when you create them. Try looking at the source HTML and if you see it like this:

attempting to get file MylongXMLFile.xml
<br>

i.e. <br> on another line, then that's it.
0
 
bakumAuthor Commented:
Brilliant.  Thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now