php - 使用命名空间从外部文件调用 PHP 类。

标签 php class namespaces

这是我第一次发帖,如果我没有选择,我真的不会发帖(我最不想做的事情就是无缘无故地烦人)。我已经研究这个脚本几天了,我用头撞着键盘试图找出它为什么不起作用。

文件 1:这是一个部分上传类,旨在从外部文件中使用。

<?php
namespace Tools\File;
class Upload {

    protected $destination;
    protected $max = 51200;
    protected $messages = array();
    protected $permitted = array('image/gif','image/jpeg', 'image/pjpeg', 'image/png');

    public function __construct($path){
        if (!is_dir($path) || !is_writable($path)) {
            throw new \Exception ("$path must be a valid,writable directory");
        }
        $this->destination = $path;
        }
    public function upload(){
        $upload = current($_FILES);
        if ($this->checkFile($uploaded)){
            $this->moveFile($uploaded);
            }
        }

    protected function checkFile($file){
        return true;
        }
    protected function moveFile ($file) {
        $success = move_uploaded_file($file ['tmp_name'] , $this->destination . $file['name']);
            if ($success){
                $result = $file['name'] . 'was uploaded successfully';
                    $this->messages[] = $result;
            }else{
                $this->messages[] = 'Could not upload' . $file['name'];
                }
        }   
    public function getMessages(){
        return $this->messages;
        }
}

文件2:上传图片表格
<head>
    <?php use Tools\File\Upload as FileUploader; ?>
</head>
<body>

    <?php
    if(isset($_POST['upload'])){
        // difine the path to the upload folder
        $destination = 'C:/Uploads/';
        // Include upload class
        require_once '../Tools/File/Upload.php';
    // Creates new instance of the upload class as loader
    try{
        $loader = new FileUploader ($destination);
        $loader->getMessages();
    }catch(Exception $e){
        echo $e->getMessages();
        }
    }   
    // Checks and echos result
        if(isset($result)){
            echo '<ul>';
            foreach($result as $message){
                echo "<li>$message</li>";
            }
            echo '</ul>';
        }
        ?>
<form id="uploadImage" enctype="multipart/form-data" action="<?php htmlspecialchars($_SERVER['PHP_SELF']) ?>" method="post">
    <p>
        <label for="image">Upload Image</label>
        <input type="hidden" name="MAX_FILE_SIZE" value="<?= $max; ?>">
        <input type="file" name="image" id="image">

    </p>
    <p>
        <input type="submit" name="upload" value="upload" id="uplaod">
    </p>
</form>
<!-- For checking the Files Super global-->

<pre>
    <?php
    if(isset($_POST['upload'])){ print_r($_FILES);}?>
</pre>

</body>

该表格工作正常。当在 File 2 本身内部调用“move_uploaded_file”函数时,文件被完美上传,但它似乎根本没有调用类。

我已经数不清我已经看过代码的次数了。重新安排并尝试我所知道的一切以使其正常工作。

任何帮助将不胜感激。
提前感谢您抽出宝贵时间。

最佳答案

您需要从第二个文件调用上传方法。否则,它将实例化 Upload 类,但不会触发任何上传事件。

try {
    $loader = new FileUploader ($destination);
    $loader->upload();
    $loader->getMessages();
 } catch(Exception $e){
    echo $e->getMessages();
 }

关于php - 使用命名空间从外部文件调用 PHP 类。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34189765/

相关文章:

c++ - 不确定如何在我的主函数中调用我的类(编译)

c++ - 如何将类成员函数的返回类型设置为私有(private)结构的对象

.net - 如何让 Visual Studio 2015 添加带有命名空间声明的新 VB.NET 类?

c++如何使用子类调用函数,具有父类(super class)指针

php - 学说 :generate:crud for Entites outside a bundle

php - PHP中查询MySQL的权限

php - SQL在查询中添加子索引列?

PHP:使用父类作为接口(interface)中的类型声明

php - 该 MySQL 版本不允许使用该命令

java - 如何在不从 java API 导入日期/日历的情况下将 n 天添加到 java 中的日期?