Source of: /manual/en/features.file-upload.put-method.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/features.file-upload.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'features.file-upload.put-method.php',
1 => 'PUT method support',
),
'up' =>
array (
0 => 'features.file-upload.php',
1 => 'Handling file uploads',
),
'prev' =>
array (
0 => 'features.file-upload.multiple.php',
1 => 'Uploading multiple files',
),
'next' =>
array (
0 => 'features.remote-files.php',
1 => 'Using remote files',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="features.file-upload.put-method" class="sect1">
<h2 class="title">PUT method support</h2>
<p class="para">
PHP provides support for the HTTP PUT method used by some clients to store
files on a server.
PUT requests are much simpler than a file upload using POST requests
and they look something like this:
</p><div class="informalexample">
<div class="example-contents programlisting">
<div class="HTTPcode"><pre class="HTTPcode">PUT /path/filename.html HTTP/1.1</pre>
</div>
</div>
</div><p>
</p>
<p class="para">
This would normally mean that the remote client would like to save
the content that follows as: <var class="filename">/path/filename.html</var> in your web tree.
It is obviously not a good idea for Apache or PHP to automatically
let everybody overwrite any files in your web tree. So, to handle
such a request you have to first tell your web server that you
want a certain PHP script to handle the request. In Apache you do
this with the <em class="emphasis">Script</em> directive. It can be
placed almost anywhere in your Apache configuration file. A
common place is inside a <i><Directory></i> block or perhaps inside
a <i><VirtualHost></i> block. A line like this would do the trick:
</p><div class="informalexample">
<div class="example-contents programlisting">
<div class="cdata"><pre>
Script PUT /put.php
</pre></div>
</div>
</div><p>
</p>
<p class="simpara">
This tells Apache to send all PUT requests for URIs that match the
context in which you put this line to the <var class="filename">put.php</var> script. This
assumes, of course, that you have PHP enabled for the <var class="filename">.php</var>
extension and PHP is active. The destination resource for all PUT
requests to this script has to be the script itself, not a filename the
uploaded file should have.
</p>
<p class="simpara">
With PHP you would then do something like the following in
your put.php. This would copy the contents of the uploaded file to the
file <var class="filename">myputfile.ext</var> on the server.
You would probably want to perform some checks and/or
authenticate the user before performing this file copy.
</p>
<p class="para">
</p><div class="example">
<p><b>Example #1 Saving HTTP PUT files</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/* PUT data comes in on the stdin stream */<br /></span><span style="color: #0000BB">$putdata </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"php://input"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Open a file for writing */<br /></span><span style="color: #0000BB">$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"myputfile.ext"</span><span style="color: #007700">, </span><span style="color: #DD0000">"w"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Read the data 1 KB at a time<br /> and write to the file */<br /></span><span style="color: #007700">while (</span><span style="color: #0000BB">$data </span><span style="color: #007700">= </span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$putdata</span><span style="color: #007700">, </span><span style="color: #0000BB">1024</span><span style="color: #007700">))<br /> </span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Close the streams */<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$putdata</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
</div><?php manual_footer(); ?>