Advertisement
Guest User

Untitled

a guest
Mar 9th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. <html>
  2. <head><title>File Insert</title></head>
  3. <body>
  4. <h3>Please Choose a File and click Submit</h3>
  5.  
  6. <form enctype="multipart/form-data" action=
  7. "<?php echo $_SERVER['server address']; ?>" method="post">
  8. <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
  9. <input name="userfile" type="file" />
  10. <input type="submit" value="Submit" />
  11. </form>
  12.  
  13. <?php
  14.  
  15. // check if a file was submitted
  16. if(!isset($_FILES['userfile']))
  17. {
  18. echo '<p>Please select a file</p>';
  19. }
  20. else
  21. {
  22. try {
  23. $msg= upload(); //this will upload your image
  24. echo $msg; //Message showing success or failure.
  25. }
  26. catch(Exception $e) {
  27. echo $e->getMessage();
  28. echo 'Sorry, could not upload file';
  29. }
  30. }
  31.  
  32. // the upload function
  33.  
  34. function upload() {
  35. $host="your_hostname";
  36. $user="your_databaseuser";
  37. $pass="your_database_password";
  38. $db="database_name_to_use";
  39. $maxsize = 10000000; //set to approx 10 MB
  40.  
  41. //check associated error code
  42. if($_FILES['userfile']['error']==UPLOAD_ERR_OK) {
  43.  
  44. //check whether file is uploaded with HTTP POST
  45. if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {
  46.  
  47. //checks size of uploaded image on server side
  48. if( $_FILES['userfile']['size'] < $maxsize) {
  49.  
  50. //checks whether uploaded file is of image type
  51. if(strpos(mime_content_type($_FILES['userfile']['tmp_name']),"image")===0) {
  52. // $finfo = finfo_open(FILEINFO_MIME_TYPE);
  53. // if(strpos(finfo_file($finfo, $_FILES['userfile']['tmp_name']),"image")===0) {
  54.  
  55. // prepare the image for insertion
  56. $imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
  57.  
  58. // put the image in the db...
  59. // database connection
  60. mysql_connect($host, $user, $pass) OR DIE (mysql_error());
  61.  
  62. // select the db
  63. mysql_select_db ($db) OR DIE ("Unable to select db".mysql_error());
  64.  
  65. // our sql query
  66. $sql = "INSERT INTO test_image
  67. (image, name)
  68. VALUES
  69. ('{$imgData}', '{$_FILES['userfile']['name']}');";
  70.  
  71. // insert the image
  72. mysql_query($sql) or die("Error in Query: " . mysql_error());
  73. $msg='<p>Image successfully saved in database with id ='. mysql_insert_id().' </p>';
  74. }
  75. else
  76. $msg="<p>Uploaded file is not an image.</p>";
  77. }
  78. else {
  79. // if the file is not less than the maximum allowed, print an error
  80. $msg='<div>File exceeds the Maximum File limit</div>
  81. <div>Maximum File limit is '.$maxsize.' bytes</div>
  82. <div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size'].
  83. ' bytes</div><hr />';
  84. }
  85. }
  86. else
  87. $msg="File not uploaded successfully.";
  88.  
  89. }
  90. else {
  91. $msg= file_upload_error_message($_FILES['userfile']['error']);
  92. }
  93. return $msg;
  94. }
  95.  
  96. // Function to return error message based on error code
  97.  
  98. function file_upload_error_message($error_code) {
  99. switch ($error_code) {
  100. case UPLOAD_ERR_INI_SIZE:
  101. return 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
  102. case UPLOAD_ERR_FORM_SIZE:
  103. return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
  104. case UPLOAD_ERR_PARTIAL:
  105. return 'The uploaded file was only partially uploaded';
  106. case UPLOAD_ERR_NO_FILE:
  107. return 'No file was uploaded';
  108. case UPLOAD_ERR_NO_TMP_DIR:
  109. return 'Missing a temporary folder';
  110. case UPLOAD_ERR_CANT_WRITE:
  111. return 'Failed to write file to disk';
  112. case UPLOAD_ERR_EXTENSION:
  113. return 'File upload stopped by extension';
  114. default:
  115. return 'Unknown upload error';
  116. }
  117. }
  118. ?>
  119. </body>
  120. </html>
  121.  
  122. var file = YOUR_FILE,
  123. fileType = file.type,
  124. reader = new FileReader();
  125.  
  126. reader.onloadend = function() {
  127. var image = new Image();
  128. image.src = reader.result;
  129.  
  130. image.onload = function() {
  131.  
  132. imageWidth = image.width,
  133. imageHeight = image.height;
  134.  
  135. //calculate new width and height however you want
  136. // var newWidth, newHeight;
  137. // ...
  138.  
  139. var canvas = document.createElement('canvas');
  140. canvas.width = newWidth;
  141. canvas.height = newHeight;
  142.  
  143. var ctx = canvas.getContext("2d");
  144. ctx.drawImage(this, 0, 0, newWidth, newHeight);
  145.  
  146. // get the resized file
  147. var finalFile = canvas.toDataURL(fileType);
  148. }
  149. }
  150.  
  151. reader.readAsDataURL(file);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement