process_add_user.php文件中的错误未显示。当值为true但是返回为$ errors时将上载文件错误值不显示?

时间:2015-09-07 12:08:12

标签: php

    **check_imgupload.class.php**

    <?php
    class Image extends Connection
    {
    private $img_name;
    private $img_extension;
    private $img_size;
    private $tmp_name;
    private $target_dir;



    //getter and setters
    public function getadmin_img()
    {
        $this->img;
    }
    public function setadmin_img($img='')
    {
        $this->img=$img;
    }
    public function getimg_name()
    {
        $this->img_name;
    }
    public function setimg_name($imgn='')
    {
        $this->img_name=$imgn;
    }

    public function setimg_size($size='')
    {
        $this->img_size=$size;
    }
    public function setimg_type($imgty='')

    {
        $this->img_type=$imgty;
    }

    public function settmp_img_name($tmpimg='')
    {
        $this->img_tmp=$tmpimg;
    }




    public function check_img()
    {
        if(isset($this->img)){
          $errors= array();
          //$file_name = $_FILES['image']['name'];
          //$file_size =$_FILES['image']['size'];
          //$file_tmp =$_FILES['image']['tmp_name'];
          //$file_type=$_FILES['image']['type'];
          $file_ext=strtolower(end(explode('.',$this->img_name)));

          $expensions= array("jpeg","jpg","png","PNG","bmp","Gif");
          $target_dir='../Uploads';

          if(in_array($file_ext,$expensions)=== false){
                $errors[]="extension not allowed, please choose a JPEG or PNG file.";
          }

          else if($this->img_size> 2097152){
             $errors[]='File size must be excately 2 MB';
          }
          else if(file_exists($target_dir.'/'.$this->img_name))
          {
            $errors[]='File already exists..';
          }

          else if(empty($errors)==true){
             move_uploaded_file($this->img_tmp,"$target_dir/$this->img_name");
             return true;
          }
          else{
             return $errors;
          }
       }


    }
    }


**Process_add_user.php**


<?php
session_start();
//load classes
require_once('../classes/db-connection.class.php');
require_once('../classes/user.class.php');
require_once('../classes/locate.class.php');
require_once('../classes/check_img.class.php');

//create an object

$objImage= new Image();
//


//setting the inputs
$admin_img=$_FILES['admin_img'];
$admin_img_name=mysqli_real_escape_string($objImage->conxn,$_FILES['admin_img']['name']);
$size=mysqli_real_escape_string($objImage->conxn,$_FILES['admin_img']['size']);
$type=mysqli_real_escape_string($objImage->conxn,$_FILES['admin_img']['type']);
$tmp_name=mysqli_real_escape_string($objImage->conxn,$_FILES['admin_img']['tmp_name']);


$objImage->setadmin_img($admin_img);
$objImage->setimg_name($admin_img_name);
$objImage->setimg_size($size);
$objImage->setimg_type($type);
$objImage->settmp_img_name($tmp_name);
//check the image extension


$img_return=$objImage->check_img();

//now teh condition for flag

    if($img_return==true)
    {
        $objUser=new User();
        $admin_name=mysqli_real_escape_string($objUser->conxn,$_POST['admin_name']);
        $admin_email=mysqli_real_escape_string($objUser->conxn,$_POST['admin_email']);
        $admin_password=mysqli_real_escape_string($objUser->conxn,$_POST['admin_password']);
        $admin_password2=mysqli_real_escape_string($objUser->conxn,$_POST['admin_password2']);
        $admin_access_level=mysqli_real_escape_string($objUser->conxn,$_POST['admin_access_level']);
            $objUser->setAdmin_username($admin_name);
            $objUser->setPassword($admin_password);
            $objUser->setAdmin_email($admin_email);
            $objUser->setaccess_level($admin_access_level);

        $flag=$objUser->addadmin();


    }
    elseif($img_return==$errors)

                echo $img_return;

            //Errorrs are not shown

                //new locate('../index.php?error='.base64_encode());
    else
        new locate('../../index');

我已经检查了上传之前的图片,当check_img()函数返回true 图像被上传并且它正确并且当返回$ errors时我想显示返回相同的错误为 $ errors echo $ errors 无效。

1 个答案:

答案 0 :(得分:0)

要显示数组,您可以var_dump()print_r()

试试这个......

if(isset($this->img)){
    ...any of this stuff...
}
      else{
         return $errors;
         print_r($errors);
      }
   }
}

应该这样做。

快乐的编码!

注意:强烈建议您对此进行错误处理。页面的输出很难看,只是用户友好或优雅的方式来处理错误。