downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

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>&lt;Directory&gt;</i> block or perhaps inside
    a <i>&lt;VirtualHost&gt;</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">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;PUT&nbsp;data&nbsp;comes&nbsp;in&nbsp;on&nbsp;the&nbsp;stdin&nbsp;stream&nbsp;*/<br /></span><span style="color: #0000BB">$putdata&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"php://input"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Open&nbsp;a&nbsp;file&nbsp;for&nbsp;writing&nbsp;*/<br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"myputfile.ext"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"w"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Read&nbsp;the&nbsp;data&nbsp;1&nbsp;KB&nbsp;at&nbsp;a&nbsp;time<br />&nbsp;&nbsp;&nbsp;and&nbsp;write&nbsp;to&nbsp;the&nbsp;file&nbsp;*/<br /></span><span style="color: #007700">while&nbsp;(</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$putdata</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1024</span><span style="color: #007700">))<br />&nbsp;&nbsp;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Close&nbsp;the&nbsp;streams&nbsp;*/<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">?&gt;</span>
</span>
</code></div>
     </div>

    </div><p>
   </p>
  </div><?php manual_footer(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites