I write a script to upload files by php curl to oipenstack server. I got the token and the address to the upload directory.
When I use curl in CLI it works. But when I use my php script some code is added to the begining of the file so the file is changed. When the file is a video file it gets damaged and media players cannot play it anymore.
I have noticed the changes that are made by php curl. I compared the file before upload and after upload. The difference is some code added before the file content that I figured out in hex editor.
Let's watch them in screenshots (before / after):
So something about Content Disposition, name, filename are added there. I would like to fix my php curl script not to add any prefixes and change files at all. My php code is this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://abc-de.fghijklm.com/v1/AUTH_8a619275-f933-4da1-b289-0c06a1a2a3a4/bcd/drop93.avi");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
$headers = array();
$headers[] = "X-Auth-Token: AUTH_tkaacb23cace324039a3be9c81b1b2b3b4";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$curlFile = curl_file_create ('./drop.avi');
$post = array (
'file_contents' => $curlFile
);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
Related
So, I've got to download a file to do a verification, I must check if the downloaded file is an XML or an ZIP.
I have the following code:
$url = $return->DocXMLLink; //link to download the file
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
$response = curl_exec($ch);
//by here I may get the file extension and do something
Thanks in advance
I am trying to zip up a website and then upload to a CDN. But I am getting an error message when the zip is opened by the cdn: Error extracting zip: Zip end of central directory signature not found
The website is not complex, just some root level html files and an image folder. I created the zip file with PHP and when I open a downloaded copy on my computer it opens perfectly. And when I manually upload it to the cdn it extracts perfectly.
But when I upload it via PHP/cURL I get the error. So assume I am doing something wrong with my cURL functions. My apologies if this is a simplistic mistake, it is my first foray into cURL.
My upload code:
<?php
$file = "/path/to/mytest.zip";
$authorization = "Authorization: Bearer my_access_token";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://api.domain.com/bla/bla/bla");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, [$authorization, 'Content-Type: application/zip']);
$cfile = new CurlFile(#$file, 'application/zip');
$data = array('data-binary' => realpath($cfile));
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$curl_response = curl_exec($curl);
curl_close($curl);
?>
Any suggestions appreciated.
You should check this snippets on the RFC: https://wiki.php.net/rfc/curl-file-upload
The correct implementation of CurlFile is:
curl_setopt($curl_handle, CURLOPT_POST, 1);
$args['file'] = new CurlFile('filename.png', 'image/png');
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args);
I am trying to download a png image from Slack and save it into a file but the only thing that gets saved into the file is HTML code for the login page of Slack.
This is what I have tried.
$url = 'https://files.slack.com/files-pri/XXXXXX-XXXXXXX/download/2016-07-11.png';
$ch = curl_init($url);
$fp = fopen('/assets/tmp/1.png', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer xoxp-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXXXXXXX'
));
curl_exec($ch);
curl_close($ch);
fclose($fp);
If I access the link in browser while logged into Slack, the image downloads just fine, but not through the PHP code. Any help would be appreciated.
I needed an extra permission scope to read files. I added files:read permission scope to my application from Slack Developer Console and the code works as expected.
I have images that are hosted on a CDN, and I want to upload that image to some other system via their API. Normally I would just use curl and put an # character in front of the file to upload. Since this is a URL, that won't work since it expects the file to be local. I don't want to copy it local first since the files could be videos and that could be time consuming.
It seems there should be a way to do this with PHP curl. I'm open to using sockets instead, but I'm not sure how to simulate how curl does the submission.
<?php
$save_file_url = file_get_contents($your_files_CDN_file_URL);
$fp = fopen('test','w');
fwrite($fp,$save_file_url);
$save_file = realpath('test') ;
$url = "http://URL_TO_SEND_FILE/get_file.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
"file_is_this"=> "#".$save_file,
"app"=>'test'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
?>
And you will recieve file at get_file.php.
I am trying to upload a file to imageshack using following script:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://post.imageshack.us/');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
'fileupload'=> '#../../resources/images/cancel.png',
'optsize' => 'resample',
'rembar' => '1'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
However, I get following error from imageshack:
Wrong file type detected for file cancel.png:application/octet-stream.
Why is that, and how can it be fixed? Thanks in advance!
EDIT:
It does work when I use JPGs or GIFs (even animated ones, although Imageshack then de-animates them, I think by only using the first frame), but not with PNGs.
EDIT 2:
I found out how to fix it. As stated earlier, it does only accept JPGs and GIFs. So I take the location of the image I need to upload, copy it to a temporary location with the ending .jpg, upload the file, and then, upon completion, I remove the file from the temporary location.
Set the Content-type to the type of the file.
According to the manual, it must be an array or object. So here
$arr = array('Content-type: text/plain') ;
curl_setopt ( $ch , CURL_HTTPHEADER , $arr );