search for in the  
<Uploading multiple filesUsing remote files>
Last updated: Thu, 19 May 2005

PUT method support

PUT method support has changed between PHP 3 and PHP 4. In PHP 4, one should use the standard input stream to read the contents of an HTTP PUT.

Example 38-5. Saving HTTP PUT files with PHP 4

<?php
/* PUT data comes in on the stdin stream */
$putdata = fopen("php://stdin", "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))
 
fwrite($fp, $data);

/* Close the streams */
fclose($fp);
fclose($putdata);
?>

Note: All documentation below applies to PHP 3 only.

PHP provides support for the HTTP PUT method used by clients such as Netscape Composer and W3C Amaya. PUT requests are much simpler than a file upload 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:

Script PUT /put.php

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.

Inside your put.php file you would then do something like this:

<?php copy($PHP_UPLOADED_FILE_NAME, $DOCUMENT_ROOT . $REQUEST_URI); ?>

This would copy the file to the location requested by the remote client. You would probably want to perform some checks and/or authenticate the user before performing this file copy. The only 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 the POST-method. 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.



User Contributed Notes
PUT method support
MagicalTux at FF.ST
22-Nov-2004 10:39
After some researchs, it seems that put data will come in php://input when PHP is built as a module.

Since I don't use PHP as a CGI anywhere, I couldn't test if php://input with CGI but that's the only way to get the data when built as module.

Tested with PHP 5.0.2 / Apache 2.0.last
gomez_NOSPAM_pixline_dot_net
18-Apr-2004 03:59
Trying to capture a PUT stream into a single variable seems not to be allowed, probably because of the non presence of some kind of EOF. In this way save a PUT request into a database isn't easy.

The only way I find would be output to a cache file, then either insert filename into db or read again its content and place it in some kind of query.
Tommy
20-Jan-2004 07:32
[PHP: PUT method example with Apache]

According to the documentation the php://input represents the raw post data.  Yes, php://input works in php 4.3, php://stdin does not (as indicated in a previous post).  -- The fact that input behaves differently to stdin may be a bug; what's the difference between raw input data and standard input?

php://stdin appeared in php 4.3.  So the example given in the PHP documentation does not work in an earlier version of Apache/PHP.
The documentation about PHP3 does not apply to PHP4.3.

I have successfully tested the example (with php://input) on Mandrake 9.2 which shipps with PHP 4.3.3.
mikeb at mikebanahan dot com
03-Nov-2003 05:41
I have spent a lot of time trying to make PUT work with Apache 2.0.40. I have not yet been able to find any way of making the Script directive invoke php via mod_php, the only way has been to have a file called example.cgi and invoke it via CGI, with the file starting
#!/usr/bin/php
so the PHP interpreter is invoked through the CGI mechanism and not as a module.

If there IS a way of making it work 'right' I'd love to know! After six hours of messing around, I've settled for CGI. The error messages in the apache error log are significantly misleading and the whole thing has been an exercise in frustration.

Attempts to use AddHandler and all 'normal' ways of trying to persuade Apache to do this have been fruitless. It does seem as if PUT can only be handled by CGI invocation.
csnyder at chxo dot com
13-Feb-2003 01:58
On my server (Linux Apache + PHP 4.3) the PUT data does not come in on php://stdin as indicated above, but on php://input instead. Not sure if this is a bug or a feature.

Note that you need to use the same kind of Apache directive ("Script PUT /path/to/handler.php") for PHP 4, also. The statement that "all documentation below applies to PHP 3 only" is misplaced, and should come right before the PHP 3 code example.
php at dreier dot nu
13-Feb-2003 01:21
I can only make it work when I am using PHP as CGI, not as an Apache module.
I am using the version of PHP/Apahce that is shipped with Debian/testing.

You have to load the action_module, but not the put_module in Apache config.

<Uploading multiple filesUsing remote files>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 18:35:34 2005 EDT