Installing HTTP_Upload
To take advantage of HTTP_Upload??™s features, you need to install it from PEAR. The
process for doing so follows:
396 CHAPTER 15 ?– HANDL ING F ILE UPLOADS
%>pear install HTTP_Upload
downloading HTTP_Upload-0.9.1.tgz ...
Starting to download HTTP_Upload-0.9.1.tgz (9,460 bytes)
.....done: 9,460 bytes
install ok: channel://pear.php.net/HTTP_Upload-0.9.1
Uploading a File
Uploading a file with HTTP_Upload is simple. Just invoke the class constructor and
pass the name of the file-specific form field to the getFiles() method. If it uploads
correctly (verified using the isValid() method), you can then move the file to its final
destination (using the moveTo() method). A sample script is presented in Listing 15-2.
Listing 15-2. Using HTTP_Upload to Move an Uploaded File
require('HTTP/Upload.php');
// New HTTP_Upload object
$upload = new HTTP_Upload();
// Retrieve the classnotes file
$file = $upload->getFiles('classnotes');
// If no problems with uploaded file
if ($file->isValid()) {
$file->moveTo('/home/httpd/html/uploads');
echo "File successfully uploaded!";
}
else {
echo $file->errorMsg();
}
?>
You??™ll notice that the last line refers to a method named errorMsg(). The package
tracks a variety of potential errors, including matters pertinent to a nonexistent upload
directory, lack of write permissions, a copy failure, or a file surpassing the maximum
upload size limit.
Pages:
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478