1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4. <head>
5. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
6. <title>Upload a File</title>
7. </head>
8. <body>
9. <?php // Script 11.4 - upload_file.php
10. /* This script displays and handles an HTML form. This script takes a file upload and stores it on the server. */
11.
12. if (isset($_POST['submitted'])) { // Handle the form.
13.
14. // Try to move the uploaded file:
15. if (move_uploaded_file ($_FILES['thefile']['tmp_name'], "../uploads/{$_FILES['thefile']['name']}")) {
16.
17. print '<p>Your file has been uploaded.</p>';
18.
19. } else { // Problem!
20.
21. print '<p style="color: red;">Your file could not be uploaded because: ';
22.
23. // Print a message based upon the error:
24. switch ($_FILES['thefile']['error']) {
25. case 1:
26. print 'The file exceeds the upload_max_filesize setting in php.ini';
27. break;
28. case 2:
29. print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form';
30. break;
31. case 3:
32. print 'The file was only partially uploaded';
33. break;
34. case 4:
35. print 'No file was uploaded';
36. break;
37. case 6:
38. print 'The temporary folder does not exist.';
39. break;
40. default:
41. print 'Something unforeseen happened.';
42. break;
43. }
44.
45. print '.</p>'; // Complete the paragraph.
46.
47. } // End of move_uploaded_file() IF.
48.
49. } // End of submission IF.
50.
51. // Leave PHP and display the form:
52. ?>
53.
54. <form action="upload_file.php" enctype="multipart/form-data" method="post">
55. <p>Upload a file using this form:</p>
56. <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
57. <p><input type="file" name="thefile" /></p>
58. <p><input type="submit" name="submit" value="Upload This File" /></p>
59. <input type="hidden" name="submitted" value="true" />
60. </form>
61.
62. </body>
63. </html>