public function __construct致命错误:未捕获错误:找不到类

时间:2018-06-11 04:10:46

标签: php

我正在尝试上传图片并将其保存到数据库中,并且我得到了这个错误,我非常确定可以找到该类,因为我给它命名空间。

  找不到'ElCont \ BaseController'   /Applications/MAMP/htdocs/eliapp2/src/Upload.php:11堆栈跟踪:

     

0 /Applications/MAMP/htdocs/eliapp2/src/Upload.php(33):Upload-> __ construct()

     在第11行的/Applications/MAMP/htdocs/eliapp2/src/Upload.php中抛出

1 {main}

关于可能是什么原因的任何想法。

upload.php的

<?php

use \ElCont\BaseController as Base;

class Upload
{

    public function __construct()
    {
        // its this line below
        $fileUpload = new Base;
        $file_name = $_FILES['image_name'];
        $file = $_FILES['file'];

        if(isset($_POST['submit'])){

            $fileUpload->uploadImage($file_name, $file);

            header('Location: /');
            exit;

        }else{
            die("File Was Not Submitted");

        }

    }


}


$upload = new Upload();

BaseController.php

<?php

namespace ElCont;

use \Eli\Db as DB;

use \Eli\View as View;

class BaseController {

    private $db;
    private $connection;
    public $fileName;
    public $fileDir;
    public $upload_src =  "./public/upload/";
    private $supported_formats = ['image/jpeg', 'image/jpg', 'image/png'];


    public function __construct()
    {
        $this->db = new DB;
        $this->connection = $this->db->connect();

    }


    public function getHome()
    {
        $view = new View;
        return $view->render('home');
    }


    public function uploadImage($imagename, $image )
    {

        try{

            if(is_array($image)){

                if(in_array($image['type'], $this->supported_formats )){

                    move_uploaded_file($image['tmp_name'], $this->upload_src . $image['name'] );
                    echo 'File has been Uploaded';
                } else{
                    die("File format is not supported");
                }

            }

        }catch (\Exception $e)
        {
            exit("Error: " . $e->getMessage());
        }

        try{
            $file= $this->connection->prepare("INSERT INTO images(image_name, img) VALUES (:imagename, :img) ");
            $file->bindparam(":imagename", $imagename);
            $file->bindparam(":img", $image);
            $file->execute();

        }

        catch (\Exception $e)
        {
            exit("Error: " . $e->getMessage());
        }


    }

查看/ Home.php

<?php require_once 'paritals/header.php';?>
<?php require_once 'paritals/nav.php';?>

    <div class="container">
        <div class="row">
            <div class="col-md-6 offset-md-4 p-5 ">
                <h1>Upload Image</h1>

                <div class="form-group mt-4">
                    <form action="/src/Upload.php" method="post" enctype="multipart/form-data">
                        <label for="exampleInputFile">File input</label>
                        <label>File Name</label>
                        <input class="form-control" type="text" name="image_name">
                        <input id="input-id" class="file form-control-file mt-4" type="file" data-preview-file-type="text" >
                        <small id="fileHelp" class="form-text text-muted">This is some placeholder block-level help text for the above input. It's a bit lighter and easily wraps to a new line.</small>
                            <button type="submit" class="btn btn-primary mt-4">Submit</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

<?php require_once  'paritals/footer.php';?>

0 个答案:

没有答案