Resumable upload with CURL in owncloud - php

I am uploading my files to owncloud using curl request. If connection interrupts how can resume the upload? because i have to upload large files. Below is my code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://server/owncloud/remote.php/webdav/' . basename($filename));
curl_setopt($ch, CURLOPT_NOPROGRESS, false );
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_USERPWD, "user:password");
curl_setopt($ch, CURLOPT_PUT, 1);
$fh_res = fopen($file_path_str, 'r');
curl_setopt($ch, CURLOPT_INFILE, $fh_res);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path_str));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); // --data-binary
$curl_response_res = curl_exec ($ch);
How to continue the upload if the connection interrupts?

Related

Downloading a file from a webDAV site with PHP Curl

I need to download a file from a webDAV site using PHP. I have tried the code below using cURL but I get a "400 Bad Request" error.
Can someone help me with what I am doing wrong?
$fp = fopen("c:/downloadtest.text", "w");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result = curl_exec ($ch);
curl_close ($ch);
fclose($fp);
I finally got it working, I was not sure how to make the CURLOPT_WRITEFUNCTION option work:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$fp = fopen("c:/downloadtest.text", "w");
fwrite($fp, $output);
fclose($fp);
curl_close($ch);

Upload the file to the FTP server over HTTPS using curl in php

I am trying to upload the data [can be text or zip] to the ftp site.
As we have proxy in place in the environment so, I decided to upload the data using curl. Before I go head to set proxy server setting, I was testing the script on the environment without proxy.
I followed :-
FTP upload file to distant server with CURL and PHP uploads a blank file
However, I couldn't able to upload a file.
link where, I would like to upload is this format:-
https://fp.emc.com/.....
Do any one know, how to upload a file to ftp server over https using curl function of PHP?
<?php
$sendTo = 'https://ftp.emc.com/....?domain=XX&user=XXX&password=XXX';
$localfile ="23.txt";
$fp = fopen($localfile, 'r');
// Create CURL Connection
$ch = curl_init();
//curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "usr:passwd");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $sendTo);
//curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
echo $ch;
$m=curl_exec ($ch);
echo "m $m<br>";
$error_no = curl_errno($ch);
echo "error_no $error_no<br>";
curl_close ($ch);
if ($error_no == 0) {
$error = 'File uploaded succesfully.';
} else {
$error = 'File upload error.';
}
echo $error;
?>
Try this:
$fp = fopen($filepath, 'r');
$ftp_url = "ftp://user:password#ftpserver:21/" . $filename;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ftp_url);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filepath));
curl_setopt($ch, CURLOPT_PROXY, $proxy_server_ip);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_server_port);
curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy_login);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
curl_close($ch);
It works for me.

post file to third party API using curl

I am using below code to post a file to third party API -
$post = array('userName' => 'testabc','password'=>'testabc','FILE1'=>'abc.csv','cn'=>'10215');
$fp = fopen("orders/abc.csv", 'r');
$ch = curl_init("https://differentdomain.com/abc.cgi");
curl_setopt($ch, CURLOPT_USERPWD, "myuser:mypwd");
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 86400);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'CURL_callback');
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize("orders/abc.csv"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$suc = curl_exec ($ch);
echo "==>".curl_error($ch);
echo "-->".$suc; die;
It returns with 500 Internal Server Error. Not sure whether the way I am posting parameters are right or wrong.
Any help appreciated.
Thanks.
Upload image using curl
Try this

php curl file upload without real file , want to write to body a string

Hi i am uploading files by this little code
<?php
$file="oks.html";
$cookie="*;";
$csrf="*";
$parent="*";
function up($d)
{
global $cookie,$csrf,$parent;
$ch = curl_init("https://files.one.ubuntu.com/upload/");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:8888");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
"csrfmiddlewaretoken"=>$csrf,
"file"=>"#".$d.";filename=r2fas1.html;type=text/html",
"base"=>"/files/",
"path"=>"/",
"parent_key"=>$parent,
"redirct"=>"False",
"public"=>"on",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
return $response;
}
echo up($file);
but i want to send different file content is there a way to send string than real file ?

PHP cURL file upload append infomation to the file

I upload file to the remote server. The code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ZDURL.$url);
curl_setopt($ch, CURLOPT_USERPWD, ZDUSER."/token:".ZDAPIKEY);
$params = array('file_name' => '#'.$temp_file);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/plain"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
But in the top of uploaded file appears information:
------------------------------cfbe90a606af
Content-Disposition: form-data; name="file_name"; filename="C:\Program Files\xampp\tmp\phpF576.tmp"
Content-Type: application/octet-stream
images have wrong format owing to this addition text
It sounds like the URL you are sending data to is expecting just a file in the request body, rather than a form submission. The best way to achieve this is with CURLOPT_INFILE. Try this code:
Try this
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ZDURL.$url);
curl_setopt($ch, CURLOPT_USERPWD, ZDUSER."/token:".ZDAPIKEY);
// You need to detect the correct content type for the file you are sending.
// Although based on the current problem you have, it looks like the server
// ignores this anyway
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: image/jpeg"));
// Send the raw file in the body with no other data
$fp = fopen($temp_file, 'r');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
fclose($fp);

Categories