Guest User

Untitled

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