robert

Differences between browser and command line API access

Recommended Posts

I’m trying to get the filename of a file in an imagefield via command line, but there seems to be a difference between the API access via browser and via command line. When I try the following (reduced case):

$img = $pages->get('/')->name_of_imagefield;
var_dump($img->filename);
var_dump($img->basename);
var_dump($img->url);
var_dump($img->path);

I get these results:

Browser (as expected):

  • filename: full path to file including name (e.g. /var/www/public/site/assets/files/1/filename.jpg)
  • basename: only name without path (e.g. filename.jpg)
  • url: webroot-based path to file including name  (e.g. /site/assets/files/1/filename.jpg)
  • path: NULL

Command Line:

  • filename: NULL
  • basename: NULL
  • url: webroot-based path to file WITHOUT filename (e.g. /site/assets/files/1/)
  • path: full path to file WITHOUT filename (e.g. /var/www/public/site/assets/files/1/)

What am I missing? Or is this normal behaviour and I just never noticed before? The website is running Processwire 3.0.94 with PHP 7.0.

Thanks a lot for any help!

Share this post


Link to post
Share on other sites

What a coincidence! Just one second after my post, I found this topic and horsts answer helped me as well. Thanks a lot, horst! 🙂 I still don’t understand why there were differences between command line and browser, but when I add $img->first()->filename, I get identical results (although the maximum file amount in the imagefield was set to 1, btw). 

Share this post


Link to post
Share on other sites

I think you're coming across the fact that on the command line outputformatting is always off so image fields always return an array regardless of the config settings.

  • Like 3

Share this post


Link to post
Share on other sites

Hi Adrian,

you are absolutely right! As soon as I switch output formatting off, the results are identically. Thanks for the fast clarification 🙂

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.