i am completely stuck with this scenario, issue 1: i want to send file from one server to other, for sending file i am using curl, the standard or more common code for sending file via curl `$url = $callthis; $filename = $_FILES['orig_file_name']['name']; $filedata = $_FILES['orig_file_name']['tmp_name']; $filesize = $_FILES['orig_file_name']['size']; if ($filedata != '') {
$headers = array("Content-Type:multipart/form-data"); // cURL headers for file uploading
$postfields = array("orig_file_name" => "@$filedata", "filename" => $filename, 'user_id' => $this->Session->read('userid'),
'artwork_type'=>$this->request->data['artwork_type'],
'description'=>$this->request->data['description'],
'itemstable_id'=>$this->request->data['itemstable_id'],
'upload_date'=>$this->request->data['upload_date'],
'brand_Approved'=>'New');
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_INFILESIZE => $filesize,
CURLOPT_RETURNTRANSFER => true
); // cURL options
curl_setopt_array($ch, $options);
curl_exec($ch);
if(!curl_errno($ch))
{
$info = curl_getinfo($ch);
debug($info);
if ($info['http_code'] == 200)
return $this->redirect(array('action' => 'uploadedart'));
}
else
{
$errmsg = curl_error($ch);
$this->Session->setFlash(__($errmsg));
}
curl_close($ch);
}`
fyi, curl is installed on server and enabled this code works perfect on my local PC and i am expecting same on the linux system, but my code will be implemented on amazone IIS server, so when i upload the code and then i test it gives me 0 status code and when i google that they say that your URL is incorrect which in my case is correct so the issue is '@' sign placed in the curl request before the file name and in php curl documentation they say that @ sign is must in order to tell the receiver server that it is a physical file not plain text, and when i remove this @ sign i get 200 response code from the receiving end but the file didn't get saved in the desired directory.....
issue 2: i implemented another code from one of the guy in stackoverflow ` $url = $callthis; $filename = $_FILES['orig_file_name']['name']; $filedata = $_FILES['orig_file_name']['tmp_name']; $filesize = $_FILES['orig_file_name']['size']; //debug($this->request->data);exit(); if ($filedata != ''){
move_uploaded_file($filedata, APP."webroot".DS."upload".DS.$filename);
$newfile=APP."webroot".DS."upload".DS.$filename;
//exit;
/* begin stuff */
$postfields = array('user_id' => $this->Session->read('userid'),
'artwork_type'=>$this->request->data['artwork_type'],
'description'=>$this->request->data['description'],
'itemstable_id'=>$this->request->data['itemstable_id'],
'upload_date'=>$this->request->data['upload_date'],
'brand_Approved'=>'New');
$file_url = $newfile; //here is the file route, in this case is on same directory but you can set URL too like "http://examplewebsite.com/test.txt"
$eol = "
"; //default line-break for mime type
$BOUNDARY = md5(time()); //random boundaryid, is a separator for each param on my post curl function
$BODY=""; //init my curl body
$BODY.= '--'.$BOUNDARY. $eol; //start param header
$BODY .= 'Content-Disposition: form-data; name="otherfields"' . $eol . $eol; // last Content with 2 $eol, in this case is only 1 content.
$BODY .= serialize($postfields) . $eol;//param data in this case is a simple post data and 1 $eol for the end of the data
$BODY.= '--'.$BOUNDARY. $eol; // start 2nd param,
$BODY.= 'Content-Disposition: form-data; name="orig_file_name"; filename="'.$filename.'"'. $eol ; //first Content data for post file, remember you only put 1 when you are going to add more Contents, and 2 on the last, to close the Content Instance
$BODY.= 'Content-Type: application/octet-stream' . $eol; //Same before row
$BODY.= 'Content-Transfer-Encoding: base64' . $eol . $eol; // we put the last Content and 2 $eol,
$BODY.= chunk_split(base64_encode(file_get_contents($file_url))) . $eol; // we write the Base64 File Content and the $eol to finish the data,
$BODY.= '--'.$BOUNDARY .'--' . $eol. $eol; // we close the param and the post width "--" and 2 $eol at the end of our boundary header.
$ch = curl_init(); //init curl
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X_PARAM_TOKEN : 71e2cb8b-42b7-4bf0-b2e8-53fbd2f578f9' //custom header for my api validation you can get it from $_SERVER["HTTP_X_PARAM_TOKEN"] variable
,"Content-Type: multipart/form-data; boundary=".$BOUNDARY) //setting our mime type for make it work on $_FILE variable
);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/1.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0'); //setting our user agent
curl_setopt($ch, CURLOPT_URL, $url); //setting our api post url
//curl_setopt($ch, CURLOPT_COOKIEJAR, $BOUNDARY.'.txt'); //saving cookies just in case we want
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); // call return content
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); //navigate the endpoint
curl_setopt($ch, CURLOPT_POST, true); //set as post
curl_setopt($ch, CURLOPT_POSTFIELDS, $BODY); // set our $BODY
$response = curl_exec($ch); // start curl navigation
print_r($response); //print response
this works fine but only with text files because it does
chunk_split(base64_encode(file_get_contents($file_url)))` i mean read the file to be sent, which work fines in text files but in my case i want to send the images which in the issue 2 dont works because it modifies the image content and hence unable to open the image....
fyi, i cant do jquery/ajax file sending, i need to do it via php, so the answer should be valid workable curl request code to send the file or cakephp httpsocket with sending file
Note that PHP >=5.6 disables curl's @file convention by default, so if you have different versions in play, this may be your issue.
To use your existing code with PHP >=5.6 you could add
CURLOPT_SAFE_UPLOAD => false
to your options array.
If this is your issue, see change log for more info: http://us3.php.net/manual/en/migration56.changed-functions.php
Otherwise, just to be clear: this should not be directly related to your web server (IIS vs. Apache) unless the server is using a different php configuration or there are permission attached to the user running your web server's service that are affecting your script.
So you are sure the php/curl library is running correctly in your environment?: Sometimes I just do a "hello word" test at the terminal command prompt, e.g.:
php -r "$ch = curl_init('http://www.google.com'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); die($output);"
This should just dump the google.com's code into your terminal. That works, right?
Do the same with your url ... especially if you are using SSL! You could have SSL issues such as peer verification, etc! Also note that if the above works with https://your.url.com, it may not guarantee that it works in your web app if the SSL library dependencies (libeay32.dll & ssleay32.dll in the case of IIS) may not be accessible.
A status code of 0 would seem to indicate something will pop up based on the above tests, but it could be giving up the ghost for some other reason:
For example, are you sure $filedata in your code is an accessible path? Maybe try prior to setting those post fields:
if(!is_file($filedata)) die('where is ' . $filedata . '?');
@filename has been deprecated in PHP >= 5.5.0:
Use new CurlFile instead, will not require any changes on the receiving end.