You want to upload a file.
Use the <input type='file'> of a form. You also need to include the attribute enctype="multipart/form-data" of the form to let the browser know to expect different types of form data. To limit the size of the file being uploaded, also use the hidden type.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
<head>
<title>File Upload</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
if (isset($_POST['submit'])) {
process_form();
}
else {
display_form();// display form for the first time
}
function display_form() {
echo <<<HTML
<h2>Feedback Form</h2>
<form action = "$_SERVER[SCRIPT_NAME]" method="post" enctype="multipart/form-data">
Filename:
<input type="file" name="file" size="40" />
<input type="hidden" name="max_size" value="30000" />
<br />
<input type="submit" name="submit" value="Upload the file" />
</form>
HTML;
}
function process_form() {
$file = "$_POST[file]";
// move the uploaded file
if (move_uploaded_file($_FILES[file][tmp_name], "data/{$_FILES[file][name]}")) {
echo "<p>Your file has been uploaded.</p>";
}
else {
// print a message based upon the error
switch ($_FILES[file][error]) {
case 1:
echo "The file exceed the upload_max_filesize setting in php.ini";
break;
case 2:
echo "The file exceeds the max_size setting in the form.";
break;
case 3:
echo "The file was only partially uploaded.";
break;
case 4:
echo "No file was uploaded.";
break;
}
}
echo "<p><a href=\"$_SERVER[SCRIPT_NAME]\">Try again?</a></p>\n";
echo "<p><a href=\"example_r.php\">
Do you want to read from the file?</a></p>\n";
}
?>
</p>
</body>
</html>
|