Jump to content

Problems with FTP wrapper in PHP

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
gelembjuk

gelembjuk

    Newbie

  • Members
  • Pip
  • 5 posts
Hello,
I found that FTP wrapper in PHP works wrong.

Simple code:

$link='ftp://mylogin:mypassword@myhost.com:21/domains/folder1/folder2/123changes.doc';


$h=fopen($link,'r');

if(!$h){

 echo 'bad';

}else

 echo 'good';


This code always shows 'good'. Even file doesn't exists on FTP server. There is no file 123changes.doc in folder 'folder2'. But fopen returns correct handle.

What is wrong in my code?

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
I'd recommend using the ftp_ set of functions instead. PHP: FTP Functions - Manual

#3
gelembjuk

gelembjuk

    Newbie

  • Members
  • Pip
  • 5 posts

John said:

I'd recommend using the ftp_ set of functions instead. PHP: FTP Functions - Manual

this set of function is good but not for me.
I need to have opened handle and read file with portions.
My code looks like:

$handle = fopen("ftp://example.com/folder/file.doc", "r");


while (!feof($handle)) {

  $data= fread($handle, 8192);

  //do something with data

}

fclose($handle);


There is no ftp_ function that can return handle.
Any idea? Is this bug in my php installation (that handle is always opened even if file doesn't exists)?

#4
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
For me, the code in your original post throws a warning and does not create a handle. Perhaps you can try the following:

$link='ftp://mylogin:mypassword@myhost.com:21/domains/folder1/folder2/123changes.doc'; 
if(!file_exists($link))
    die("The file does not exist");

$h=fopen($link,'r'); 
if(!$h){ 
 echo 'bad'; 
}else 
 echo 'good';  
}


#5
gelembjuk

gelembjuk

    Newbie

  • Members
  • Pip
  • 5 posts

John said:

For me, the code in your original post throws a warning and does not create a handle. Perhaps you can try the following:

$link='ftp://mylogin:mypassword@myhost.com:21/domains/folder1/folder2/123changes.doc'; 

if(!file_exists($link))

    die("The file does not exist");


$h=fopen($link,'r'); 

if(!$h){ 

 echo 'bad'; 

}else 

 echo 'good';  

}

Thanks for your advice.
But this doesn't help me.
function file_exists always returns FALSE for me. Even if file exists.
There is something definitely wrong with this wrapper in my installation.

I have found one solution, but it is very slow. I just list all files in directory of FTP server and see if needed file is there. I still want to find good solution for this problem.

#6
gelembjuk

gelembjuk

    Newbie

  • Members
  • Pip
  • 5 posts
To solve my problem i use function ftp_size to check if file exists before downloading it.
But i still don't know what is the problem with my wrapper.

#7
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
If you'd rather hack up a way than to use ftp_* functions go ahead, but you'll need to do some tricky things:
$link = 'ftp://anonymous@ftp9.us.postgresql.org:21/pub/mirrors/postgresql/README'; //Sample
if(!$h = @fopen($link,'r')) {
    print "bad";
} else {
    print "good";
    $sData = "";
    while(!feof($h))
        $sData .= fread($h, 4024);
    fclose($h);
}
You won't need to use ftp_size to check if it exists now, although I'd recommend to just stick with alternates to fopen() for that.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#8
gelembjuk

gelembjuk

    Newbie

  • Members
  • Pip
  • 5 posts

Nullw0rm said:

If you'd rather hack up a way than to use ftp_* functions go ahead, but you'll need to do some tricky things:
$link = 'ftp://anonymous@ftp9.us.postgresql.org:21/pub/mirrors/postgresql/README'; //Sample

if(!$h = @fopen($link,'r')) {

    print "bad";

} else {

    print "good";

    $sData = "";

    while(!feof($h))

        $sData .= fread($h, 4024);

    fclose($h);

}
You won't need to use ftp_size to check if it exists now, although I'd recommend to just stick with alternates to fopen() for that.

What alternatives to fopen() do you mean?



I have tried your example with little modificztion:

$link = 'ftp://anonymous@ftp9.us.postgresql.org:21/pub/mirrors/postgresql/README22'; //Sample

if(!$h = @fopen($link,'r')) {

    print "bad";

} else {

    print "good";

    $sData = "";

    while(!feof($h)){

	    echo '<br>read';

        $sData .= fread($h, 4024);

    }

echo $sData;

    fclose($h);

}


That file doesn't exists and i still see 'good'. echo $sData; shows nothing. $sData is empty.
I see:
good
read

#9
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Your exact code reports the file as "bad" for me, as we're getting inconsistant results you should best be using proper FTP functions rather than fopen's wrapper:
http://ca3.php.net/m.../en/ref.ftp.php
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#10
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
appearantly the fopen returns false only when an error occured. And a non-existing file isn't handled as error.

if (file_exists("myfile.php"))

source: PHP ::

#11
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts

oxano said:

appearantly the fopen returns false only when an error occured. And a non-existing file isn't handled as error.:[/url]

That's why I used the presidence to check if fopen returns a warning (550 from FTP that file doesn't exist), for some reason his installation doesn't report it.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.