For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > October 2006 > PHP - FTP Download









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author PHP - FTP Download
Vic Spainhower

2006-10-30, 7:03 pm

Hello,

I am trying to ftp a file from an ftp server to the local drive. However,
when I try to open the output file on the local drive ( $handle =
fopen($localfile, 'w');) I get an error and it is trying to open a file in
the server path!!

following is the PHP script:

Thanks for any help on this I could get!

Vic



$handle =
fopen("/home/sitename/public_html/mydomain/uploads/ShowID.txt","w");
if (fwrite($handle, $showID) === FALSE) {
echo "Cannot write to uploads folder on server - contact
support@mydomain.com ";
exit;
}

$host="ftp.mydomain.com";
$user="username@mydomain.com";
$password="mypassword";
$remotefile="/home/sitename/public_html/mydomain/uploads/ShowID.txt";
$localfile="C:/DownloadFiles/ShowID.txt";

$conn=ftp_connect($host);
if (!$conn)
{
echo "error unable to connect to ftp server to transmit
showid - contact support@showmyhorse.com";
exit;
}

$result = ftp_login($conn, $user, $password);
if (!result)
{
echo "could not log on as secretary on ftp server - contact
support@showmyhorse.com";
exit;
}


// open the file on the local drive
$handle = fopen($localfile, 'w'); // Fails here msg = Warning:
fopen(C:/DownloadFiles/ShowID.txt) [function.fopen]: failed to open stream:
No such file or directory in
/home/sitename/public_html/mydomain/uploads/upload.php on line 318


// try to download $remote_file and save it to $handle
if (ftp_fget($conn, $handle, $remotefile, FTP_ASCII, 0)) {
echo "successfully written to $localfile\n";
} else {
echo "There was a problem while downloading $remote_file to
$local_file\n";
}


Koncept

2006-10-30, 7:03 pm

In article < 7IidndFOha73td_YnZ2dnUVZ_tydnZ2d@comcast
.com>, Vic
Spainhower <vic@showsec.com> wrote:

> Hello,
>
> I am trying to ftp a file from an ftp server to the local drive. However,
> when I try to open the output file on the local drive ( $handle =
> fopen($localfile, 'w');) I get an error and it is trying to open a file in
> the server path!!
>
> following is the PHP script:
>
> Thanks for any help on this I could get!
>
> Vic



<?php
$ftp = (object) array(
"host" => "host",
"user" => "username",
"password" => "password",
"remoteFile" => "/home/sitename/public_html/"
."mydomain/uploads/ShowID.txt",
"localFile" => "C:/DownloadFiles/ShowID.txt"
);

// Step one. Verify the local file can be created and has write access
if( !is_resource( $localRsrc=@fopen( $ftp->localFile,"w" ) ) )
trigger_error( "Could not create or open local "
. "file for writing", E_USER_ERROR );

// Step two: Connect to the remote server
if( !is_resource( $remoteRsrc=@ftp_connect( $ftp->host ) ) )
trigger_error( "Could not connect to remote "
. "server {$ftp->host}", E_USER_ERROR );

// Step three: Log on to the remote server
if( !@ftp_login( $remoteRsrc,$ftp->user,$ftp->password ) )
trigger_error( "Could not log on to remote "
. "server {$ftp->host}", E_USER_ERROR );

// Step four: Get the remote file and write to local resource
if( !@ftp_fget( $remoteRsrc, $localRsrc,
$ftp->remoteFile,FTP_ASCII, 0 ) )
trigger_error( "Could not write remote file "
. "to local file", E_USER_ERROR );

// Step five: Print success message
echo "Remote file successfully written to {$ftp->localFile}";
?>

--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Vic Spainhower

2006-10-30, 7:03 pm

Koncept,

Thanks for the reply and script! I replaced the script I was using with the
one supplied by you and I get basically the same error:

Fatal error: Could not create or open local file for writing in
/home/pointkee/public_html/myDomain/uploads/upload.php on line 298

It is failing in step #1 but is pointing to the server instead of the path
on the local system! ??? I don't understand what is going on, can you shed
some light?

Thanks,

Vic




"Koncept" <user@unknown.invalid> wrote in message
news:271020061920421863%user@unknown.invalid...
> In article < 7IidndFOha73td_YnZ2dnUVZ_tydnZ2d@comcast
.com>, Vic
> Spainhower <vic@showsec.com> wrote:
>
>
>
> <?php
> $ftp = (object) array(
> "host" => "host",
> "user" => "username",
> "password" => "password",
> "remoteFile" => "/home/sitename/public_html/"
> ."mydomain/uploads/ShowID.txt",
> "localFile" => "C:/DownloadFiles/ShowID.txt"
> );
>
> // Step one. Verify the local file can be created and has write access
> if( !is_resource( $localRsrc=@fopen( $ftp->localFile,"w" ) ) )
> trigger_error( "Could not create or open local "
> . "file for writing", E_USER_ERROR );
>
> // Step two: Connect to the remote server
> if( !is_resource( $remoteRsrc=@ftp_connect( $ftp->host ) ) )
> trigger_error( "Could not connect to remote "
> . "server {$ftp->host}", E_USER_ERROR );
>
> // Step three: Log on to the remote server
> if( !@ftp_login( $remoteRsrc,$ftp->user,$ftp->password ) )
> trigger_error( "Could not log on to remote "
> . "server {$ftp->host}", E_USER_ERROR );
>
> // Step four: Get the remote file and write to local resource
> if( !@ftp_fget( $remoteRsrc, $localRsrc,
> $ftp->remoteFile,FTP_ASCII, 0 ) )
> trigger_error( "Could not write remote file "
> . "to local file", E_USER_ERROR );
>
> // Step five: Print success message
> echo "Remote file successfully written to {$ftp->localFile}";
> ?>
>
> --
> Koncept <<
> "The snake that cannot shed its skin perishes. So do the spirits who are
> prevented from changing their opinions; they cease to be a
> pirit." -Nietzsche



Koncept

2006-10-30, 7:03 pm

> > <?php
>
>

In article < 342dnc_wDPeUP9_YnZ2dnUVZ_rWdnZ2d@comcast
.com>, Vic
Spainhower <vic@showsec.com> wrote:

> Koncept,
>
> Thanks for the reply and script! I replaced the script I was using with the
> one supplied by you and I get basically the same error:
>
> Fatal error: Could not create or open local file for writing in
> /home/pointkee/public_html/myDomain/uploads/upload.php on line 298
>
> It is failing in step #1 but is pointing to the server instead of the path
> on the local system! ??? I don't understand what is going on, can you shed
> some light?
>
> Thanks,
>
> Vic
>


$localFile should *not* be "/home/pointKee/...". The "Local file" is
the file on *your* computer "C:/path/to/file/copied/from/server.txt"
which you are copying to. $remoteFile should be the path to the file on
the *FTP* *server* which you wish to *copy* *from*.

ftp_fget() writes from a remote resource to a local resource. The local
resource is $localRsrc ( aka. your file in C:/etc which you open in
step one).

You are getting an error for one of two reasons:
1) The path to your local file ( sub directories too ) do(es) not yet
exist
2) You don't have permissions to write to the local file.

Uncomment the @ sign in front of fopen to see what error PHP throws. If
you still don't understand this, post your exact code here. I tested
this code out on a remote server and it wrote to a local file just fine
with PHP 5.1.6

--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Koncept

2006-10-30, 7:03 pm

In article < 342dnc_wDPeUP9_YnZ2dnUVZ_rWdnZ2d@comcast
.com>, Vic
Spainhower <vic@showsec.com> wrote:

> Koncept,
>
> Thanks for the reply and script! I replaced the script I was using with the
> one supplied by you and I get basically the same error:
>
> Fatal error: Could not create or open local file for writing in
> /home/pointkee/public_html/myDomain/uploads/upload.php on line 298
>
> It is failing in step #1 but is pointing to the server instead of the path
> on the local system! ??? I don't understand what is going on, can you shed
> some light?
>
> Thanks,
>
> Vic



BTW... don't forget to close your open resources at the end of your
code....

<?php
ftp_close($remoteRsrc );
fclose($localRsrc );
?>

--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Vic Spainhower

2006-10-30, 7:03 pm

Hi Koncept,

Here's the error that I'm getting: I do have the local file pointing to my
C: drive and remote is pointed to the server which is what's confusing me!

Warning: fopen(C:/Database/ShowSecXP/ShowMyHorseID.txt) [function.fopen]:
failed to open stream: No such file or directory in
/home/pointkee/public_html/showmyhorse/uploads/upload.php on line 296

Fatal error: Could not create or open local file for writing in
/home/pointkee/public_html/showmyhorse/uploads/upload.php on line 298


The path C:/Database/ShowSecXP does exist on my local drive and here's the
code.

$ftp = (object) array(
"host" => "ftp.mydomain",
"user" => "showsecretary@mydomain",
"password" => "upload",
"remoteFile" => "/home/pointkee/public_html/mydomain/uploads/ShowID.txt",
"localFile" => "C:/Database/ShowSecXP/ShowID.txt"
);

// Step one. Verify the local file can be created and has write access
if( !is_resource( $localRsrc=fopen( $ftp->localFile,"w" ) ) )
trigger_error( "Could not create or open local "
. "file for writing", E_USER_ERROR );

// Step two: Connect to the remote server
if( !is_resource( $remoteRsrc=@ftp_connect( $ftp->host ) ) )
trigger_error( "Could not connect to remote "
. "server {$ftp->host}", E_USER_ERROR );

// Step three: Log on to the remote server
if( !@ftp_login( $remoteRsrc,$ftp->user,$ftp->password ) )
trigger_error( "Could not log on to remote "
. "server {$ftp->host}", E_USER_ERROR );

// Step four: Get the remote file and write to local resource
if( !@ftp_fget( $remoteRsrc, $localRsrc,
$ftp->remoteFile,FTP_ASCII, 0 ) )
trigger_error( "Could not write remote file "
. "to local file", E_USER_ERROR );

// Step five: Print success message
echo "Remote file successfully written to {$ftp->localFile}";
exit;


Koncept

2006-10-30, 7:03 pm

In article < CpSdnRjai91ATN_YnZ2dnUVZ_tqdnZ2d@comcast
.com>, Vic
Spainhower <vic@showsec.com> wrote:

> Hi Koncept,
>
> Here's the error that I'm getting: I do have the local file pointing to my
> C: drive and remote is pointed to the server which is what's confusing me!
>
> Warning: fopen(C:/Database/ShowSecXP/ShowMyHorseID.txt) [function.fopen]:
> failed to open stream: No such file or directory in
> /home/pointkee/public_html/showmyhorse/uploads/upload.php on line 296
>
> Fatal error: Could not create or open local file for writing in
> /home/pointkee/public_html/showmyhorse/uploads/upload.php on line 298


Hmmm. Just out of interest, what version are you running?

How about if we attack this from another similar (but different) angle?

<?php

$ftp = (object) array(
"host" => "ftp.mydomain",
"user" => "showsecretary@mydomain",
"password" => "upload",
"remoteFile" =>
"/home/pointkee/public_html/mydomain/uploads/ShowID.txt",
"localFile" => "C:/Database/ShowSecXP/ShowID.txt",
"mode" => FTP_ASCII
);

// Step one: Connect to the FTP server
if(!is_resource($remoteRsrc=@ftp_connect
($ftp->host)))
trigger_error("Could not connect to remote "
. "server {$ftp->host}", E_USER_ERROR );

// Step two: Log into the server
if( !@ftp_login($remoteRsrc,$ftp->user,$ftp->password))
trigger_error("Could not log on to remote "
. "server {$ftp->host}", E_USER_ERROR );

// Step Three: Attempt to copy remote file to local file
if(@ftp_get($remoteRsrc,$ftp->localFile,$ftp->remoteFile,$ftp->mode))
echo "Remote file successfully written to {$ftp->localFile}";
else
trigger_error("Could not import remote file.",E_USER_ERROR);

// Step Four: Close the connection
ftp_close($remoteRsrc);

?>

--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Vic Spainhower

2006-10-30, 7:03 pm

Koncept,

I am running php version 4.3.10

When trying to download the file to C:/tmp I get the following error:

Warning: ftp_get() [function.ftp-get]: Error opening C:/tmp/ShowID.txt in
/home/pointkee/public_html/mydomain/uploads/upload.php on line 306

Fatal error: Could not import remote file. in
/home/pointkee/public_html/mydomain/uploads/upload.php on line 309

If I remove the C: then I get the following error:

Warning: ftp_get() [function.ftp-get]: Can't open
/home/pointkee/public_html/showmyhorse/uploads/ShowID.txt: No such file or
directory in /home/pointkee/public_html/mydomain/uploads/upload.php on line
306

Fatal error: Could not import remote file. in
/home/pointkee/public_html/mydomain/uploads/upload.php on line 309


Vic




Koncept

2006-10-30, 7:03 pm

In article < WZidnQe5_LsZx97YnZ2dnUVZ_oudnZ2d@comcast
.com>, Vic
Spainhower <vic@showsec.com> wrote:

> Warning: ftp_get() [function.ftp-get]: Error opening C:/tmp/ShowID.txt in
> /home/pointkee/public_html/mydomain/uploads/upload.php on line 306


You're on Windoze locally! Shouldn't the path items be delimited via
"\" and not "/" ?

aka fopen("c:\\tmp\\showID.txt","w")

--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Vic Spainhower

2006-10-30, 7:03 pm

Koncept,

aka fopen("c:\\tmp\\showID.txt","w")

I've tried both ways, but my understanding is that Windows will work with
forward slashes.

Vic


"Koncept" <user@unknown.invalid> wrote in message
news:281020061659516221%user@unknown.invalid...
> In article < WZidnQe5_LsZx97YnZ2dnUVZ_oudnZ2d@comcast
.com>, Vic
> Spainhower <vic@showsec.com> wrote:
>
>
> You're on Windoze locally! Shouldn't the path items be delimited via
> "\" and not "/" ?
>
> aka fopen("c:\\tmp\\showID.txt","w")
>
> --
> Koncept <<
> "The snake that cannot shed its skin perishes. So do the spirits who are
> prevented from changing their opinions; they cease to be a
> pirit." -Nietzsche



Vic Spainhower

2006-10-30, 7:03 pm

Koncept,

I'm being told by my ISP that they block fopen requests to download files on
the shared server and I would need to upgrade to a dedicated server to
download files using fopen and ftp. My cost would go from $7.00 to $100.00
per month and I can't do that. All I'm trying to do is get a long number
back to the Access program that did the upload of some data which represents
a record id in a table that was loaded. Can you suggest an alternate method
to do this without using FTP?

Thanks a lot,

Vic




"Koncept" <user@unknown.invalid> wrote in message
news:281020061659516221%user@unknown.invalid...
> In article < WZidnQe5_LsZx97YnZ2dnUVZ_oudnZ2d@comcast
.com>, Vic
> Spainhower <vic@showsec.com> wrote:
>
>
> You're on Windoze locally! Shouldn't the path items be delimited via
> "\" and not "/" ?
>
> aka fopen("c:\\tmp\\showID.txt","w")
>
> --
> Koncept <<
> "The snake that cannot shed its skin perishes. So do the spirits who are
> prevented from changing their opinions; they cease to be a
> pirit." -Nietzsche



Koncept

2006-10-30, 7:03 pm

In article < IJednUH8CZ8hjNnYnZ2dnUVZ_s6dnZ2d@comcast
.com>, Vic
Spainhower <vic@showsec.com> wrote:

> Koncept,
>
> I'm being told by my ISP that they block fopen requests to download files on
> the shared server and I would need to upgrade to a dedicated server to
> download files using fopen and ftp. My cost would go from $7.00 to $100.00
> per month and I can't do that. All I'm trying to do is get a long number
> back to the Access program that did the upload of some data which represents
> a record id in a table that was loaded. Can you suggest an alternate method
> to do this without using FTP?
>
> Thanks a lot,
>
> Vic


Never heard of blocking fopen ports before. $100 a month to enable
seems a bit excessive don't you think? lol. <*}}}>{{{ (* fishy * )

If you want some solid hosting, I can put you in touch with somebody I
know that resells for a very reliable hosting company. The server
itself is clustered so speed is very good and 100% uptime is 100%
guaranteed. It also has PHP4/5 support ( even ASP ). Depending on what
language your script is built in, will depend on what server processes
the document. I currently host five sites with him and the service is
terrific. nubeat << gmail << com

As for your situation. I'm glad to learn that I am not going crazy!

You could try sockets or maybe shell exec via another program ( python,
ruby, CGI ) to get the file from one place to the other. It's going to
be tricky though if your ISP wants to make it that way.

If SSH enabled on your account perhaps you can even tunnel request
through allowed ports.

--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Vic Spainhower

2006-10-30, 7:03 pm

Koncept,

OK the tech who told me fopen was blocked was out in left field. What is
happening is the file is being placed back into the server's ftp folder.
When I changed the destination name so that it was different then the source
name I realized what was going on but I don't know why! It is named
C:\TestShowID.txt on the server! Can you tell me why this is happening?
Thanks,

Vic

$ftp = (object) array(
"host" => "ftp.mydomain.com",
"user" => "showsecretary@mydomain.net",
"password" => "download",
"remoteFile" => "ShowID.txt",
"localFile" => "C:\\TestShowID.txt"
);
// Step one: Connect to the FTP server
if(!is_resource($remoteRsrc=@ftp_connect
($ftp->host)))
trigger_error("Could not connect to remote "
. "server {$ftp->host}", E_USER_ERROR );

// Step two: Log into the server
if( !@ftp_login($remoteRsrc,$ftp->user,$ftp->password))
trigger_error("Could not log on to remote "
. "server {$ftp->host}", E_USER_ERROR );

// Step Three: Attempt to copy remote file to local file
if(@ftp_get($remoteRsrc,$ftp->localFile,$ftp->remoteFile,FTP_BINARY))
echo "Remote file successfully written to {$ftp->localFile}";
else
trigger_error("Could not import remote file.",E_USER_ERROR);

// Step Four: Close the connection
ftp_close($remoteRsrc);


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com