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:
PUT /path/filename.html HTTP/1.1
This would normally mean that the remote client would like to save
the content that follows as: /path/filename.html 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 Script directive. It can be
placed almost anywhere in your Apache configuration file. A
common place is inside a <Directory> block or perhaps inside
a <Virtualhost> block. A line like this would do the trick:
This tells Apache to send all PUT requests for URIs that match the
context in which you put this line to the put.php script. This
assumes, of course, that you have PHP enabled for the .php
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.
With PHP 4 and following you would then do something like the following in
your put.php. This would copy the contents of the uploaded file to the
file myputfile.ext on the server.
You would probably want to perform some checks and/or
authenticate the user before performing this file copy.
Example 38_5. Saving HTTP PUT files with PHP 4
/* PUT data comes in on the stdin stream */
$putdata = fopen("php://input", "r");
/* Open a file for writing */
$fp = fopen("myputfile.ext", "w");
/* Read the data 1 KB at a time
and write to the file */
while ($data = fread($putdata, 1024))
/* Close the streams */
All documentation below applies to PHP 3 only.
Example 38_6. Saving HTTP PUT files with PHP 3
<?php copy($PHP_UPLOADED_FILE_NAME, $DOCUMENT_ROOT . $REQUEST_URI); ?>
trick here is that when PHP sees a PUT_method request it stores
the uploaded file in a temporary file just like those handled by
When the request ends, this temporary file is deleted. So, your
PUT handling PHP script has to copy that file somewhere. The
filename of this temporary file is in the $PHP_PUT_FILENAME
variable, and you can see the suggested destination filename in
the $REQUEST_URI (may vary on non_Apache web servers). This
destination filename is the one that the remote client specified.
You do not have to listen to this client. You could, for example,
copy all uploaded files to a special uploads directory.