Skip to content

Instantly share code, notes, and snippets.

View simonbernard's full-sized avatar

Simon simonbernard

View GitHub Profile
@simonbernard
simonbernard / php-curl-download-and-revert-curlopt_file.php
Created February 14, 2014 14:45
It took me some time so I would like to share with you: How-to download a file with PHP's cURL and afterwards continue with other cURL requests which return their output as usual.
$fh = fopen('/your/target/file', 'w');
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_URL, 'http://your/download/url');
curl_exec($ch);
// revert it
curl_setopt($this->ch, CURLOPT_FILE, STDOUT);
fclose($fh); // don't forget to close the handle
@simonbernard
simonbernard / php-curl-upload.php
Last active January 28, 2016 19:39
Ever tried to upload a file via PHP's cURL? Here is how I did it. I hope it well help someone.
// client side upload
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data")); // i guess it's the default
curl_setopt($ch, CURLOPT_URL, 'http://url/to/upload');
curl_setopt($ch, CURLOPT_POST, true);
$file = curl_file_create('/path/to/your/file.zip');
$post = array('anyParameterYouNeed' => 'anyValue', 'fileArrayIndexOnServer' => $file);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postData);
curl_exec($ch);
// and on the server side it looks like this
@simonbernard
simonbernard / angularjs-contenteditable.js
Last active August 29, 2015 13:56
I spent some reasonable time finding out, how to properly set-up a contenteditable directive in angularjs. What I wanted was a directive that preserves all new-line characters but I did not want any HTML code in the model. I ended up with below directive which replaces the tags with new-line characters. I used this directive on a contenteditable…
app.directive('contenteditable', function() {
return {
restrict: 'A',
require: '?ngModel',
link: function(scope, element, attrs, ngModel) {
if(!ngModel)
return;
// model -> view
ngModel.$render = function() {
element.html(ngModel.$viewValue);