Volkova

Upload File

Jun 6th, 2011
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.05 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. $user_name = "username";
  5. $password = "password";
  6. $database = "database";
  7. $server = "127.0.0.1";
  8.  
  9. $db_handle = mysql_connect($server, $user_name, $password);
  10. $db_found = mysql_select_db($database, $db_handle);
  11.  
  12. $title = $_POST["title"];
  13.  
  14.  
  15. if (isset($_COOKIE['username']) && isset($_COOKIE['password'])) {
  16.     $usersend = mysql_real_escape_string(htmlentities($_COOKIE['username']));
  17.     $passsend = mysql_real_escape_string(htmlentities($_COOKIE['password']));
  18.     $md5pass = $passsend;
  19.    
  20.     $SQL = "SELECT user_id FROM tb_users WHERE username='$usersend' and password='$md5pass'";
  21.     $result=mysql_query($SQL);
  22.     $count=mysql_num_rows($result);
  23.     while ($db_field = mysql_fetch_assoc($result)) {
  24.         $id = $db_field['user_id'];
  25.         $stamp = $db_field['timestamp'];
  26.     }
  27.     if($count==1) {
  28.         $time = time();
  29.         $SQL = "UPDATE tb_users SET timestamp=$time WHERE user_id=$id";
  30.         $result=mysql_query($SQL);
  31.         $SQL = "UPDATE tb_users SET online=1 WHERE user_id=$id";
  32.         $result=mysql_query($SQL);
  33.         // print "jfdijfksdjfkdf";
  34.     }
  35.    
  36. }
  37.  
  38. function make_thumb($img_name,$filename,$new_w,$new_h)
  39. {
  40. //get image extension.
  41. $ext=getExtension($img_name);
  42. //creates the new image using the appropriate function from gd library
  43. if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
  44. $src_img=imagecreatefromjpeg($img_name);
  45.  
  46. if(!strcmp("png",$ext))
  47. $src_img=imagecreatefrompng($img_name);
  48.  
  49. //gets the dimmensions of the image
  50. $old_x=imageSX($src_img);
  51. $old_y=imageSY($src_img);
  52.  
  53. $dst_img=ImageCreateTrueColor($new_w,$new_h);
  54. imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,$old_x,$old_y);
  55.  
  56. if(!strcmp("png",$ext))
  57. imagepng($dst_img,$filename);
  58. else
  59. imagejpeg($dst_img,$filename);
  60.  
  61. imagedestroy($dst_img);
  62. imagedestroy($src_img);
  63. }
  64.  
  65. function getExtension($str) {
  66. $i = strrpos($str,".");
  67. if (!$i) { return ""; }
  68. $l = strlen($str) - $i;
  69. $ext = substr($str,$i+1,$l);
  70. return $ext;
  71. }
  72.  
  73. $errors=0;
  74. if(isset($_POST['Submit']))
  75. {
  76.  
  77. $image=$_FILES['image']['name'];
  78. // if it is not empty
  79. if ($image)
  80. {
  81. // get the original name of the file from the clients machine
  82. $filename = stripslashes($_FILES['image']['name']);
  83.  
  84. // get the extension of the file in a lower case format
  85. $extension = getExtension($filename);
  86. $extension = strtolower($extension);
  87. // if it is not a known extension, we will suppose it is an error, print an error message and will not upload the file, otherwise we continue
  88. if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png"))
  89. {
  90. //echo '<h1>Unknown extension!</h1>';
  91. $errors=1;
  92. }
  93. else
  94. {
  95. // get the size of the image in bytes
  96. // $_FILES[\'image\'][\'tmp_name\'] is the temporary filename of the file in which the uploaded file was stored on the server
  97. $size=getimagesize($_FILES['image']['tmp_name']);
  98. $sizekb=filesize($_FILES['image']['tmp_name']);
  99.  
  100. }} }
  101.  
  102. // End of thumbnail function
  103.  
  104.  
  105. if (($_FILES["image"]["type"] == "image/gif")
  106.     || ($_FILES["image"]["type"] == "image/jpeg")
  107.     || ($_FILES["image"]["type"] == "image/pjpeg")
  108.     || ($_FILES["image"]["type"] == "image/png")) {
  109.     if ($_FILES["image"]["error"] > 0) {
  110.         //echo "Error: " . $_FILES["image"]["error"] . "<br />";
  111.     } else {
  112.         //echo "Title: " . $title . "<br />";
  113.         //echo "Upload: " . $_FILES["image"]["name"] . "<br />";
  114.         //echo "Type: " . $_FILES["image"]["type"] . "<br />";
  115.         //echo "Size: " . ($_FILES["image"]["size"] / 1024) . " Kb<br />";
  116.        
  117.         if (file_exists("uploads/" . $_FILES["image"]["name"])) {
  118.             //echo $_FILES["image"]["name"] . "already exists. ";
  119.             //md5_file($_FILES["image"]["tmp_name"])
  120.             continue;
  121.         } else {
  122.  
  123.                
  124.                 $endfilename = stripslashes($_FILES['image']['name']);
  125.                 $extension = getExtension($filename);
  126.                 $extension = strtolower($extension);
  127.                 $endfilename = time() . "." . $extension;
  128.                
  129.                 move_uploaded_file ($_FILES["image"]["tmp_name"],
  130.                 "uploads/" . $endfilename);
  131.                
  132.                 $filename = $endfilename;
  133.                 $srcth = $endfilename;
  134.                
  135.                 move_uploaded_file ($_FILES["image"]["tmp_name"],
  136.                 "uploads/" . $_FILES["image"]["name"]);
  137.                 //$filename = "uploads/" . $_FILES["image"]["name"];
  138.                 //$srcth = "uploads/thumbs/" . $_FILES["image"]["name"];
  139.            
  140.             //echo "Stored: " . $filename;
  141.            
  142.             //Make thumbnails and insert information into database
  143.             $one = "uploads/$filename";
  144.             $two = "uploads/thumbs/$filename";
  145.             make_thumb($one,$two,200,125);
  146.            
  147.             $title = mysql_real_escape_string($title);
  148.             $filename = mysql_real_escape_string($filename);
  149.             $tags = mysql_real_escape_string($tags);
  150.            
  151.            
  152.             if ($db_found) {
  153.  
  154.                
  155.                 $time = date("Y-n-j ~ H:i A");
  156.                
  157.                 $SQL = "INSERT INTO tb_images (img_id, img_name, filename, user) VALUES (NULL, '$title', '$filename', '$usersend')";
  158.                
  159.             $result = mysql_query($SQL);
  160.            
  161.             mysql_close($db_handle);
  162.             }
  163.             else {
  164.                 //print "Database Not Found ";
  165.                 mysql_close($db_handle);
  166.                 //header('Location: index.php');
  167.             }
  168.         }
  169.     }
  170. } else {
  171.     //echo "Something went wrong...";
  172.     $prev = $_SERVER['HTTP_REFERER'];
  173.     header("Location: $prev");
  174. }
  175.  
  176. $prev = $_SERVER['HTTP_REFERER'];
  177. header("Location: $prev");
  178. ?>
Advertisement
Add Comment
Please, Sign In to add comment