I'm writing a script to download files from Megaupload onto my server. I'm using cURL on PHP, I have the login script which downloads the cookie file:
<?php
function login($username, $password){
$mega = curl_init();
curl_setopt($mega, CURLOPT_URL, "http://www.megaupload.com/?c=login");
curl_setopt($mega, CURLOPT_POST, true);
curl_setopt($mega, CURLOPT_POSTFIELDS, "login=1&redir=1&username=$username&password=$password");
curl_setopt($mega, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/megaupload_cookie.txt");
curl_setopt($mega, CURLOPT_COOKIEJAR, dirname(__FILE__) . "/megaupload_cookie.txt");
curl_exec($mega);
curl_close($mega);
}
?>
and the downloading script:
<?php
include("megaupload_login.php");
login("username", "ps");
set_time_limit(0);
$url = "http://www.megaupload.com/?d=A428CAKH";
$fp = fopen("winch.zip", "w");
$dl = curl_init($url);
curl_setopt($dl, CURLOPT_COOKIEFILE, "megaupload_cookie.txt");
curl_setopt($dl, CURLOPT_FILE, $fp);
curl_exec($dl);
curl_close($dl);
fclose($fp);
?>
The problem is, the file doesn't download. All I get is a file named winch.zip with a size of 0 bytes. I think the program is actually downloading the login page, as when run the script the browser just shows the megaupload login page but the address is localhost. Any ideas on why this might not be working?
Try Following for your download part of code:
function download_file($link) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookies/megaupload.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$filecontents = download_file("http://www.megaupload.com/?d=A428CAKH");
Hope it helps
Related
The $source is the url link of an image from a website that requires authentication/login. I was able to download the image but the downloaded image can't be opened. I knew the image is not downloaded properly as it is asking for credentials. I use CURL to download the image and now, I have no idea what's wrong with the code, what else should i add to it??
<?php
$username = $_SESSION["username"];
$password = $_SESSION["password"];
$source = 'https://example.com/sequence.png';
$fp = fopen('C:/image/'.basename($source), 'w');
$ch = curl_init($source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY ) ;
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");// to use for connection
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 1);// content type
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_exec($ch);
fclose($fp);
?>
If your authentication is not basic, you need some library like Guzzle.
You can do the login process and then download the image
I was trying to upload image using curl put request, And was able to upload image if the image is stored in local directory. but the problem is that i have to upload image from another image url.
could anyone help me.
$localfile = "testing.jpg";
// echo filesize($localfile); die;
$url = API_URL . "pages/343/files/=".$localfile."?description=testing";
$fp = fopen ($localfile, "r");
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_USERPWD, API_USERNAME . ":" . API_PASSWORD);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
$http_result = curl_exec($ch);
$error = curl_error($ch);
$http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE);
// $jsonOutput = json_decode($http_result, true);
curl_close($ch);
fclose($fp);
i have tried to change $localfile to
$localfile = "https://someurl/someimage.png";
but it gave me following error.
Warning: curl_setopt(): cannot represent a stream of type tcp_socket/ssl as a STDIO FILE* in C:\xampp\htdocs\newPutMedia.php on line 29
Warning: filesize(): stat failed for https://help.optimizely.com/hc/en-us/article_attachments/200205465/1._Site-Wide_Addition_gmc_BEFORE.png in C:\xampp\htdocs\newPutMedia.php on line 30
Apparently this is a bug in PHP cURL. You can use the bug fix as mentioned here or you can do this :
Copy the file from URL to your local directory :
file_put_contents(__DIR_\_ . "/testing.jpg", file_get_contents($url));
Use the new file stored in your server for cURL PUT request :
$localfile = __DIR_\_ . "/testing.jpg";
Since the bug does not exist for file reading from your server, the bug won't cause errors and this would perfectly work.
Hello I'm having problems uploading a file to the site bellow, I have inspected the elements and added them to the $data file, I'm fairly new to this, so please guide me.
function upload($url,$data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "C:/xampp/htdocs/curl_upload/cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "C:/xampp/htdocs/curl_upload/cookies.txt");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 1000);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$data = array("file_0"=>"#C:\files\somerarfile.rar","submit_btn"=>" Upload! ");
echo upload("http://180upload.com/",$data);
Have you tried this on a live domain? Everything appears to be in shape, maybe there's a problem with the local php.ini, filesize or your operating system (with its security).
Try on a live (sub)domain.
To set you max filesize, try
<?php
ini_set('upload_max_filesize', '40M');
ini_set('post_max_size', '40M');
?>
Obviously, set the values to whatever you need.
With your upload page, have you tried logging all the POST, GET, FILES variables?
What I do when I'm debugging and something doesn't want to work is:
<?php
$f = fopen("access.log", "a+");
$data = print_r($_REQUEST,1);
fwrite($f, $data);
fclose($f);
?>
Same with $_FILES. If there's an access log with the information, I know the page is being reached, everything is being sent but I'm handling it wrong.
If there's no file then, obviously, the page isn't getting reached.
Hope this helps
Mark
There is a file "/home/test.mp4" in my local machine,
I want to upload it into /var/www/ok.mp4 (the name changed when uploaded it). All the source file and target file are in the local machine.
How to fix my partial code ,to add something or to change something ?
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_exec($ch);
?>
Think to Ram Sharma, the code was changed as the following:
<?php
$request = curl_init('http://127.0.0.1/');
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
$request,
CURLOPT_POSTFIELDS,
array(
'file' => '#' . realpath('/home/test.mp4')
));
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);
// close the session
curl_close($request);
?>
An error message occur:
It works!
This is the default web page for this server.
The web server software is running but no content has been added, yet.
I have test with ftp_put,code1 works fine.
code1:
<?php
set_time_limit(0);
$host = 'xxxx';
$usr = 'yyyy';
$pwd = 'zzzz';
$src = 'd:/upload.sql';
$ftp_path = '/public_html/';
$des = 'upload_ftp_put.sql';
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
$upload = ftp_put($conn_id, $ftp_path.$des, $src, FTP_ASCII);
print($upload);
?>
The file d:/upload.sql in my local pc can be uploaded into my_ftp_ip/public_html/upload_ftp_put.sql with code1.
Now i rewite it with curl into code2.
code2:
<?php
set_time_limit(0);
$ch = curl_init();
$host = 'xxxx';
$usr = 'yyyy';
$pwd = 'zzzz';
$src = 'd:/upload.sql';
$ftp_path = '/public_html';
$dest = 'upload_curl.sql';
$fp = fopen($src, 'r');
curl_setopt($ch, CURLOPT_URL, 'ftp://user:pwd#host/'.$ftp_path .'/'. $dest);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($src));
curl_exec ($ch);
$error_no = curl_errno($ch);
print($error_no);
curl_close ($ch);
?>
The error info output is 6 .Why can't upload my local file into the ftp with curl?How to fix it?
Use copy():
copy('/home/test.mp4', '/var/www/ok.mp4');
It does not make sense to run the file through the network stack (which is what cURL does), on any protocol (HTTP, FTP, …), when the manipulation can be done locally, through the file system. Using network is more complicated and error-prone.
It is a low level error.
curl_setopt($ch, CURLOPT_URL, "ftp://$usr:$pwd#$host$ftp_path/$dest");
try something like this and I feel instead of server directory path it would be http url.
// initialise the curl request
$request = curl_init('http://example.com/');
// send a file
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
$request,
CURLOPT_POSTFIELDS,
array(
'file' => '#' . realpath('test.txt')
));
// output the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);
// close the session
curl_close($request);
This code might help you:
<?php
$rCURL = curl_init();
curl_setopt($rCURL, CURLOPT_URL, 'http://www.google.com/images/srpr/logo11w.png');
curl_setopt($rCURL, CURLOPT_HEADER, 0);
curl_setopt($rCURL, CURLOPT_RETURNTRANSFER, 1);
$aData = curl_exec($rCURL);
curl_close($rCURL);
file_put_contents('bla.jpeg', $aData);
// file_put_contents('my_folder/bla.jpeg', $aData); /*You can use this too*/
Try to specify the MIME type of the file sent like this
curl_setopt(
$request,
CURLOPT_POSTFIELDS,
array(
'file' => '#' . realpath('/home/test.mp4') . ';type=video/mp4'
));
The code you posted is for the client side. If you want to upload a file using HTTP, you HTTP server must be able to handle this upload request and save the file where you want. The “error message” is probably the server’s default web page.
Sample server-side code in PHP, for your reference:
<?php
if ($_FILES) {
$filename = $_FILES['file']['name'];
$tmpname = $_FILES['file']['tmp_name'];
if (move_uploaded_file($tmpname,'/var/www/ok.mp4')) {
print_r('ok');
} else {
print_r('failure');
}
}
curl -X POST -F "image=#test.mp4" http://example.com/
You will also need a page that can process this request (POST)
I really, really need help as to how to solve this issue I'm having:
Using script:
<?php
$curl = curl_init();
$fp = fopen("somefile.zip", "w");
curl_setopt ($curl, CURLOPT_URL, "http://website.com/test.zip");
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_exec ($curl);
curl_close ($curl);
I have asked before, and no-one seems to have a solution as to how I solve this... If someone can even tell me why it's happening ie. is it file size, binary transfer etc. I can work with that!
The file ZIP file downloads and creates somefile.zip but the XML file within is partially corrupt.
Sample:
K#Teº22)dVTð¼ÜvØ
rÏ*HIê±dE*¬òPÜÊâR}ÝbJÉÂX:Î#z|Eª2Ér tk2UÄOK¼É,·,Ûs¦ê1Z°VÝk6Ù«ËGÝw©5Æ]ÛQcq¥¼½ØïÒÐ]êÈy¨ð¶Çùûü]ÛßþW¤ùâÝÀw|~§ïúÁ¸ÛHBq®*YtrÛÕiî$ /ñ¥n?è¶;_ò
É¡ä ç&ýOr óß)yÿ¤$+`~TÙAófHU ¢SÝvW¶¦xA5Å׶Ãrå<8^ÐË4w qz Ø«<Ñ"*ººÝ?èO^;ÃQûÉOÏÀ¾?ìw|Õ±¥©3w©Ýr£ ÃÊÀ ¿^Á^UÛLß_ôÜÎh4îÖWcíF^8¾ö÷ؼ¾¿`âX3Ûú^{ À<.Æ¡(±1f¢.¸®k/ìÝeÓçê'PAnÓõ¸K`TeQ÷b|'¥Ñ)1ÓãnsÞèàÎZ|ê*+kuw×cªëÇ:§$¤ã¸Î1ü±Úh6ÕÀQ¦©D4Âp4b{Èo¾
,4"R
Can you set CURLOPT_HEADER to 0 and try again?
Edit:
Or try this:
$url = 'http://website.com/test.zip';
$path = 'somefile.zip';
$ch = curl_init($url);
if($ch === false)
{
die('Failed to create curl handle');
}
$fp = fopen($path, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
$data = curl_exec($ch);
curl_close($ch);
fclose($fp);