Guest User

Untitled

a guest
Feb 18th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. <?php
  2. /**
  3. * PHP Grid Component
  4. *
  5. * @author Abu Ghufran <gridphp@gmail.com> - http://www.phpgrid.org
  6. * @version 1.5.2
  7. * @license: see license.txt included in package
  8. */
  9.  
  10. // include db config
  11. include_once("../../config.php");
  12.  
  13. // include and create object
  14. include(PHPGRID_LIBPATH."inc/jqgrid_dist.php");
  15.  
  16. // Database config file to be passed in phpgrid constructor
  17. $db_conf = array(
  18. "type" => PHPGRID_DBTYPE,
  19. "server" => PHPGRID_DBHOST,
  20. "user" => PHPGRID_DBUSER,
  21. "password" => PHPGRID_DBPASS,
  22. "database" => PHPGRID_DBNAME
  23. );
  24.  
  25. $g = new jqgrid($db_conf);
  26.  
  27. // code to download blob field
  28. if (!empty($_GET["get_file"]))
  29. {
  30. $fid = intval($_GET["get_file"]);
  31. $r = $g->execute_query("SELECT fname,fcontents from filecontents where fid = $fid");
  32. $rs = $r->getrows();
  33.  
  34. header( 'Content-Type: applicaton/download');
  35. header( 'Content-Disposition: attachment;filename='.$rs[0]["fname"]);
  36.  
  37. echo $rs[0]["fcontents"];
  38. die;
  39. }
  40.  
  41. $grid["height"] = '250'; // by default sort grid by this field
  42. $grid["sortname"] = 'fid'; // by default sort grid by this field
  43. $grid["sortorder"] = "asc"; // ASC or DESC
  44. $grid["caption"] = "File Upload Blob"; // caption of grid
  45. $grid["autowidth"] = true; // expand grid to screen width
  46. $grid["multiselect"] = false; // allow you to multi-select through checkboxes
  47. $grid["form"]["position"] = "center"; // allow you to multi-select through checkboxes
  48.  
  49. $grid["add_options"]["bottominfo"] = "Only pdf, gif, jpg, txt, doc, bmp, png files are allowed!";
  50.  
  51. $g->set_options($grid);
  52.  
  53. $g->set_actions(array(
  54. "add"=>true, // allow/disallow add
  55. "edit"=>true, // allow/disallow edit
  56. "delete"=>true, // allow/disallow delete
  57. "rowactions"=>true, // show/hide row wise edit/del/save option
  58. "search" => "advance" // show single/multi field search condition (e.g. simple or advance)
  59. )
  60. );
  61.  
  62. // this db table will be used for add,edit,delete
  63. $g->table = "filecontents";
  64. // select query with FK_data as FK_id, e.g. clients.name as client_id
  65. $g->select_command = "SELECT * FROM filecontents";
  66.  
  67. $col = array();
  68. $col["title"] = "Id"; // caption of column
  69. $col["name"] = "fid";
  70. $col["width"] = "10";
  71. $cols[] = $col;
  72.  
  73. $col = array();
  74. $col["title"] = "Name";
  75. $col["name"] = "fname";
  76. $col["width"] = "100";
  77. $col["align"] = "left";
  78. $col["editable"] = true;
  79. $cols[] = $col;
  80.  
  81. // virtual file upload column in blob
  82. $col = array();
  83. $col["title"] = "File";
  84. $col["name"] = "fileupload";
  85. $col["width"] = "50";
  86. $col["editable"] = true; // this column is editable
  87. $col["edittype"] = "file"; // render as file
  88. $col["upload_dir"] = "temp"; // upload here
  89. $col["editrules"] = array("ifexist"=>"rename"); // "rename", "override" can also be set
  90. $col["show"] = array("list"=>true,"edit"=>true,"add"=>true); // only show in add/edit dialog
  91. $cols[] = $col;
  92.  
  93. // virtual column to display blob field
  94. $col = array();
  95. $col["title"] = "File";
  96. $col["name"] = "fileview";
  97. $col["width"] = "20";
  98. $col["editable"] = false;
  99. $col["default"] = "<a href='?get_file={fid}'>Download</a>";
  100. $cols[] = $col;
  101.  
  102. // pass the cooked columns to grid
  103. $g->set_columns($cols);
  104.  
  105. // use events if you need custom logic for upload
  106. $e["on_insert"] = array("add_file", null, false);
  107. $e["on_update"] = array("add_blob", null, false);
  108. $g->set_events($e);
  109.  
  110. // generate grid output, with unique grid name as 'list1'
  111. $out = $g->render("list1");
  112.  
  113. // callback for add
  114. function add_blob($data)
  115. {
  116. $upload_file_path = $data["params"]["fileupload"];
  117. unset($data["params"]["fileupload"]);
  118.  
  119. // if file is uploaded
  120. if ($upload_file_path)
  121. {
  122. $file_content = file_get_contents($upload_file_path);
  123.  
  124. // check if file has hello
  125. if (strpos($file_content, 'testing') === true)
  126. {
  127. phpgrid_error("Not allowed");
  128. }
  129.  
  130. // check if file ext allowed
  131. $ext = pathinfo(realpath($upload_file_path), PATHINFO_EXTENSION);
  132. if ($ext <> "pdf" && $ext <> "gif" && $ext <> "jpg" && $ext <> "txt" && $ext <> "doc" && $ext <> "bmp" && $ext <> "png")
  133. {
  134. unlink(realpath($upload_file_path));
  135. phpgrid_error("Only pdf, gif, jpg, txt, doc, bmp, png files are allowed!");
  136. }
  137.  
  138. $p = realpath($upload_file_path);
  139. $p = str_replace("\\","/",$p);
  140.  
  141. // insert in db as blob
  142. $g = new jqgrid();
  143. $g->execute_query("insert into filecontents (fname,fcontents) values ('{$data["params"]["fname"]}', LOAD_FILE('{$p}'))");
  144.  
  145. unlink($p);
  146. }
  147. }
  148.  
  149. ?>
  150. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  151. <html>
  152. <head>
  153. <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/themes/redmond/jquery-ui.custom.css"></link>
  154. <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/jqgrid/css/ui.jqgrid.css"></link>
  155.  
  156. <script src="../../lib/js/jquery.min.js" type="text/javascript"></script>
  157. <script src="../../lib/js/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
  158. <script src="../../lib/js/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script>
  159. <script src="../../lib/js/themes/jquery-ui.custom.min.js" type="text/javascript"></script>
  160. </head>
  161. <body>
  162. <div style="margin:10px">
  163. <?php echo $out?>
  164. </div>
  165. </body>
  166. </html>
Add Comment
Please, Sign In to add comment