Syntax help - replace path with variable

My PHP “expertise” does not go much beyond “copy and paste”.

What syntax do I need to replace = images/intro/ with $img2_folder see below

I have this variable name & value
$img2_folder = “images/intro/”;

The code at the bottom of this post correctly displays all images from folder images/intro/ that meet my criteria, in filename order.

I have managed to include my variable $img2_folder in the echo (last line) but all attempts to replace images/intro/ with $img2_folder in the line starting $handle either do not work or break the page.

I have tried multiple syntax variations eg (FILE))./’.$img2_folder.’);

I think the main issue is something to do with how to include the necessary / and $img2_folder

				$Count1image2 = 0;
			   $handle = opendir(dirname(realpath(__FILE__)).'/images/intro/');
						while($file = readdir($handle)){
							if($file !== '.' && $file !== '..' && ( strpos($file,"damage") !== false && strpos($file,"$city") !== false )){
							$image2[$Count1image2] = $file;
						for($i=0; $i<$Count1image2; $i++)
							echo '<amp-img src="'.$img2_folder.$image2[$i].'" alt="'.$img2_alt.'" class="xs-12" width="353" height="210" layout="responsive"></amp-img>';
//original echo '<amp-img src="images/intro/'.$image2[$i].'"

You can use this, but you need to be consistent. Is it “images/intro/” or is it “/images/intro/”

Hi @astonecipher
I appreciate your help with 2 different issues with the same code. I trust I was correct in posting as two queries as one relates to replacing a path with a variable and the other relates to true / false.

The original code was not created be me (beyond my abilities) but in one place it uses
$handle = opendir(dirname(realpath(FILE)).’/images/intro/’);
and further down it uses
echo ‘<amp-img src="images/intro/’.$image2[$i].’"

As you can see from the code above I have managed to swap the echo folder
with my variable
'.$img2_folder (note different position of ')

The original code is not consistent with use of

and does not work if I remove the leading / in the opendir section

I am not sure if I need to make images/intro/ consistent in both places or if there is a syntax I can use to replace (in the opendir section)

while retaining the opening /

Note (The trailing / in the opendir section has no effect if present or not present, but is clearly required in the echo section as it separates the folder from the file name variable.

Be aware of the difference between a file-system path, filenames and a URL (or URI).

a filename is just the name of a file it self e.g. logo.jpg or log.txt. So far it is simple.

a file-system path locates to a file or directory on a computer. The opendir() command needs a file-system path to a directory to work. Paths can ben written in absolute or relative manners.
Example of absolute path: C:\xampp\htdocs\readm.txt for Windows or /var/www/html/readme.doc for Linux systems. Relative paths ar a latter portion of absolute paths for example html/readme.doc.
Relative paths are interpret from the “current work directory”. So if the current workdirectory is
C:\xampp then you could use the relative htdocs\readm.txt path to find the same file as in the absolute path example.

A URL or URI are paths that are used over the internet. You are making a amp-img element with a src attribute in your code example. The src attribute must have a URI because your browser will try to find the image file over the internet using a request to a webserver. On the server the URL will be used from the document root which is the directory where your “homepage” index.php is located. URL’s are also available in absolute and relative flavors. Examples of absolute URL’s are (forces the use of the htttp protocol) (forces the use of the htttps protocol)
// (can be used on any protocol, browser will use the same as the main request)
/images/logo.jpg (using a trailing slash)

and a relative URL could again be
images/logo.jpg (without a trailing slash)

Like i wrote a few lines above a URL will be started from the document root on the server. But if we want to find a image with a URI images/intro/logo.jpg on the file system and the document root is /var/www/html and the main request was http:/ than the full absolute path will be /var/www/html/amp/images/intro/logo.jpg which is document root + directory from the main request (amp) + relative URI

Hope this helps you a little bit.

Hi @frankbeen

Thanks for the info. I was aware of differences between file path and URI but your explanation clarified a few things.

Based on your logo example the existing code uses absolute to opendir
and relative to echo images/intro/

$handle = opendir(dirname(realpath(FILE)).’/images/intro/’);

echo ‘<amp-img src="images/intro/’.$image2[$i].’"
images/intro/ replaced with my variable $img2_folder

I have already managed to replace images/intro/ with my variable $img2_folder in the echo section
echo ‘<amp-img src="’.$img2_folder.$image2[$i].’"

I think all I need is the syntax (eg use of " . ') to replace images/intro/ in the opendir part - and keeping the / before $img2_folder

It would be more simple to help you if you gave a bit more information without any variables about:

  • What you want to achieve (how must the result of the code look like)

  • What is your document root (the absolute path to the homepage)

  • What is the absolute directory that you want to read

Perhaps I included too much detail in original question, then the discussion went off to other aspects of the code I included.

Query again
In one part of my code I had successfully replaced
echo ‘<amp-img src="images/intro/’.$image2[$i].’"
echo ‘<amp-img src="’.$img2_folder.$image2[$i].’"

I just needed the syntax to replace images/intro/ with $img2_folder in this code
$handle = opendir(dirname(realpath(FILE)).’/images/intro’);

After some reading about PHP concatenation and the use of single ’ and double " I worked it out with trial and error.

solution =
$handle = opendir(dirname(realpath(FILE)).’/’."$img2_folder");

Basically I need a way to concatenate / and $img2_folder

The code above works, but if anyone can point out any errors in my syntax I would be happy to read comment.

Thanks to those who tried to help me and who expanded my knowledge of PHP.
This place has such a different atmosphere to stackoverflow which is so unfriendly to non experts

i was wondering what FILE is. is it a contant? I know the magic constant FILE

One other advise would be stop with giving a variabele a number after its name. what about $imageFolder instead of $img2_folder? or otherwise $image_folder or just $folder …

At last you can consider to put the string ‘images/intro’ in another variabele (or constant) since you are using it at least twice.

Sponsor our Newsletter | Privacy Policy | Terms of Service