Upload a file on archive.org using php and curl

Archive.org provide you with a free to access online library of different kind of media, audio, movies, books, images …etc. You can upload your own file for free and make it available for everyone in different formats, for example uploading a mp3 audio file the servers will automatically convert it in ogg and vbr mp3 as well (check the faq for more info about derivatives) and afterward you will be able to download it with a direct link or with a torrent or even stream.

And here is a php code showing how to upload using CURL

//The file you want to upload and it should be inside the folder or add the path if it is somewhere else
$file = 'aa.png';
$file_read = fopen($file, 'r');

$headers = array(
'Authorization: LOW accesskey:secretkey',
'x-amz-auto-make-bucket:1',
'x-archive-meta01-collection:Images',
'x-archive-meta-title:Tariq File'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "http://s3.us.archive.org/myFolder/$file");

curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILE, $file_read);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_exec($ch);

curl_close($ch);

How to reload current page without losing any form data

The best way to keep form values after reloading the page is using Sysyphus.js javascript library.

It is easy to use:

First include the library in your pages:
<script src="js/sisyphus.min.js">

Then you need to call yourform.sisyphus(); function Suppose we have a form with id=”my-form”, here is an example showing how to use the library to keep form values:

<script src="js/sisyphus.min.js"></script>

<form action="" id="my-form" method="get">
  <input id="my-form-name-field" name="name" type="text" value="">
</form>

<script>
// Here we'll persist the form's data into localStorage on 
// every keystroke 
$( function() { 
  $( "#my-form" ).sisyphus(); 
  // or you can persist all forms data at one command 
  // $( "form" ).sisyphus(); 
 });
</script>

Note: to use it in drupal you have to include the library in hook_init() and use it inside your module or to include it and use it in your theme

Using SSH and SFTP with PHP

PHP’s SSH2 extension, a wrapper for libssh2 which implements the SSH2 protocol, provides several functions you can use to securely transfer files.

To begin leveraging these functions, it’s obvious the SSH2 package needs to be installed. As it’s a PECL extension, the installation process will depend based on your operating system of choice. Follow the guidelines on php.net.
yum install php-pecl-ssh2

Continue reading

cURL Requests with PHP

Introduction

cURL allows transfer of data across a wide variety of protocols, and is a very powerful system. It’s widely used as a way to send data across websites. cURL is unrestricted in what it can do, from the basic HTTP request, to the more complex FTP upload or interaction with an authentication enclosed HTTPS site.

 

Basics

Before we can do anything with a cURL request, we need to first instantiate an instance of cURL – we can do this by calling the function curl_init();, which returns a cURL resource. This function takes one parameter which is the URL that you want to send the request to, however, in our case, we’ll hold off doing that for now and set it an alternatively way later.

Continue reading