file snippets

How to upload a file with PHP and lib_curl

Tagged php, file, upload, client, pear  Languages php

This is an example of how to upload a file from PHP to another server that may or may not be running PHP.

Note that using Pear's HTTP_Request package is probably easier.

  Var_Dump::displayInit(array('display_mode' => 'HTML4_Text'), array('mode' => 'normal','offset' => 4));

  $url = '';
  $file = '/tmp/file_to_upload';
  $post_data = array();

  $post_data['username'] = '';
  $post_data['password'] = '';
  $post_data['file'] = "@$file";

  $ch = curl_init();

  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
#    'Cookie: xxxx',
#    'Content-Type: xxxx'
  curl_setopt($ch, CURLOPT_POST, true);
  #curl_setopt($ch, CURLOPT_PROXY, '');

  curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

  $response  = curl_exec($ch);
  $info      = curl_getinfo($ch); 
  $http_code = $info['http_code'];
  $speed     = round($info['speed_upload'] / 1024);
  $size      = round($info['size_upload'] / 1024);

  if($http_code != 200)
    echo "Upload failed with HTTP code $http_code. Uploaded $size kilobytes with $speed kilobytes/s.<br/>";
    echo "<h1>Response</h1><pre>$response</pre><h1>Debug information</h1>";

    echo "Uploaded successful. Uploaded $size kilobytes with $speed kilobytes / s.<br/>";
    echo "<h1>Response</h1><pre>$response</pre>";

You can remove the Var_Dump code (Pear module), if you haven't installed it, or don't need it.

PHP file upload gotchas

Tagged gotcha, php, file, upload  Languages php

PHP file upload works in mysterious ways:

How to load a local file into a UIWebView

Tagged iphone, uiwebview, file  Languages objectivec

First you'll need to setup a UIWebView in for example Interface Builder. Then in the controller you add this incredible piece of code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"about" ofType:@"html" inDirectory:@"html"];
NSLog(@"%@", path);
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];

This beautiful piece of code will look for a file named about.html in a folder called html. Now go create that folder and the file with your favorite tools.

Next drag that folder under your application in Groups & Files in XCode. Now to make XCode happy you have to make sure you select Create Folder References for any added folders.

That's it. If you get this error it means the file couldn't be found, and that you didn't import the files correctly:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'

Logging to a file with Elixir

Tagged elixir, file, logging  Languages elixir


  def application do
      applications: [:logger, :logger_file_backend, ...],
      mod: {Snippets, []}

  def deps do
      {:cowboy, "~> 1.0.0"}, # web server
      {:logger_file_backend, "~> 0.0.7"} # writes log messages to a file, LOL


config :logger, :error_log,
  path: "log/app.log",
  level: :debug