<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>PUT method support</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="features.file-upload.multiple.html">Uploading multiple files</a></div> <div class="next" style="text-align: right; float: right;"><a href="features.remote-files.html">Using remote files</a></div> <div class="up"><a href="features.file-upload.html">Handling file uploads</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><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: <div class="informalexample"> <div class="example-contents"> <div class="HTTPcode"><pre class="HTTPcode">PUT /path/filename.html HTTP/1.1</pre> </div> </div> </div> </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 <em><Directory></em> block or perhaps inside a <em><VirtualHost></em> block. A line like this would do the trick: <div class="informalexample"> <div class="example-contents"> <div class="cdata"><pre> Script PUT /put.php </pre></div> </div> </div> </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"> <div class="example" id="example-360"> <p><strong>Example #1 Saving HTTP PUT files</strong></p> <div class="example-contents"> <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> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="features.file-upload.multiple.html">Uploading multiple files</a></div> <div class="next" style="text-align: right; float: right;"><a href="features.remote-files.html">Using remote files</a></div> <div class="up"><a href="features.file-upload.html">Handling file uploads</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>