cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

RE: Reading/Writing to the same file via FTP...

From: -Sys Adm- <centralsource_at_hotmail.com>
Date: Fri, 16 Dec 2011 18:37:13 -0500

On Sat, Dec 17, 2011 at 12:19 AM, -Sys Adm- <centralsource_at_hotmail.com> wrote:

> Date: Fri, 16 Dec 2011 12:04:30 +0600
> Subject: Re: Reading/Writing to the same file via FTP...
> From: shiplu.net_at_gmail.com

> To: curl-and-php_at_cool.haxx.se
>
> You need to write the file on local disk first. Then upload it to ftp.
> I have modified your code. See in line bellow.

>
> On Fri, Dec 16, 2011 at 7:41 AM, -Sys Adm- <centralsource_at_hotmail.com> wrote:
> >
> > All I need to do in PHP is:
> >

> > READ the contents of a file on my FTP server. (Contents of file are only a single number, 0 - 12)
> > Add that to a $variable string
> > Change that $variable and re-write the contents to the same file I READ from.

> >
> > I am currently READING the contents of the file and adding that to a $variable perfectly using this function:
> >
> > $expire = "filename";
> >
> > function read_file($expire)

> > {
> > $curl = curl_init();
> > curl_setopt($curl, CURLOPT_URL, "ftp://"location"/".$expire);
> > curl_setopt($curl, CURLOPT_USERPWD, "U:P");

> > curl_setopt($curl, CURLOPT_HEADER, 0);
> > curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
> > curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
> > curl_setopt($curl, CURLOPT_TIMEOUT, 20);

> > $data = curl_exec($curl);
> > curl_close($curl);
> > if ($data != false){
> > return $data;
> > }else{
> > echo 'ERROR';

> > }
> > }
> > $content = read_file($expire);
> >
> >
> > As far as WRITING the $new_content variable back to the same file, I am completely lost on finding any examples of this....

> >
> > My assumption would be:
> >
> > $expire = "filename";
> > $content = $new_contents;
> >
> > function write_file($expire)
> $ch = curl_init();

> $fp = fopen($expire, 'r');
> curl_setopt($ch, CURLOPT_URL,
> 'ftp://ftp_login:password@ftp.domain.com/'.$expire);

> curl_setopt($ch, CURLOPT_UPLOAD, 1);
> curl_setopt($ch, CURLOPT_INFILE, $fp);
> curl_setopt($ch, CURLOPT_INFILESIZE, filesize($expire));
> fclose($fp);
>
> > curl_exec($curl);

> $data = curl_errno($ch);
> > curl_close($curl);
> > if ($data != false){
> > return $data;
> > }else{
> > echo 'ERROR';

> > }
> > }
> >
> >
>
>
>
> --
> Shiplu Mokaddim
> Talks: http://shiplu.mokadd.im

> Follow: http://twitter.com/shiplu
> Innovation distinguishes between follower and leader
>
> _______________________________________________

> http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php

--- This makes no sense to me that in my PHP that fopen, fwrite and fclose can work on the same file "on the fly" with a simple ftp connection call, but cURL can not?

The whole reason I'm using cURL is for it's encrypted authentication to the FTP server.

So basically, if I have to create the file locally with fopen on the client's host machine that is executing my PHP, then they can just "capture" that file prior to cURL uploading it back to the FTP server? This is not at all secure for my application.

I was having this done using a simple FTP connection with fopen, fwrite, fclose, but obviously ftp streams the username:password in clear text... again, the whole reason to use cURL.

Surely there must be a way once I have the "captured" file contents within cURL to overwrite that data "on the fly" and close the file back. With nothing being created locally....

SysAdm

                                               

_______________________________________________

http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php

_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php

Date: Sat, 17 Dec 2011 04:01:31 +0800
Subject: Re: Reading/Writing to the same file via FTP...
From: me_at_lx.sg
To: curl-and-php_at_cool.haxx.se

Write it to php://memory, then pass it to CURLOPT_INFILE as a stream.http://php.net/manual/en/wrappers.php.php
If
 all you're sending is a file containing a number, it would be more
straightforward to just set up a basic API on the receiving end and
communicate over HTTPS instead of messing with FTP (you're sending the
credentials in plaintext even if you're using cURL, unless you're using
FTPS).
With regards,
Liu Shan Shui
http://lx.sg/
"Life would be much easier if I had the source code." - Anonymous

____________________________

Thanks Liu Shan Shui for verifying what I suspected...

I was "assuming" cURL with just FTP access was encrypting the data transit automatically, no need for FTPS or .pem files, or other "handshake" files needed. I guess I was wrong...

Seems like that would be the next industry standard if someone wrote a lib that "encased" a username:password along transit over HTTP using a simple call... without all the HTTPS, FTPS, needing static IP, etc... etc... hassles...

I ended up going HTTPS, with .htaccess username:password "handshake", then I just used the good old fopen, fwrite, and fclose technique.

Consider this thread SOLVED!

Thanks for everyone's input and ideas....

SysAdm

                                               

_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
Received on 2011-12-17