从文件路径Zend Framework 2中删除文档根目录

时间:2018-03-11 21:47:26

标签: php zend-framework2

我试图显示图片,但我在浏览器控制台中遇到 <div class="field" ng-if="drink.name == 'tea'"> <label class="radio" ng-repeat="drink in grindsIndex.menuDrinks" > <input type="radio" ng-model="value" value="{{ drink.price }}" placeholder="{{ drink.select }}" name="select" ng-model="grindsIndex.name" ng-change="drinkValue(value)" ng-click="clickMethod()"/> {{ drink.select }} &nbsp {{ drink.price }} </label> </div> 错误。我想要做的是使用Zend Framework 2提供的基本路径,但我在模型中检索图像(据我所知),我不能像Not allowed to load local resource: file:///C:/xampp/htdocs/public/images/profile/jimmy/status/boned.jpg一样使用我认为。

这是我正在返回的json字符串,但我希望能够返回$this->basePath()以及其他任何图像。

我获取了目录&#39; status&#39;之外的所有文件。我试图获取状态目录中的文件。当我执行/images/profile/jimmy/status/boned.jpg这是我得到的var_dump我不清楚为什么在&#39; / jimmy&#39;

之后省略状态目录

返回json字符串:

string(43) "C:\xampp\htdocs/public/images/profile/jimmy"

以下是相关的PHP代码(在模型中):

{"feed":{"username":"Timmy","status":["this is jimmy, test"],"images":["videos","status","sithtoon.jpg","sith.jpg","edited_photos","diploma.jpg","current","albums","Screenshot_2016-08-09_21_28_13_361272.jpg","Screenshot_2016-08-05_17_55_48_500802.jpg","515gIIJ-Imgur.png",".htaccess"]}}

控制器代码:

public function listFriendsStatus()
{
    $user_id = $this->getUserId()['id'];

    // get the friend ids based on user id
    // and then compare the friend id to the id in status table
    $friend_query = new Select('friends');

    $friend_query->columns(array('friend_id'))
    ->where(array('user_id' => $user_id));  

    $query = $this->sql->getAdapter()->query(
        $this->sql->buildSqlString($friend_query),
        Adapter::QUERY_MODE_EXECUTE
    );

    if ($query->count() > 0) {
        $friend_id = array();

        foreach ($query as $result) {
            $friend_id[] = $result['friend_id'];
        }


        $status = new Select('status');

        $status->columns(array('status'))
        ->where(array('id' => $friend_id)); 

        $status_query = $this->sql->getAdapter()->query(
            $this->sql->buildSqlString($status),
            Adapter::QUERY_MODE_EXECUTE
        );

        if ($status_query->count() > 0) {
            // check if a image was used
            $members = new Select('members');

            $members->columns(array('username'))
            ->where(array('id' => $friend_id));

            $image_query = $this->sql->getAdapter()->query(
                $this->sql->buildSqlString($members),
                Adapter::QUERY_MODE_EXECUTE
            );

            if ($image_query->count() > 0) {
                foreach ($image_query as $value) {
                    if (is_dir(getcwd() . '/images/profile/' . $value['username'] . '/status/')) {
                        $status_dir = pathinfo(getcwd() . '/images/profile/' . $value['username'] . '/status/');
                    }
                }


                $images = array();

                chdir($status_dir['dirname']);

                var_dump($status_dir['dirname']);

                // retrieve the image inside the status directory
                foreach (array_diff(scandir($status_dir['dirname'], 1), array('.', '..')) as $values) {
                    $images[] = $values;    
                }
            } else {
                throw new FeedException("The user does not exist in the user table.");
            }

            $status = array();

            // get all the statuses
            foreach ($status_query as $rows) {
                $status[] = $rows['status'];    
            }

            return array('username' => ucfirst($value['username']), 'status' => $status, 'images' => $images); // how to just get the basePath path with zf2
        } else {
            throw new FeedException("No status was found for your friends.");
        }
     } else {
        throw new FeedException(sprintf("Could not locate any friends for %s", $this->user));
     }
}

jquery代码:

public function getfriendstatusAction()
{
    $layout = $this->layout();
    $layout->setTerminal(true);

    $view_model = new ViewModel();
    $view_model->setTerminal(true);

    try {
        echo json_encode(array('feed' => $this->getStatusService()->listFriendsStatus()));
    } catch (FeedException $e) {
        echo json_encode(array('fail' => $e->getMessage()));
    }

    return $view_model;
}

我一直在尝试使用PHP提供的其他目录函数,但如果我尝试了任何操作,则无法找到错误目录。基本上我要做的是使用$.getJSON('/members/feed/get-friend-status', function(data) { $.each(data, function(i, item) { $('.w3-container.w3-card-2.w3-white.w3-round.w3-margin').find('h4').html(data[i].username); $('.w3-container.w3-card-2.w3-white.w3-round.w3-margin').find('p').html(data[i].status); $('.w3-container.w3-card-2.w3-white.w3-round.w3-margin').find('img').attr('src', data[i].images); }); }).fail(function(response) { console.log(response); }); 的类似方法,但在模型中。

我希望这很清楚......

谢谢!

以下是我所获得的内容以及我希望如何获取状态目录的屏幕截图,而不是其中的目录。

enter image description here

1 个答案:

答案 0 :(得分:1)

我有个主意。 在你的代码中是:

$status_dir = pathinfo(getcwd() . '/images/profile/' . $value['username'] . '/status/');
// ..............
chdir($status_dir['dirname']);
var_dump($status_dir['dirname']);

尝试:     var_dump($status_dir);

我猜&#39;状态&#39;将在&#39; basename&#39;和/或文件名&#39; pathinfo将参数字符串路径的最后一段作为&#39; basename&#39;。 Pathinfo仅将字符串解析为路径并返回数组信息,不要检查isDir或isFile。如果需要使用pathinfo,那么正确的chdir应该看起来像chdir($status_dir['dirname'] . '/' . $status_dir['basename'] );

换句话说:&#39; images / profile / jimmy / status&#39;的dirname是&#39; images / profile / jimmy&#39;这就是为什么你没有看到var_dump($status_dir['dirname'])中的状态以及为什么chdir($status_dir['dirname'])无法正常工作的原因。