curlコマンドでPUTメソッドを使用してファイルのアップロードを行うPHPサンプルスクリプトを紹介します。
尚、本記事は以下のURLを参考にしました。
http://www.php.net/manual/ja/features.file-upload.put-method.php
htmlinsert(): The given local file does not exist or is not readable.
$ lsb_release -d Description: Ubuntu 12.04.4 LTS
$ apache2 -v Server version: Apache/2.2.22 (Ubuntu) Server built: Jul 12 2013 13:37:10
$ php --version PHP 5.3.10-1ubuntu3.9 with Suhosin-Patch (cli) (built: Dec 12 2013 04:27:25) Copyright (c) 1997-2012 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
以下のサンプルスクリプトはcurlコマンドでPUTされた内容を/tmp/putdataファイルに出力するサンプルスクリプトです。
<?php if (getenv('REQUEST_METHOD') == 'PUT') { $putdata = fopen("php://input", "r"); $fp = fopen("/tmp/putdata", "w"); while ($data = fread($putdata, 1024)) fwrite($fp, $data); fclose($fp); fclose($putdata); } else { print "Not PUT Method."; } ?>
以下のようにcurlコマンドを使ってPUTしてみます。
$ curl http://localhost/put.php -X PUT -d "Hello" $ ls -l /tmp/putdata -rw-r--r-- 1 www-data www-data 5 3月 18 14:15 /tmp/putdata $ cat /tmp/putdata Hello
putdataファイルにはHelloが保存されていることが確認できます。
以上、PHPでPUTメソッドを扱うサンプルスクリプトでした。