I am trying to download a file using PHP and CURL, here is my code:
set_time_limit(0);
// Set file to write to
$file = fopen($nameGenerated['path'], 'w+');
$ch = curl_init();
if ($ch == false) {
die('Failed to create curl handle');
}
// Set Curl options
curl_setopt($ch, CURLOPT_URL, str_replace(" ", "%20", $uri));
curl_setopt($ch, CURLOPT_FILE, $file);
// get curl response
curl_exec($ch);
curl_close($ch);
fclose($file);
The file is empty and curl_exec always returns false. When I tried to get the errors using curl_error($ch) there was no error.
Note: I am using Yii2.
Can someone help me understand what's the matter?
I managed to fix the problem by using the ipv4 addess instead of the usual 127.0.0.1. Now I know the problem, the CURL was excepting an ip address, not the localhost or 127.0.0.1. Thanks for all the people who tried to help!
Related
Currently I am successfully downloading a file using a PHP cURL request. The issue is that the remote file seems to get deleted after each download. Is there a setting to tell cURL to not delete the file after the download? I'm not finding anything on this when researching online. I'm actually only finding questions asking how to delete the files after downloading which is obviously working for me already but not my desired result.
Here is basically the code I am running currently:
$url = 'ftp://ftp.example.com/file.txt';
$username = 'username';
$password = 'password';
$filename = dirname(__FILE__) . '/file.csv';
$fp = fopen($filename, 'w');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILE, $fp);
$output = curl_exec($ch);
curl_close($ch);
fclose($fp);
Would anybody happen to be able to provide me with some suggestions as to how NOT to delete the remote files?
Thanks!
Well I've contacted the remote source and sure enough they were archiving the files just to keep the directory clean.
Thanks to all!
I want to open and write a txt file from another server, but I don't know how to do it? Can anyone help me?
<?PHP
$fname=$_POST["fname"];
$groupid=$_POST["groupid"];
$myfile = fopen("Ji.txt", "a+") or die("Unable to open file!");
fwrite($myfile, $fname."|".$groupid."\r\n");
fclose($myfile)
?>
I want to replace Ji.txt with http://xxx.xxx.xx.x/ddd.txt
You can read the files over HTTP by using fopen or cURL.
You can't write to files over HTTP unless the server you are writing to is set up to understand an appropriate request. You could configure it to support PUT requests (make sure you have some kind of authn/authz system in place!) and make one using cURL.
Alternatively, you could use some other protocol to make the file available between servers (such as NFS).
Alternatively you can do it with the FTP functions
<?php
// Connect to remote FTP server
$conn = ftp_connect("ftp.example.com")or die("Cant connect to ftp server");
$login = ftp_login($conn, "username", "password");
// Open local (temporary) file handle
$fh = fopen("Ji.txt", "a+");
// Get remote file and save it to the previous file handle
if(ftp_fget($conn, $fh, "Ji.txt", FTP_ASCII))
{
// Local file has now been updated with the content of the remote Ji.txt
$fname = $_POST['fname'];
$groupid = $_POST['groupid'];
fwrite($fh, $fname.'|'.$groupid.'\r\n');
if(ftp_fput($conn, "Ji.txt", $fh, FTP_ASCII))
echo 'File saved to remote server';
else
echo 'Error saving to remote server';
}
else
echo 'Error downloading remote file';
ftp_close($conn);
fclose($fh);
?>
Read more about ftp_fput etc here: http://php.net/manual/en/function.ftp-fput.php
As long as allow_url_fopen is set to True you can pass an URL to fopen() and read the data. Writing however, is impossible. However, you can write a wrapper around the file that accepts both GET & POSTS requests. A GET returns the contents of the file and a POST (or PUT) saves content to the files. The downside of this is that you must add security and it's a lot more complex to save the data.
Assuming you have access to the other server and the text file is available at a web address (ie you can see it in a browser) then you can use fopen to grab it, make your edits or whatever and then post back to a script that will handle the post data and save the file using curl. Example code below copied from this answer
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => '#/path/to/file.txt'));
curl_setopt($ch, CURLOPT_URL, 'http://server2/upload.php');
curl_exec($ch);
curl_close($ch);
Posting server should be something like this
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('ourfilename' =>'yourpath/test.txt'));
curl_setopt($ch, CURLOPT_URL, 'http://urlreceivepost/getfile.php');
curl_exec($ch);
curl_close($ch);
Then getfile.php at your above url
if ($_POST) {
$file= $_POST['ourfilename'];
$data = file_get_contents($file);
$new = 'test.txt';
file_put_contents($new, $data);
var_dump($new);}
I'm using cUrl to get the file's contents of the same website's page, and writing to another file ( To convert dynamic php file into static php file for menu caching purpose )
$dynamic = 'http://mysite.in/menu.php';
$static = "../menu-static.php" ;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$dynamic);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file = curl_exec($ch);
file_put_contents($static, $file);
die();
It works perfect on localhost. But taking too much time when it is running on hosted website, and at last even the output file ($static = "../menu-static.php") is empty.
I can't determine where is the problem .. Please help
I've also tried file_get_contents instead of cUrl with no luck ..
I'm having trouble downloading a remote file via PHP.
I've tried using cURL and streaming, neither of which produces an error.
Here's my current code for streaming.
$url = "http://commissiongeek.com/files/text.txt";
$path = "/files/cb.txt";
file_put_contents($path, file_get_contents($url));
I'll be downloading a zip file when I get this working, but in theory this should work just fine...
The folder's permissions are set to 777, and as said before, no errors are being thrown.
What could cause this?
Split this up into multiple sections, so you can verify that each stage is working:
$url = 'http://...';
$txt = file_get_contents($url);
var_dump($txt);
var_dump(file_put_contents('/files/cb.txt', $txt));
The first dump SHOULD show you whatever that text that url returns. The second dump should output a boolean true/false depending on if the file_put failed or not.
It seems you have an absolute path that you are trying to save in. I believe you want the path changed to "files/cb.txt" instead and do not have any access to /files/
If you have allow_url_fopen set to true:
$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));
Else use cURL:
$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
this is my code but this is not working whats a problem in this code.u have any another code.give me idea to download a file from server using ftp.i am trying this code in my localhost also in my own server.
$curl = curl_init();
$file = fopen("ftpfile/file.csv", 'w');
curl_setopt($curl, CURLOPT_URL, "ftp:http://www.address.com/file.csv"); #input
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FILE, $file); #output
curl_setopt($curl, CURLOPT_USERPWD, "myusername:mypassword");
curl_exec($curl);
I'm guessing one of two things: the fopen call failed (and you're not checking if it succeeded by seeing if $file !== false), or the double-use of _returntransfer AND _file is not acting as you expect.
returntransfer tells curl to return the retrieved data in the exec call, instead of outputting it directly to the browser. I suspect that if you did $data = curl_exec($curl); file_put_contents('ftpfile/file.csv', $data) you'd end up with a properly populatd file. So... either remove the returntransfer option, or eliminate the whole _file business and output the file yourself using what the _exec call returns.