<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >PUT method support</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Manual PHP" HREF="index.html"><LINK REL="UP" TITLE="Handling file uploads" HREF="features.file-upload.html"><LINK REL="PREVIOUS" TITLE="Uploading multiple files" HREF="features.file-upload.multiple.html"><LINK REL="NEXT" TITLE="Using remote files" HREF="features.remote-files.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-2"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Manual PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="features.file-upload.multiple.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Cap. 20. Handling file uploads</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="features.remote-files.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="features.file-upload.put-method" >PUT method support</A ></H1 ><P > 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. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN4357" ></A ><P ><B >Exemplu 20-4. Saving HTTP PUT files with PHP 4</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">/* PUT data comes in on the stdin stream */<br /></font><font color="#0000BB">$putdata </font><font color="#007700">= </font><font color="#0000BB">fopen</font><font color="#007700">(</font><font color="#DD0000">"php://stdin"</font><font color="#007700">, </font><font color="#DD0000">"r"</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Open a file for writing */<br /></font><font color="#0000BB">$fp </font><font color="#007700">= </font><font color="#0000BB">fopen</font><font color="#007700">(</font><font color="#DD0000">"myputfile.ext"</font><font color="#007700">, </font><font color="#DD0000">"w"</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Read the data 1 KB at a time<br /> and write to the file */<br /></font><font color="#007700">while (</font><font color="#0000BB">$data </font><font color="#007700">= </font><font color="#0000BB">fread</font><font color="#007700">(</font><font color="#0000BB">$putdata</font><font color="#007700">, </font><font color="#0000BB">1024</font><font color="#007700">))<br /> </font><font color="#0000BB">fwrite</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#0000BB">$data</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Close the streams */<br /></font><font color="#0000BB">fclose</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">);<br /></font><font color="#0000BB">fclose</font><font color="#007700">(</font><font color="#0000BB">$putdata</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B > All documentation below applies to PHP 3 only. </P ></BLOCKQUOTE ></DIV ><P > PHP provides support for the HTTP PUT method used by clients such as <SPAN CLASS="productname" >Netscape Composer</SPAN > and W3C <SPAN CLASS="productname" >Amaya</SPAN >. PUT requests are much simpler than a file upload and they look something like this: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4365" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >PUT /path/filename.html HTTP/1.1</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > This would normally mean that the remote client would like to save the content that follows as: <TT CLASS="filename" >/path/filename.html</TT > 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 <SPAN CLASS="emphasis" ><I CLASS="emphasis" >Script</I ></SPAN > 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: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4370" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >Script PUT /put.php</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > 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. </P ><P > Inside your put.php file you would then do something like this: </P ><P > <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4375" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php copy</font><font color="#007700">(</font><font color="#0000BB">$PHP_UPLOADED_FILE_NAME</font><font color="#007700">, </font><font color="#0000BB">$DOCUMENT_ROOT </font><font color="#007700">. </font><font color="#0000BB">$REQUEST_URI</font><font color="#007700">); </font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > 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 <A HREF="features.file-upload.html#features.file-upload.post-method" >POST-method</A >. 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 <VAR CLASS="varname" >$PHP_PUT_FILENAME</VAR > variable, and you can see the suggested destination filename in the <VAR CLASS="varname" >$REQUEST_URI</VAR > (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. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="features.file-upload.multiple.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Acasã</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="features.remote-files.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Uploading multiple files</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="features.file-upload.html" ACCESSKEY="U" >Sus</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Using remote files</TD ></TR ></TABLE ></DIV ></BODY ></HTML >