I post pictures from other websites and I would rather have those on my servers, in case their server dies all of a sudden. Say the file is located at "www.www.www/image.gif", how would I copy it to my directory "images" safely?
I write in PHP.
Thanks!
The following should work:
// requires allow_url_fopen
$image = file_get_contents('http://www.url.com/image.jpg');
file_put_contents('/images/image.jpg', $image);
or the cURL route:
$ch = curl_init('http://www.url.com/image.jpg');
$fp = fopen('/images/image.jpg', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
If your server is configured to support http:// file paths, you could use file_get_contents.
If that doesn't work, the second simplest way is by using curl for which you will certainly find full-fledged download scripts.
Some servers you pull images from may require a User-agent that shows that you are a regular browser. There is a ready-made class in the User Contributed Notes to curl that handles that, and provides a simple DownloadFile() function.
Related
I want to upload the file from local to server using curl in php and without using the form (which is html).
My php version is 5.2.6.
I have tried many different way and make many research about (upload file to server using curl in php), but the solution in google cannot solve my problem.
Below is my code:
// open file descriptor
$fp = fopen ("user.com/user/fromLocal.txt", 'w+') or die('Unable to write a file');
// file to download
$ch = curl_init('C:/wamp/www/user/fromLocal.txt');
// enable SSL if needed
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// output to file descriptor
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
// set large timeout to allow curl to run for a longer time
curl_setopt($ch, CURLOPT_TIMEOUT, 1000);
curl_setopt($ch, CURLOPT_USERAGENT, 'any');
// Enable debug output
curl_setopt($ch, CURLOPT_VERBOSE, true);
echo $ch;
echo $fp;
curl_exec($ch);
curl_close($ch);
fclose($fp);
Expected output:
The file can upload to server and view.
Actual output:
Unable to write the file
I think you miss important information.
fopen ("user.com/user/fromLocal.txt", 'w+')
this means nothing.
To send a file to the server the server has to be configured to accept a POST request and you have to send the file through that endpoint.
If you have a form, do you send it to: "user.com/user/fromLocal.txt" ??
You have to create the form data with curl and send it to a server ready to accept your request. There are different ways to accomplish that. And the most simple is exactly to send a form using curl and not the HTML. But absolutly you cannot write a file like that in a server.
I'm try to save some pictures from url to my server, but i'm not able to do it.
this is my code (it's standard, i've found on internet):
$ch = curl_init($url);
$fp = fopen($img, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
but for each link i put inside $url variable, on my server is always saved a 26 byte image (that's not the original image).
What's wrong?
I can successfully download image using your curl code. It can happen that your server is not allowed to connect the outside web links.
This is a curl equivalent code that download images as well. I believe from your server, you can not download image using this code.
file_put_contents("img.jpg", file_get_contents("http://www.letteratu.it/wp-content/uploads/2014/03/cielo.jpg"));
Run your curl with verbose mode to see the curl's debug messages, and show that us.
curl_setopt($ch, CURLOPT_VERBOSE, 1);
I'm pretty sure you need to include the http:// in the URL. I'm fairly certain it thinks that it is a local file without it (i.e., an implicit file://).
I am trying to go through a basic curl example. I have a php file running on a server that seems to have curl properly installed and I have another empty text document on the server that I am trying to have information copied to. When I run the script by opening the php file, no information is written to the text file.
For the server, it says curl is enabled:
curl
cURL support enabled
cURL Information libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
Here is the php file I am trying to run:
http://www.php.net/manual/en/curl.examples-basic.php
<?php
$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
I have an empty file named "example_homepage.txt" in the same folder. Any idea what is going wrong?
Your script is working perfectly. A quick test with curl example.com shows that it returns no output. If you want to actually fetch content, try a real site like $ch = curl_init("http://www.google.com/");.
The address "www.example.com" is just that, an example. You're meant to replace it with something real.
I try to download a file from FTP server into client. If I use ftp_get, the file is downloaded into PHP server, which can write the output into browser. So the download process is
FTP server -> PHP server -> client
This doubles traffic - this is bad in downloading big files. There is a way how to write the file directly into the browser described here: Stream FTP download to output - but the data flows through PHP server anyway, am I right?
Is there any way how to establish this download (if yes, how?), or is it principially impossible?
FTP server -> client
Edit: it should work also with non-anonymous FTP servers in secure way.
Download the file ;-)
If the client can directly access the file in question (i.e. no secret usernames or passwords necessary), just redirect him to it:
header('Location: ftp://example.com/foobar');
This will cause the client to access the URL directly. You can't control what the client will do though. The browser may simply start to download the file, but it may also launch an FTP client or do other things which you may or may not care about.
try below code for that.
$curl = curl_init();
$file = fopen("ls-lR.gz", 'w');
curl_setopt($curl, CURLOPT_URL, "ftp://ftp.sunet.se/ls-lR.gz"); #input
curl_setopt($curl, CURLOPT_FILE, $file); #output
curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_exec($curl);
Thanks.
I'm having a good deal of trouble sending a file from a linux server to a windows server over SFTP via PHP.
I seem to be connecting just fine, but it always throws an error that I can't create the file on the remote server's end. It's possible that I am messing up at the syntax for the file location.
I have tried two ways now, one using ssh2_scp_send(), and another trying
fopen(ssh2.sftp://D:/path/file.csv)
Also, logging into the sftp server via a client puts me at my home folder (ie D:\path\to\home) but if I do a
ssh2_exec($connection, 'cd');
and print the stream to the screen, it shows me that my ssh session is currently in the windows filesystem on the C drive.
I was hoping someone would have some advice on this. And I'm not married to this method. I'm using php on my end because it's all coming from a drupal module, but I could always try and incorporate another method.
If all you want to do is send one file, curl works, I suppose, but if you want to do anything more - like maybe verifying that the file has been uploaded, upload multiple files, use publickey authentication, or whatever, curl just isn't versatile enough for that.
My recommendation would be to use phpseclib, a pure PHP SFTP implementation.
CURL can use th sftp library.
$localfile = 'test.txt';
$ch = curl_init();
$fp = fopen ($localfile, "r") or die('Cannot open textfile');
curl_setopt($ch, CURLOPT_URL,"sftp://123.123.123.123:10022/username/test.txt");
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
if(curl_exec($ch)){
echo 'File was successfully transferred using SFTP';
}else{
echo 'File was unable to be transferred using SFTP';
}
curl_close ($ch);