【问题标题】:Calling PHP Class from external file using namespace.使用命名空间从外部文件调用 PHP 类。
【发布时间】:2015-12-09 21:57:09
【问题描述】:

这是我第一次发帖,如果我没有其他选择,我真的不会发帖(我最不想做的就是无缘无故地烦人)。几天来,我一直在编写这个脚本,我的头撞在键盘上试图找出它为什么不起作用。

文件 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>

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

我已经记不清我翻阅代码的次数了。重新安排和尝试我所知道的一切,让它发挥作用。

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

【问题讨论】:

    标签: php class namespaces


    【解决方案1】:

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

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

    【讨论】:

    • 适用于寻找类似问题答案并希望测试此代码的任何人。我不得不将文件 1 中的 $uploaded 更改为 $upload,我能否再次感谢 Steve E。我知道我说过我不会烦人,但我真的很感谢你的帮助
    • 接受答案然后:)
    猜你喜欢
    • 2013-12-31
    • 1970-01-01
    • 2017-12-11
    • 2019-06-02
    • 1970-01-01
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    相关资源
    最近更新 更多