PageRenderTime 93ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/php/upload.php

https://bitbucket.org/isanneh/campus-pages
PHP | 190 lines | 142 code | 23 blank | 25 comment | 24 complexity | 3cfe543b1fabbc3812b230e67f7375ef MD5 | raw file
  1. <?php
  2. include ("connect.php");
  3. // This function makes usage of
  4. // $_GET, $_POST, etc... variables
  5. // completly safe in SQL queries
  6. function sql_safe($s)
  7. {
  8. if (get_magic_quotes_gpc())
  9. $s = stripslashes($s);
  10. return mysql_real_escape_string($s);
  11. }
  12. // If user pressed submit in one of the forms
  13. if ($_SERVER['REQUEST_METHOD'] == 'POST')
  14. {
  15. // cleaning title field
  16. $title = trim(sql_safe($_POST['title']));
  17. if ($title == '') // if title is not set
  18. $title = '(empty title)';// use (empty title) string
  19. if ($_POST['password'] != $password) // cheking passwors
  20. $msg = 'Error: wrong upload password';
  21. else
  22. {
  23. if (isset($_FILES['photo']))
  24. {
  25. @list(, , $imtype, ) = getimagesize($_FILES['photo']['tmp_name']);
  26. // Get image type.
  27. // We use @ to omit errors
  28. if ($imtype == 3) // cheking image type
  29. $ext="png"; // to use it later in HTTP headers
  30. elseif ($imtype == 2)
  31. $ext="jpeg";
  32. elseif ($imtype == 1)
  33. $ext="gif";
  34. else
  35. $msg = 'Error: unknown file format';
  36. if (!isset($msg)) // If there was no error
  37. {
  38. $data = file_get_contents($_FILES['photo']['tmp_name']);
  39. $data = mysql_real_escape_string($data);
  40. // Preparing data to be used in MySQL query
  41. mysql_query("INSERT INTO {$table}
  42. SET ext='$ext', title='$title',
  43. data='$data'");
  44. $msg = 'Success: image uploaded';
  45. }
  46. }
  47. elseif (isset($_GET['title'])) // isset(..title) needed
  48. $msg = 'Error: file not loaded';// to make sure we've using
  49. // upload form, not form
  50. // for deletion
  51. if (isset($_POST['del'])) // If used selected some photo to delete
  52. { // in 'uploaded images form';
  53. $id = intval($_POST['del']);
  54. mysql_query("DELETE FROM {$table} WHERE id=$id");
  55. $msg = 'Photo deleted';
  56. }
  57. }
  58. }
  59. elseif (isset($_GET['show']))
  60. {
  61. $id = intval($_GET['show']);
  62. $result = mysql_query("SELECT ext, UNIX_TIMESTAMP(image_time), data
  63. FROM {$table}
  64. WHERE id=$id LIMIT 1");
  65. if (mysql_num_rows($result) == 0)
  66. die('no image');
  67. list($ext, $image_time, $data) = mysql_fetch_row($result);
  68. $send_304 = false;
  69. if (php_sapi_name() == 'apache') {
  70. // if our web server is apache
  71. // we get check HTTP
  72. // If-Modified-Since header
  73. // and do not send image
  74. // if there is a cached version
  75. $ar = apache_request_headers();
  76. if (isset($ar['If-Modified-Since']) && // If-Modified-Since should exists
  77. ($ar['If-Modified-Since'] != '') && // not empty
  78. (strtotime($ar['If-Modified-Since']) >= $image_time)) // and grater than
  79. $send_304 = true; // image_time
  80. }
  81. if ($send_304)
  82. {
  83. // Sending 304 response to browser
  84. // "Browser, your cached version of image is OK
  85. // we're not sending anything new to you"
  86. header('Last-Modified: '.gmdate('D, d M Y H:i:s', $ts).' GMT', true, 304);
  87. exit(); // bye-bye
  88. }
  89. // outputing Last-Modified header
  90. header('Last-Modified: '.gmdate('D, d M Y H:i:s', $image_time).' GMT',
  91. true, 200);
  92. // Set expiration time +1 year
  93. // We do not have any photo re-uploading
  94. // so, browser may cache this photo for quite a long time
  95. header('Expires: '.gmdate('D, d M Y H:i:s', $image_time + 86400*365).' GMT',
  96. true, 200);
  97. // outputing HTTP headers
  98. header('Content-Length: '.strlen($data));
  99. header("Content-type: image/{$ext}");
  100. // outputing image
  101. echo $data;
  102. exit();
  103. }
  104. ?>
  105. <html><head>
  106. <title>MySQL Blob Image Gallery Example</title>
  107. </head>
  108. <body>
  109. <?php
  110. if (isset($msg)) // this is special section for
  111. // outputing message
  112. {
  113. ?>
  114. <p style="font-weight: bold;"><?=$msg?>
  115. <br>
  116. <a href="<?=$PHP_SELF?>">reload page</a>
  117. <!-- I've added reloading link, because
  118. refreshing POST queries is not good idea -->
  119. </p>
  120. <?php
  121. }
  122. ?>
  123. <h1>Blob image gallery</h1>
  124. <h2>Uploaded images:</h2>
  125. <form action="<?=$PHP_SELF?>" method="post">
  126. <!-- This form is used for image deletion -->
  127. <?php
  128. $result = mysql_query("SELECT id, image_time, title FROM {$table} ORDER BY id DESC");
  129. if (mysql_num_rows($result) == 0) // table is empty
  130. echo '<ul><li>No images loaded</li></ul>';
  131. else
  132. {
  133. echo '<ul>';
  134. while(list($id, $image_time, $title) = mysql_fetch_row($result))
  135. {
  136. // outputing list
  137. echo "<li><input type='radio' name='del' value='{$id}'>";
  138. echo "<a href='{$PHP_SELF}?show={$id}'>{$title}</a> &ndash; ";
  139. echo "<small>{$image_time}</small></li>";
  140. }
  141. echo '</ul>';
  142. echo '<label for="password">Password:</label><br>';
  143. echo '<input type="password" name="password" id="password"><br><br>';
  144. echo '<input type="submit" value="Delete selected">';
  145. }
  146. ?>
  147. </form>
  148. <h2>Upload new image:</h2>
  149. <form action="<?=$PHP_SELF?>" method="POST" enctype="multipart/form-data">
  150. <label for="title">Title:</label><br>
  151. <input type="text" name="title" id="title" size="64"><br><br>
  152. <label for="photo">Photo:</label><br>
  153. <input type="file" name="photo" id="photo"><br><br>
  154. <label for="password">Password:</label><br>
  155. <input type="password" name="password" id="password"><br><br>
  156. <input type="submit" value="upload">
  157. </form>
  158. </body>
  159. </html>