Advertisement
Guest User

story insert

a guest
Jul 31st, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1.  
  2. <!-- document to insert an image into a story -->
  3.  
  4. <html>
  5. <head><title>File Insert</title></head>
  6. <body>
  7. <h3>Please Choose a File and click Submit</h3>
  8.  
  9. <form enctype="multipart/form-data" action=
  10. "home_page.php" method="get">
  11. <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
  12. <input name="userfile" type="file" />
  13. <input type="submit" value="Submit" />
  14. </form>
  15.  
  16. <?php
  17. session_start();
  18. $host="localhost"; // Host name
  19. $user="wustl_inst"; // Mysql username
  20. $pass="wustl_pass"; // Mysql password
  21. $db="newsite"; // Database name
  22. $tbl_name="accounts"; // Table name
  23.  
  24. // Connect to server and select databse.
  25. $mysqli = new mysqli($host, $user, $pass, $db);
  26. if($mysqli->connect_errno){
  27. printf("Connection Failed: %s\n", $mysqli->connect_error);
  28. exit;
  29. }
  30.  
  31. // check if a file was submitted
  32. if(!isset($_FILES['userfile']))
  33. {
  34. echo '<p>Please select a file</p>';
  35. }
  36. else
  37. {
  38. try {
  39. $msg= upload(); //this will upload your image
  40. echo $msg; //Message showing success or failure.
  41. }
  42. catch(Exception $e) {
  43. echo $e->getMessage();
  44. echo 'Sorry, could not upload file';
  45. }
  46. }
  47.  
  48. // the upload function
  49.  
  50. function upload() {
  51. // include "file_constants.php";
  52. $maxsize = 10000000; //set to approx 10 MB
  53.  
  54. //check associated error code
  55. if($_FILES['userfile']['error']==UPLOAD_ERR_OK) {
  56.  
  57. //check whether file is uploaded with HTTP POST
  58. if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {
  59.  
  60. //checks size of uploaded image on server side
  61. if( $_FILES['userfile']['size'] < $maxsize) {
  62.  
  63. //checks whether uploaded file is of image type
  64. //if(strpos(mime_content_type($_FILES['userfile']['tmp_name']),"image")===0) {
  65. $finfo = finfo_open(FILEINFO_MIME_TYPE);
  66. if(strpos(finfo_file($finfo, $_FILES['userfile']['tmp_name']),"image")===0) {
  67.  
  68. // prepare the image for insertion
  69. $imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
  70.  
  71. // put the image in the db...
  72. // database connection
  73. mysql_connect($host, $user, $pass) OR DIE (mysql_error());
  74.  
  75. // select the db
  76. mysql_select_db ($db) OR DIE ("Unable to select db".mysql_error());
  77.  
  78. // our sql query
  79. /* $sql = "INSERT INTO test_image
  80. (image, name)
  81. VALUES
  82. ('{$imgData}', '{$_FILES['userfile']['name']}');";
  83.  
  84. // insert the image
  85. mysql_query($sql) or die("Error in Query: " . mysql_error());
  86. $msg='<p>Image successfully saved in database with id ='. mysql_insert_id().' </p>';*/
  87. //my insert query
  88. $imageinsert = $mysqli->prepare("update stories set story_image = ? where story_title = ?");
  89. if(!$imageinsert) {
  90. printf("Query prep failed: %s\n", $mysqli->error);
  91. exit;
  92. }
  93. $imageinsert->bind_param('bs', $imgData, $_SESSION['mystorytitle']);
  94. $imageinsert->execute();
  95. $imageinsert->close();
  96.  
  97. }
  98. else
  99. $msg="<p>Uploaded file is not an image.</p>";
  100. }
  101. else {
  102. // if the file is not less than the maximum allowed, print an error
  103. $msg='<div>File exceeds the Maximum File limit</div>
  104. <div>Maximum File limit is '.$maxsize.' bytes</div>
  105. <div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size'].
  106. ' bytes</div><hr />';
  107. }
  108. }
  109. else
  110. $msg="File not uploaded successfully.";
  111.  
  112. }
  113. else {
  114. $msg= file_upload_error_message($_FILES['userfile']['error']);
  115. }
  116. return $msg;
  117. }
  118.  
  119. // Function to return error message based on error code
  120.  
  121. function file_upload_error_message($error_code) {
  122. switch ($error_code) {
  123. case UPLOAD_ERR_INI_SIZE:
  124. return 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
  125. case UPLOAD_ERR_FORM_SIZE:
  126. return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
  127. case UPLOAD_ERR_PARTIAL:
  128. return 'The uploaded file was only partially uploaded';
  129. case UPLOAD_ERR_NO_FILE:
  130. return 'No file was uploaded';
  131. case UPLOAD_ERR_NO_TMP_DIR:
  132. return 'Missing a temporary folder';
  133. case UPLOAD_ERR_CANT_WRITE:
  134. return 'Failed to write file to disk';
  135. case UPLOAD_ERR_EXTENSION:
  136. return 'File upload stopped by extension';
  137. default:
  138. return 'Unknown upload error';
  139. }
  140. }
  141. ?>
  142. </body>
  143. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement