Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

PUT Method Requests


The PUT method is an HTTP request for a file to be placed on the remote server. It's like
a file upload that doesn't come from a form and tells you where to place the file in your
document tree. You might guess that this is a very dangerous thing to allow for
anonymous users. It's especially dangerous when users could be uploading PHP scripts.


Not all browsers support PUT requests, and neither do all servers. Netscape Composer
and the W3C's Amaya browsers reportedly allow PUT requests. Apache for UNIX will
accept them if configured to do so. To configure Apache to allow PUT requests, you use
the Script directive inside a configuration file. See the Apache site for more information
<http://www.apache. org/docs/mod/mod_actions.html#script>. You could tell
Apache to run all PUT requests through a PHP script with a line like Script PUT
/handle_put.php in httpd.conf.


The PHP_UPLOADED_FILE_NAME variable will be set with the path to the uploaded file,
which will be in a temporary directory. Just as with file uploads using the POST method,
this file will be automatically deleted when your script ends if you don't move it. If you
need to know the requested URI, look in the REQUEST_URI variable.


Reading and Writing to Files


Communication with files follows the pattern of opening a stream to a file, reading from
or writing to it, and then closing the stream. When you open a stream, you get an integer
that refers to the open stream. Each time you want to read from or write to the file, you
use this stream identifier. Internally PHP uses this integer to refer to all the necessary
information for communicating with the file.


To open a file on the local file system, you use the fopen function. It takes a name of a
file and a string that defines the mode of communication. This may be r for read-only or
w for write-only, among other modes. It is also possible to specify an Internet address by
starting the file name with http:// or ftp:// and following it with a full path including
a host name. The file functions are fully defined in Chapter 8.


Two other functions create file streams. You may open a pipe with the popen function or
you may open a socket connection with the fsockopen function. If you have much
experience with UNIX, you will recognize pipes as temporary streams of data between
executing programs. A common Perl method for sending mail is to open a pipe to
sendmail, the program for sending mail across the Internet. Because PHP has so many
built-in functions, it is rarely necessary to open pipes, but it's nice to know it's an option.


You can open a file stream that communicates through TCP/IP with fsockopen. This
function takes a hostname and a port and attempts to establish a connection. It is
described in Chapter 8, along with the rest of the I/O functions.

Free download pdf