2

This question looks pretty easy (and probably it is), but I am stuck with the following scenario: I'm using XAMPP and have several Joomla installations in subfolders such as \joomla3, \joomla4, etc. I want to check if a given file is in the folder 'images' and if the folder 'images' is a subfolder of root. Following code is from a plugin written in PHP:

$uri = Uri::getInstance();
$base = $uri->base();

$base is 'https://localhost/Joomla3/'

From given URLs like:

  1. $src = '/Joomla3/images/foo/bar.webp'
  2. $src = 'https://localhost/joomla3/images/foo/bar2.png'

Following code

$uri2 = Uri::getInstance($src);
$path = $uri2->getPath();

returns:

  1. /Joomla3/images/foo/bar.webp
  2. /joomla3/images/foo/bar2.png

The returned value of getPath is starting with the subfolder of the installation, so in order to check if 'images' is in the root I have to use some regex probably to exclude '/Joomla3/'. Is there a more elegant way to have the following returns:

  1. /images/foo/bar.webp
  2. /images/foo/bar2.png

Having them is easy to check if the path starts with '/images/'.

2 Answers 2

1
$imageIsAt = Uri::base(true) . '/images/my_logo.png';

Results will be:

  1. /images/my_logo.png (IN CASE OF ROOT AS MAIN FOLDER)
  2. /subfolder_site/images/my_logo.png (IN CASE OF SUBDIRECTORY)

PHP 8.X (to check if it starts properly)

if (str_starts_with(Uri::base(true).'/images', '/images')) 
    // The URI starts with /images
    echo('I'm at root folder :)');
else
    // The Uri starts with some_subfolder/images
    echo('I'm not at root :(');

Another way to test paths.

You want to check if a given file is in 'images' folder. I don't know what information you've got about the file itself but let's assume you have 'logo.png' filename and that's all.

Whatever the file path you've got you can easily extract filename with extension via File/JFile libraries and test it like below:

To check if it's inside images you can also do:

use Joomla\CMS\Filesystem\File; // include this namespace
$path = JPATH_ROOT.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'logo.png';
var_dump(File::exists($path));

Mind that this method is suitable for Joomla 4.X. Joomla 3.x might have JFile.

There are few server-side paths like JPATH_ROOT, JPATH_ADMINISTRATOR, ...

1
  • In my case result of Uri::base(true) is "/Joomla3". Which could be useful for checks, but in a way not as in your answer. Or something in my code is wrong. // Joomla 4.2.x, PHP 8.1.x
    – Plamen
    Oct 12, 2022 at 5:32
0

Have you looked on J! Constants ? https://docs.joomla.org/Constants

2
  • So far i'm extracting all behind the host from $base and removing same string from beginning of $path
    – Plamen
    Oct 11, 2022 at 19:23
  • Stack Exchange sites prefer to avoid link-only answers. Please statically express your advice as text and perhaps demonstrate the implementation of your advice in addition to the hyperlink to documentation. Oct 11, 2022 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.