user2101411
user2101411

Reputation: 1202

Removing document root from file path Zend Framework 2

I'm trying to display an image but I am running into the error of Not allowed to load local resource: file:///C:/xampp/htdocs/public/images/profile/jimmy/status/boned.jpg in the browser console. What I am trying to do is use the base path provided by Zend Framework 2 but I'm retrieving the images in the model so (as far as I know), I can't use $this->basePath() like I would in the view.

This is my json string I am returning but would like to just be able to return /images/profile/jimmy/status/boned.jpg and whatever other images are in there.

I'm getting all the files outside of the directory 'status'. I am trying to get the files inside the status directory. When I did a var_dump this is what I get string(43) "C:\xampp\htdocs/public/images/profile/jimmy" I'm unclear why it is omitting the status directory after '/jimmy'

json string being returned:

{"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"]}}

Here is the relevant PHP code (in the model):

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));
     }
}

controller code:

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;
}

jquery code:

$.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);
});

I've been trying to use other directory functions provided with PHP but if I try anything, I run into the error directory could not be found. Basically what I am trying to do is use the similiar approach of $this->basePath() but in a model.

I hope that is clear enough..

Thanks!

Here is a screenshot of what I'm getting and how I want to get the status directory, not the directory outside of it.

enter image description here

Upvotes: 1

Views: 208

Answers (1)

SlaWitDev
SlaWitDev

Reputation: 467

I have an idea. In your code is:

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

Try: var_dump($status_dir);

I guess 'status' will be in 'basename' and / or in 'filename' pathinfo gets last segment of argument string path as 'basename'. Pathinfo only parses string as path and return array info, don't check it for isDir or isFile. Your correct chdir should looks like chdir($status_dir['dirname'] . '/' . $status_dir['basename'] ); if you need use of pathinfo.

In other words: dirname of 'images/profile/jimmy/status' is 'images/profile/jimmy' and its a reason why you don't see status in var_dump($status_dir['dirname']) and why chdir($status_dir['dirname']) not working correctly.

Upvotes: 1

Related Questions