0

UPDATED: My question has been closed. But the solution someone provided (the question that's been already asked) does not solve my problem. I have already tried single quotes, double quotes etc as stated in someone's else answer. Can someone please open my question?:)

I am using PHP. This is the code I am using to display the image.

echo '<img src="$saving" width="100" height="100" alt="Random image" />' . "<br /><br />";

Here $saving is : userPosts/hemlata993/94/5f2628f30cc623.13126045.jpg The /5f2628f30cc623.13126045.jpg is the image name and it changed everytime it loops through to display next image.

The problem here is: If I write userPosts/hemlata993/94/5f2628f30cc623.13126045.jpg it works, but that's the same thing is inside $saving variable as I printed it out on website and copied this userPosts/hemlata993/94/5f2628f30cc623.13126045.jpg and pasted it in image src to see and it works. Why is it not working when I write the variable name but works if I write the variable content?

I am basically trying to loop through folders and their images and printing it out on the website. It prints out all the name of images along the path correctly.

My full code:

$image = 'userPosts/hemlata993';

function listFolderFiles($dir){
    $ffs = scandir($dir);
 
    unset($ffs[array_search('.', $ffs, true)]);
    unset($ffs[array_search('..', $ffs, true)]);

    // prevent empty ordered elements
    if (count($ffs) < 1)
        return;

    echo '<ol>';
 
    foreach($ffs as $ff){
      
        echo '<li>'.$ff;
       
        $saving = "$dir/$ff";
       
        $saving .=  '<li>'.$ff;
     
        echo "$saving";
        
      
       
        echo '<img src="$saving" width="100" height="100" alt="Random image" />' . "<br /><br />";

        if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
        echo '</li>';
      
    }
    echo '</ol>';
}

listFolderFiles($image);
iachi
  • 115
  • 2
  • 11
  • Variables are not interpreted when string is enclosed between single quotes. Try this: `echo "\"Random"` Enclose string between double quotes and escape inner double quotes by preceding them with a backslash. – Triby Aug 02 '20 at 07:39
  • @Triby I tried it but it does not work – iachi Aug 02 '20 at 07:50
  • Also, I am not sure why my question is closed. The question someone said it already has an answer I have tried that all using single quotes, double etc but it does not work. Could someone please open my question? Thanks – iachi Aug 02 '20 at 07:51
  • You should edit your question to add what you've tried using double quotes. By the way, `$saving` is an HTML list element, not an image URL. – Triby Aug 02 '20 at 23:06

0 Answers0