uploading files with curl and php

by Martin Monperrus
To upload files with PHP from the command-line, you can use curl as follows.

curl -v -F filepassword=@myfile.png http://www.domain.com/upload.php

The equivalent HTML form is:
<form method="post" enctype="multipart/form-data" action="upload.php"> <input type="file" name="filepassword"/> <input type="submit"/> </form>

PHP code

<?

// first security check, the field name
$v = $_FILES['filepassword'];

// second security check; we might allow only some files
if(ereg("^(foo.php|bar.png)$",$v['name'])) {

   rename($v['tmp_name'],$v['name']);
   chmod($v['name'],0755);
   echo $v['name']." uploaded!";

 }
?>
You may also protect upload with HTTP Basic or Digest authentication.
Tagged as: