Advertisement
Guest User

Imagenes con PHP

a guest
Dec 7th, 2015
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. public function add(){
  2. if(Session::get("tipo") == "admin"){
  3. Session::set('mod', isset($_POST['txtmod']) ? $_POST['txtmod'] : Session::get('mod'));
  4. $modelos = (Session::get('mod')!="") ? $this->mod_v->obtenerXDataList(Session::get('mod')) : array();
  5. if (isset($_POST['btnaceptar'])) {
  6. if($this->checkDates()) {
  7. if(isset($_FILES['foto'])){
  8. $ruta=$this->checkImage($_FILES['foto']);
  9. if($ruta!= null){
  10. $load= array($_POST['txtmat'],$_POST['txtcant'],$_POST['txtdes'],$ruta,$_POST['txtmod'],$_POST['cboxtipo']);
  11. $this->mod_v->guardame($load);
  12. Session::set('vehiculos', array());
  13. Session::set("msg","Vehículo Creado");
  14. $this->redirect(array('index.php'));
  15. exit();
  16. }
  17. }
  18. }
  19. }
  20. Session::set('modelos', $modelos);
  21. Session::set('tiposveh', $this->mod_tv->obtenerTodos());
  22. $this->redirect(array('add.php'));
  23. }
  24. else {
  25. Session::set("msg","Debe ser administrador para acceder.");
  26. $this->redirect(array('Main','index.php'));
  27. }
  28. }
  29. public function edit(){
  30. if(Session::get("tipo") == "admin"){
  31. Session::set("id",$_GET['p']);
  32. Session::set('mod', isset($_POST['txtmod']) ? $_POST['txtmod'] : Session::get('mod'));
  33. $modelos = (Session::get('mod')!="") ? $this->mod_v->obtenerXDataList(Session::get('mod')) : array();
  34. if (Session::get('id')!=null && isset($_POST['btnaceptar'])){
  35. if($this->checkDates()) {
  36. $load= array($_POST['hid'],$_POST['txtmat'],$_POST['txtcant'],$_POST['txtdes'],$_POST['txtmod'],$_POST['cboxtipo']);
  37. $this->mod_v->modificame($load);
  38. Session::set('vehiculos', array());
  39. Session::set("msg","Vehículo Editado");
  40. $this->redirect(array('index.php'));
  41. exit();
  42. }
  43. }
  44. Session::set('vehiculo', $this->mod_v->obtenerXId(Session::get('id')));
  45. Session::set('modelos', $modelos);
  46. Session::set('tiposveh', $this->mod_tv->obtenerTodos());
  47. $this->redirect(array('edit.php'));
  48. }
  49. else {
  50. Session::set("msg","Debe ser administrador para acceder.");
  51. $this->redirect(array('Main','index.php'));
  52. }
  53. }
  54. public function foto(){
  55. if(Session::get("tipo") == "admin"){
  56. if (isset($_POST['btnaceptar'])) {
  57. if(isset($_FILES['foto'])){
  58. $ruta= $this->checkImage($_FILES['foto']);
  59. if($ruta!= null){
  60. $load=array(Session::get('vehiculo')['vehId'],$ruta);
  61. $this->mod_v->mod_foto($load);
  62. echo 'anda';
  63. header("Location:index.php?c=vehiculos&a=edit&p=$load[0]");
  64. exit();
  65. }
  66. }
  67. }
  68. $this->redirect(array('foto.php'));
  69. }
  70. else {
  71. Session::set("msg","Debe ser administrador para acceder.");
  72. $this->redirect(array('Main','index.php'));
  73. }
  74. }
  75.  
  76. private function checkImage($file){
  77. if ($file["error"] > 0){
  78. Session::set('msg', "ha ocurrido un error");
  79. return null;
  80. }
  81. else {
  82. $permitidos = array("image/jpg", "image/jpeg", "image/gif", "image/png");
  83. if($this->checkExtension($file)){
  84. $prefijo = substr(md5(uniqid(rand())),0,6);
  85. $ruta = "View/Layout/upload/".$prefijo."_".$file['name'];
  86. $limite_kb = 100;
  87. if (in_array($file['type'], $permitidos) && $file['size'] <= $limite_kb * 1024){
  88. $resultado = move_uploaded_file($file["tmp_name"], $ruta);
  89. return ($resultado) ? $ruta : null;
  90. }
  91. else {
  92. Session::set('msg', "tipo de archivo no permitido o excede a los $limite_kb kb");
  93. return null;
  94. }
  95. }
  96. else {
  97. Session::set('msg', "error en la extensión");
  98. return null;
  99. }
  100. }
  101. }
  102. private function checkExtension($file){
  103. $file['type'] = pathinfo($file['name'], PATHINFO_EXTENSION);
  104. $types = array('jpg', 'jpeg','png', 'gif');
  105. foreach($types as $type){
  106. if($type == $file['type']){
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement