Source for file upload_example.php

Documentation is available at upload_example.php

  1. <?php
  2. /**
  3. * upload_example.php
  4. *
  5. * Simple upload-script to demonstrate and test FlexUpload.
  6. * Feel free to use this to start developing your own scripts.
  7. *
  8. * Copyright (C) 2007 SPLINELAB, Mirko Schaal
  9. * http://www.splinelab.de/flexupload/
  10. *
  11. * All rights reserved
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the Free
  15. * Software Foundation; either version 2 of the License, or (at your option)
  16. * any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful, but WITHOUT
  19. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  20. * FITNESS FOR A PARTICULAR PURPOSE.
  21. * See the GNU General Public License for more details.
  22. *
  23. * The GNU General Public License can be found at
  24. * http://www.gnu.org/copyleft/gpl.html.
  25. * A copy is found in the textfile GPL and important notices to the license from
  26. * the author is found in the LICENSE file distributed with the program.
  27. *
  28. * This copyright notice MUST APPEAR in all copies of the program!
  29. *
  30. * ---------------------------------------------------------------------------
  31. *
  32. * Note:
  33. * Your upload script have to return some information for FlexUpload.
  34. *
  35. * If the upload was successfull just print "OK" (uppercase without quotation marks)
  36. * If something went wrong print a nice error message to inform the user what happened.
  37. *
  38. *
  39. * Important Note:
  40. *
  41. * In real-life your upload script have to be more secure than this one.
  42. * You have to make sure that only authorized users can upload files to your server!
  43. * You also should check the extension of the uploaded file to prevent bad guys to upload
  44. * malicious executable files (you should not accept files endig with .ph*, .cgi, .pl, ...)
  45. *
  46. *
  47. * Notes for migration from JavaUpload:
  48. *
  49. * The field name has changed to "Filedata" (this is the default in Flex) so you have to use
  50. * $_FILES['Filedata'] to access the uploaded file.
  51. *
  52. * The format of the return value also has changed. If you print something different than "OK"
  53. * FlexUpload assumes an error.
  54. * E.g. In JavaUpload you printed "success=1\r\n" if your upload succeeds and
  55. * "success=0\r\nSome error occurred"
  56. * Now you print "OK" if your upload succeeds and "Some error occured". This helps to avoid
  57. * parsing errors of the return value if your script raises some php warnings or errors.
  58. *
  59. *
  60. * @version 1.0
  61. * @author Mirko Schaal <ms@splinelab.com>
  62. * @package FlexUpload
  63. * @subpackage example
  64. */
  65.  
  66.  
  67. // just test GET parameters provided to the postURL parameter...
  68. //echo "myGETVariable: ".$_GET['myGETVariable'];
  69.  
  70.  
  71. // is Filedata there?
  72.  
  73. if (! isset($_FILES['Filedata'])) {
  74. echo "Whooops! There is no file! (maybe filesize is greater than POST_MAX_SIZE directive in php.ini)";
  75. exit;
  76. }
  77.  
  78. // make nicer filenames ;)
  79. $fn = preg_replace("/[^a-zA-Z0-9._-]/", "_", $_FILES['Filedata']['name']);
  80. // and set the directory
  81. $fn = 'uploaddir/'.$fn;
  82.  
  83.  
  84. // check if the file already exists
  85. if ( file_exists($fn) ) {
  86. echo "File exists - i don't like to overwrite it!";
  87. exit;
  88. }
  89.  
  90. // move the uploaded file
  91. if (is_uploaded_file($_FILES['Filedata']['tmp_name'])) {
  92. if (move_uploaded_file($_FILES['Filedata']['tmp_name'], $fn)) {
  93. @chmod($fn, 0666);
  94. echo 'OK';
  95. } else {
  96. echo 'can\'t move the uploaded file';
  97. }
  98. } else {
  99. switch($_FILES['Filedata']['error']) {
  100. case 0:
  101. echo 'possible file attack!';
  102. break;
  103. case 1:
  104. echo 'uploaded file exceeds the UPLOAD_MAX_FILESIZE directive in php.ini';
  105. break;
  106. case 2:
  107. echo 'uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form';
  108. break;
  109. case 3:
  110. echo 'uploaded file was only partially uploaded';
  111. break;
  112. case 4:
  113. echo 'no file was uploaded';
  114. break;
  115. default: //a default error, just in case! :)
  116. echo 'default error - that\'s magic!';
  117. break;
  118. }
  119. }
  120. ?>

Documentation generated on Mon, 18 Jun 2007 21:42:51 +0200 by phpDocumentor 1.3.0RC3