Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. <?php
  2. $folder = "upload/";
  3. if (is_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'])) {
  4. if (move_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'], $folder.$HTTP_POST_FILES['filename']['name'])) {
  5. echo "File uploaded";
  6. } else {
  7. echo "File not moved to destination folder. Check permissions";
  8. };
  9. } else {s
  10. echo "File is not uploaded";
  11. };
  12. ?>
  13.  
  14. <?php
  15.  
  16. $target_dir = "upload/";
  17. $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  18. $uploadOk = 1;
  19. $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
  20.  
  21. // Check if image file is a actual image or fake image
  22. if(isset($_POST["submit"])) {
  23.  
  24. if ($target_file == "upload/") {
  25. $msg = "cannot be empty";
  26. $uploadOk = 0;
  27. }
  28.  
  29. // Check if file already exists
  30. else if (file_exists($target_file)) {
  31. $msg = "Sorry, file already exists.";
  32. $uploadOk = 0;
  33. }
  34.  
  35. // Check file size
  36. else if ($_FILES["fileToUpload"]["size"] > 5000000) {
  37. $msg = "Sorry, your file is too large.";
  38. $uploadOk = 0;
  39. }
  40.  
  41. // Check if $uploadOk is set to 0 by an error
  42. else if ($uploadOk == 0) {
  43. $msg = "Sorry, your file was not uploaded.";
  44.  
  45. // if everything is ok, try to upload file
  46. } else {
  47.  
  48. if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
  49. $msg = "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
  50. }
  51.  
  52. }
  53.  
  54. }
  55.  
  56. ?>
  57.  
  58. <form action="upload.php" method="post" id="myForm" enctype="multipart/form-data">
  59. Select file to upload:
  60. <input type="file" name="fileToUpload" id="fileToUpload">
  61. <button name="submit" class="btn btn-primary" type="submit" value="submit">Upload File</button>
  62. </form>
  63.  
  64. public static function uploadFile($filepath="upload",$existCheck=0,$uniq=0){
  65. global $_FILES;
  66. try {
  67. // Undefined | Multiple Files | $_FILES Corruption Attack
  68. // If this request falls under any of them, treat it invalid.
  69. if (
  70. !isset($_FILES['uploaded_file']['error']) ||
  71. is_array($_FILES['uploaded_file']['error'])
  72. ) {
  73. $result["status"]="fail";$result["errors"]=('Invalid parameters.');return $result;
  74. }
  75.  
  76.  
  77. // Check $_FILES['uploaded_file']['error'] value.
  78. switch ($_FILES['uploaded_file']['error']) {
  79. case UPLOAD_ERR_OK:
  80. break;
  81. case UPLOAD_ERR_NO_FILE:
  82. $result["status"]="fail";$result["errors"]=('No file sent.');return $result;
  83. case UPLOAD_ERR_INI_SIZE:
  84. case UPLOAD_ERR_FORM_SIZE:
  85. $result["status"]="fail";$result["errors"]=('Exceeded filesize limit.');return $result;
  86. default:
  87. $result["status"]="fail";$result["errors"]=('Unknown errors.');return $result;
  88. }
  89.  
  90. // You should also check filesize here.
  91. if ($_FILES['uploaded_file']['size'] > 1000000) {
  92. $result["status"]="fail";$result["errors"]=('Exceeded filesize limit.');return $result;
  93. }
  94.  
  95. // DO NOT TRUST $_FILES['uploaded_file']['mime'] VALUE !!
  96. // Check MIME Type by yourself.
  97. $finfo = new finfo(FILEINFO_MIME_TYPE);
  98. if (false === $ext = array_search(
  99. $finfo->file($_FILES['uploaded_file']['tmp_name']),
  100. array(
  101. 'jpg' => 'image/jpeg',
  102. 'png' => 'image/png',
  103. 'gif' => 'image/gif',
  104. ),
  105. true
  106. )) {
  107. $result["status"]="fail";$result["errors"]=('Invalid file format.');return $result;
  108. }
  109. if($uniq==0){
  110. $temp=$filepath;
  111. }
  112. else{
  113. $temp=$filepath."/".uniqid()."_".$_FILES['uploaded_file']['name'];
  114. }
  115.  
  116. if ($existCheck==1 && file_exists($temp)) {
  117. $result["status"]="fail";$result["errors"]=('Unknown errors.');return $result;
  118. }
  119. if(@copy($_FILES['uploaded_file']['tmp_name'], $temp)) {
  120. return $result["status"]="success";
  121. }
  122. $result["status"]="fail";$result["errors"]=('Unknown errors.');return $result;
  123.  
  124. } catch (Exception $e) {
  125.  
  126. $result["status"]="fail";$result["errors"]= $e->getMessage();return $result;
  127.  
  128. }
  129. }
  130.  
  131. first, your write html code like this don't forget to write enctype="multipart/form-data" and then crerte new file name is upload.php
  132.  
  133. <form action="upload.php" method="post" enctype="multipart/form-data">
  134. <input type="file" name="fileToUpload" id="fileToUpload">
  135. <input type="submit" value="Upload Image" name="submit">
  136. </form>
  137. $path = "form/";
  138. $target_file = $path.basename($_FILES["fileToUpload"]["name"]);
  139. $file=$_FILES['fileToUpload']['name'];
  140. $result = move_uploaded_file($_FILES['fileToUpload']['tmp_name'],$target_file.$file);
  141. if ($result) {
  142. echo "file successfully uploaded";
  143. }
  144. else {
  145. echo "please select your file";
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement