0

Hi,

I want to connect to a external server to either copy image.jpg to my local computer or list all the files stored in a path.

How can I do this? I think cURL is used but examples on the web are not working for me.

Thanks in advance

For example:

Server: 192.168.11.111
Username: root
Password: 123123
Port: 22

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by hielo
0

Curl is used for http requests. Based on the sample port you posted, you are probably wanting to connect to an FTP server (Port 21). If so, refer to the FTP functions in php:
http://www.php.net/manual/en/function.ftp-connect.php

IF you are in fact running an HTTP server, then on the following link:
http://blog.unitedheroes.net/curl/

the third example under the "What to do" section shows you how to use curl.

An alternative to curl would be:
file_get_contents();
http://us.php.net/manual/en/function.file-get-contents.php

So if you know the image url, try:

$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar\r\n"
  )
);

$context = stream_context_create($opts);

file_put_contents( $_SERVER['DOCUMENT_ROOT'].'/theRemoteImage.gif', file_get_contents('http://remotesite.com/image.gif'), 0, $context);
0

If you are using this code to download an image to your computer (and not a server), I would use Python instead since it is suited for server side development as well as little scripts you want to schedule on your computer.
If you would like to continue with PHP, hielo has the right idea. Although I think cURL can handle FTP if you use an FTP URL like ftp://user:pass@host:port, but I'm not sure.
On second glance I noticed that you provided us with port 22, which happens to be the port for SSH (most likely for SFTP use). If you are using SSH/SFTP, you will need to use PHP's SSH functions.

Edited by FlashCreations: n/a

0

Hi,

Hielo thanks for your example. It works great. I also, found one more example which works fine as well. However, I need your opinions. Which one do you think would be safer and better approach?

Example 1:

$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar\r\n"
  )
);

$context = stream_context_create($opts);

file_put_contents( $_SERVER['DOCUMENT_ROOT'].'/theRemoteImage.gif', file_get_contents('http://remotesite.com/image.gif'), 0, $context);

Example 2:

//Source : external server
$targetFile = fopen("https://192.168.11.111/photos/myphoto.gif", "r");
//Target: my local computer
$handle = fopen("photos/theLocalCopy.gif", "w");

while(! feof($targetFile)){
	$output = fread($targetFile, 1008192);
	fwrite($handle, $output);
}
    
fclose($targetFile);
fclose($handle);
0

I can't say which one is "better" than the other. My suggestion to you would be to use whichever of the two you find easier to understand. Down the road you may need to come back to your script and make changes. Understanding well what your code does will make your job easier.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.