PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/pirozek-yaps/plugins/uploadify/files/upload-file.php

http://pirozek-yaps.googlecode.com/
PHP | 120 lines | 105 code | 14 blank | 1 comment | 22 complexity | aff3c35d8f67bbf8996a514e1d9d050a MD5 | raw file
  1. <?php
  2. include '../../../config.php';
  3. if($_GET['gallery_section'] > 0) //gallery mode
  4. {
  5. if(GALLERY_MODE == 'dir') //we can create dirs
  6. {
  7. $section = mysql_fetch_array(mysql_query("SELECT * FROM ".DB_PREFIX."yaps_gallery_sections WHERE id = ".$_GET['gallery_section']));
  8. $uploaddir = '../../../'.GALLERY_DIR.$section['dir'];
  9. if(!is_dir($uploaddir)) //cant find dir, lets try to create it :)
  10. {
  11. $res = mkdir($uploaddir,0777);
  12. }
  13. $ext = explode('.',basename($_FILES['Filedata']['name']));
  14. $ext[1] = strtolower($ext[1]);
  15. $name = time();
  16. $file_for_upload = $uploaddir . $name .'.'. $ext[1]; //random name
  17. $file = $section['dir'] . $name .'.'. $ext[1]; //random name
  18. $thumb = $section['dir'] . $name.'_thumb.'.$ext[1];
  19. }
  20. else //we cant create dirs
  21. {
  22. $section = mysql_fetch_array(mysql_query("SELECT * FROM ".DB_PREFIX."yaps_gallery_sections WHERE id = ".$_GET['gallery_section']));
  23. $ext = explode('.',basename($_FILES['Filedata']['name']));
  24. $ext[1] = strtolower($ext[1]);
  25. $name = substr($section['dir'],0,-1).'.'.time();
  26. $file_for_upload = '../../../' . GALLERY_DIR . $name .'.'. $ext[1]; //random name
  27. $file = GALLERY_DIR . $name .'.'. $ext[1]; //random name
  28. $thumb = GALLERY_DIR . $name .'_thumb.'. $ext[1];
  29. }
  30. }
  31. else //upload mode
  32. {
  33. $file_for_upload = '../../../..' . $_REQUEST['folder'] . basename($_FILES['Filedata']['name']);
  34. }
  35. $handler = fopen('log.txt','a');
  36. fwrite($handler,$file_for_upload."\n\n".print_r($_FILES,true));
  37. fclose($handler);
  38. $size=$_FILES['Filedata']['size'];
  39. if (move_uploaded_file($_FILES['Filedata']['tmp_name'], $file_for_upload))
  40. {
  41. echo "success";
  42. $handler = fopen('log.txt','a');
  43. fwrite($handler,'nahrano!');
  44. fclose($handler);
  45. if($_GET['gallery_section'] > 0) //gallery mode
  46. {
  47. createthumb($file_for_upload,'../../../'.$thumb,GALLERY_THUMB_WIDTH,GALLERY_THUMB_HEIGHT);
  48. createthumb($file_for_upload,'../../../'.$file,GALLERY_MAX_WIDTH,GALLERY_MAX_HEIGHT);
  49. //protection against multiple insertion
  50. $row = mysql_fetch_array(mysql_query("SELECT * FROM ".DB_PREFIX."yaps_gallery_images ORDER BY id DESC LIMIT 1"));
  51. if($row['file'] != $file && $row['thumb'] != $thumb)
  52. {
  53. mysql_query("INSERT INTO ".DB_PREFIX."yaps_gallery_images VALUES('0','".$file."','".$thumb."','','','".$_GET['gallery_section']."','0')");
  54. mysql_query("UPDATE ".DB_PREFIX."yaps_gallery_sections SET photo_count = photo_count + 1 WHERE id = ".$_GET['gallery_section']);
  55. }
  56. }
  57. else //upload mode
  58. {
  59. $check = mysql_query("SELECT * FROM ".DB_PREFIX."yaps_uploads WHERE name = '".addslashes($_FILES['Filedata']['name'])."'");
  60. if(mysql_num_rows($check) < 1)
  61. {
  62. $query = mysql_query("INSERT INTO ".DB_PREFIX."yaps_uploads VALUES('0','".addslashes($_FILES['Filedata']['name'])."',NOW())");
  63. }
  64. }
  65. }
  66. else
  67. {
  68. echo "error ".$_FILES['uploadfile']['error']." --- ".$_FILES['Filedata']['tmp_name']." %%% ".$file."($size)";
  69. $handler = fopen('log.txt','a');
  70. fwrite($handler,"\n\nerror ".$_FILES['Filedata']['error']." --- ".$_FILES['Filedata']['tmp_name']."---> ".$_FILES['Filedata']['name']." %%% ".$file."($size)");
  71. fclose($handler);
  72. }
  73. function createthumb($name,$filename,$new_w,$new_h)
  74. {
  75. $ext = substr($name, -5);
  76. if (preg_match("/jpg|jpeg/",$ext)){$src_img=imagecreatefromjpeg($name);}
  77. if (preg_match("/png/",$ext)){$src_img=imagecreatefrompng($name);}
  78. $old_x=imageSX($src_img);
  79. $old_y=imageSY($src_img);
  80. if ($old_x > $old_y)
  81. {
  82. $thumb_w=$new_w;
  83. $thumb_h=$old_y*($new_h/$old_x);
  84. }
  85. if ($old_x < $old_y)
  86. {
  87. $thumb_w=$old_x*($new_w/$old_y);
  88. $thumb_h=$new_h;
  89. }
  90. if ($old_x == $old_y)
  91. {
  92. $thumb_w=$new_w;
  93. $thumb_h=$new_h;
  94. }
  95. $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
  96. imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
  97. if (preg_match("/png/",$ext))
  98. {
  99. imagepng($dst_img,$filename);
  100. } else {
  101. imagejpeg($dst_img,$filename);
  102. }
  103. imagedestroy($dst_img);
  104. imagedestroy($src_img);
  105. }
  106. ?>