3.7. Example PHP File Upload Script
Below shows a PHP file upload script that is written to handle the form data submitted from the earlier XHTML MP document. The PHP script prints out the information obtained from the HTTP request and saves the uploaded file to the "/file_uploads" directory of the WAP server.
<?php
header('Content-type: application/vnd.wap.xhtml+xml'); ?>
<?php
echo '<?xml version="1.0"?' . '>'; ?>
<!DOCTYPE
html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN"
"http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>File
Upload Example</title>
</head>
<body>
<h1>Data
Received at the Server</h1>
<hr/>
<p>
<?php
foreach
($_POST as $key => $value){
?>
<b>Name-value
Pair Info:</b><br/>
Field name: <?php echo $key;
?><br/>
Field value: <?php echo $value;
?><br/><br/>
<?php
}
$optionalFileName
= $_POST['filename'];
if ($_FILES['myFile']['error'] ==
UPLOAD_ERR_OK){
$fileName
= $_FILES['myFile']['name'];
?>
<b>Uploaded File
Info:</b><br/>
Content type: <?php echo
$_FILES['myFile']['type']; ?><br/>
Field name:
myFile<br/>
File name: <?php echo $fileName; ?><br/>
File
size: <?php echo $_FILES['myFile']['size'];
?><br/><br/>
<?php
/* Save the uploaded
file if its size is greater than 0. */
if
($_FILES['myFile']['size'] > 0){
if ($optionalFileName ==
"")
$fileName =
basename($fileName);
else
$fileName =
$optionalFileName;
$dirName = '/file_uploads/';
if
(move_uploaded_file($_FILES['myFile']['tmp_name'], $dirName .
$fileName)){
?>
<b>The uploaded file has been
saved successfully.</b>
<?php
}
else{
?>
<b>An
error occurred when we tried to save the uploaded
file.</b>
<?php
}
}
}
?>
</p>
</body>
</html>
The following screenshots show what you will see on the Nokia 6230 cell phone:
|
The above PHP script is very straightforward. Most of the code has been covered before. Below shows some lines of code that you may be unfamiliar with.
The line:
<?php header('Content-type: application/vnd.wap.xhtml+xml'); ?>
is used to set the MIME type of the PHP document. "application/vnd.wap.xhtml+xml" is the MIME type of XHTML MP.
The line:
<?php echo '<?xml version="1.0"?' . '>'; ?>
is used to output the XML declaration "<?xml version="1.0"?>". We have to output it this way since "<?" and "?>" of the XML declaration are the opening and closing tags of the PHP language. If we include the XML declaration in a PHP document directly, like this:
<?php
header('Content-type: application/vnd.wap.xhtml+xml'); ?>
<?xml
version="1.0"?>
<!DOCTYPE html PUBLIC
"-//WAPFORUM//DTD XHTML Mobile 1.0//EN"
"http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
...
then a parse error will occur.
The line:
$fileName = basename($fileName);
is necessary in the example PHP script since some browsers provide the full path of the uploaded file in the HTTP request, which means $_FILES['myFile']['name'] may contain a path but not a file name. Hence, we need to use the PHP function basename() to extract the file name from the path. For example, both basename('/files/myFile.txt') and basename('myFile.txt') returns the string "myFile.txt".
As XHTML MP is compatible with HTML/XHTML, the resulting XHTML MP document generated by the PHP script can also be viewed on web browsers such as Microsoft Internet Explorer and Mozilla Firefox. The only thing you need to do is to remove the following line from the PHP script:
<?php header('Content-type: application/vnd.wap.xhtml+xml'); ?>
This is because unlike WAP 2.0 browsers on cell phones, Internet Explorer 6 and Mozilla Firefox 2.0 do not understand the MIME type of XHTML MP. Instead of displaying the XHTML MP document, they will pop up a dialog box asking you to select a program to open the document or save the document on disk.
The following screenshots show the result on Mozilla Firefox 2.0:
|
Previous Page | Page 7 of 11 | Next Page |