Advertisement
Guest User

Code

a guest
Apr 11th, 2013
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.21 KB | None | 0 0
  1. <?php
  2. if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
  3. //uploading successfully done
  4. } else {
  5. throw new UploadException($_FILES['file']['error']);
  6. }
  7.  
  8. //**********************************************************************************************
  9.  
  10.  
  11. echo "Please wait while we attempt to upload your file...<br><br>";
  12.  var_dump($_FILES);
  13. //**********************************************************************************************
  14.  
  15.  
  16. $target_path = APP_PATH."/uploads/";
  17.  
  18. $flag = 0; // Safety net, if this gets to 1 at any point in the process, we don't upload.
  19.  
  20. $filename = $_FILES['file']['name'];
  21. $filesize = $_FILES['file']['size'];
  22. $mimetype = $_FILES['file']['type'];
  23.  
  24. $filename = htmlentities($filename);
  25. $filesize = htmlentities($filesize);
  26. $mimetype = htmlentities($mimetype);
  27.  
  28. $target_path = $target_path . basename( $filename );
  29.  
  30. if($filename != ""){
  31.  
  32. echo "Beginning upload process for file named: ".$filename."<br>";
  33. echo "Filesize: ".$filesize."<br>";
  34. echo "Type: ".$mimetype."<br><br>";
  35.  
  36. }
  37.  
  38. //First generate a MD5 hash of what the new file name will be
  39. //Force a MP3 extention on the file we are uploading
  40.  
  41. $hashedfilename = md5($filename);
  42. $hashedfilename = $hashedfilename.".mp3";
  43.  
  44. //Check for empty file
  45. if($filename == ""){
  46. $error = "No File Exists!";
  47. $flag = $flag + 1;
  48.  
  49. }
  50.  
  51. //Now we check that the file doesn't already exist.
  52. $existname = "uploads/".$hashedfilename;
  53.  
  54. if(file_exists($existname)){
  55.  
  56. if($flag == 0){
  57. $error = "Your file already exists on the server!  
  58. Please choose another file to upload or rename the file on your
  59. computer and try uploading it again!";
  60. }
  61.  
  62. $flag = $flag + 1;
  63. }
  64.  
  65. //Whitelisted files - Only allow files with MP3 extention onto server...
  66.  
  67. $whitelist = array(".mp3");
  68. foreach ($whitelist as $ending) {
  69.  
  70. if(substr($filename, -(strlen($ending))) != $ending) {
  71.  $error = "The file type or extention you are trying to upload is not allowed!  
  72. You can only upload MP3 files to the server!";
  73. $flag++;
  74. }
  75. }
  76.  
  77.  
  78. //Now we check the filesize.  If it is too big or too small then we reject it
  79. //MP3 files should be at least 1MB and no more than 6.5 MB
  80.  
  81. if($filesize > 6920600){
  82. //File is too large
  83.  
  84. if($flag == 0){
  85. $error = "The file you are trying to upload is too large!  
  86. Your file can be up to 6.5 MB in size only.  
  87. Please upload a smaller MP3 file or encode your file with a lower bitrate.";
  88. }
  89.  
  90. $flag = $flag + 1;
  91. }
  92.  
  93. if($filesize < 1048600){
  94. //File is too small
  95.  
  96. if($flag == 0){
  97. $error = "The file you are trying to upload is too small!
  98. Your file has been marked as suspicious because our system has
  99. determined that it is too small to be a valid MP3 file.
  100. Valid MP3 files must be bigger than 1 MB and smaller than 6.5 MB.";
  101. }
  102.  
  103. $flag = $flag + 1;
  104.  
  105. }
  106.  
  107. //Check the mimetype of the file
  108. if($mimetype != "audio/x-mp3" and $mimetype != "audio/mpeg" and $mimetype != "audio/mp3"){
  109.  
  110. if($flag == 0){
  111. $error = "The file you are trying to upload does not contain expected data.
  112. Are you sure that the file is an MP3?";
  113. }
  114.  
  115. $flag = $flag + 1;
  116. }
  117.  
  118. //Check that the file really is an MP3 file by reading the first few characters of the file
  119. #$f = @fopen($_FILES['file']['tmp_name'],'r');
  120. #$s = @fread($f,3);
  121. #@fclose($f);
  122. #if($s != "ID3"){
  123. #
  124. #if($flag == 0){
  125. #$error = "The file you are attempting to upload does not appear to be a valid MP3 file.";
  126. #}
  127.  
  128. #$flag++;
  129. #}
  130.  
  131.  
  132.  
  133. //All checks are done, actually move the file...
  134.  
  135. if($flag == 0){
  136.  
  137. if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
  138.    
  139.  
  140.     //Change the filename to MD5 hash and FORCE a MP3 extention.
  141.  echo var_dump(@file_exists($target_path.$filename));
  142.     if(!file_exists($target_path.$filename)){
  143.  
  144.     //Rename the file to an MD5 version
  145.    
  146.     var_dump(rename($target_path.$filename, $target_path.$hashedfilename));
  147.  
  148.     echo "The file ".  basename( $filename ). "
  149.     has been uploaded.";
  150.    
  151.     }  
  152.     else{
  153.       echo "There was an error uploading the file, please try again!1". PHP_EOL;
  154.     }
  155.  
  156.  
  157. } else{
  158.     echo "There was an error uploading the file, please try again2!". PHP_EOL;
  159. }
  160.  
  161. }
  162. else {
  163. echo "File Upload of $filename Failed!<br>";
  164. if($error != ""){
  165. echo $error;
  166. }
  167. }
  168.  
  169.  
  170. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement