-4

Sorry I am really a newbie to html and PHP and others included in web coding. I have a piece of code that should be showing all pictures in the folder, instead i just get it opened like a text file in a web-browser. Is there a need to put a special html or anything else because i really would like this to work... Thank you. And by the way here is the code if there is any special needs:

    <?
    $files = glob("images/*.*");
    for ($i=1; $i<count($files); $i++)
{
    $num = $files[$i];
    print $num."<br>";
    echo '<img src="'.$num.'" alt="random image" />'."<br><br>";
}
?>

Also yes the .php file is named: "index.php". Inside the images folder I got a few of them but that does not matter for this question.

Frosty Tosty
  • 63
  • 1
  • 8
  • 1
    How are you trying to load this file? Are you doing `http://localhost/index.php` or `file:///www/index.php`? PHP needs a *web server* to work. This can be local (WAMP) or remote. – gen_Eric Nov 06 '14 at 18:22
  • Have you tried right clicking the file and selecting the "Open With" option? – AlvinJ Nov 06 '14 at 18:23
  • 2
    Are you running this from a server? Php is a server side language and will not run when opened from a client machine like html/javascript. – zack.lore Nov 06 '14 at 18:23
  • @zack.lore is right : you might want to install PHP, for instance using a package like WAMP. – SolarBear Nov 06 '14 at 18:24
  • Ok, ok so i have read everything but is there a way to get php running on PC offline because i can host it just the thing is it is a free hosting thing so it is anoying to go trough the process everytime i do something with the code. – Frosty Tosty Nov 06 '14 at 18:31
  • 1
    Install a local server on your machine - something like WAMP, XAMPP or MAMP. – Jay Blanchard Nov 06 '14 at 18:33

2 Answers2

3

There could be several issues here:

1) For PHP to run, it needs to be on a server that has PHP installed. If you are running it locally, you'll need to run it at localhost. Look into MAMP if you are on MAC, or WAMP if you are on windows. Both will let you set up an easy local php server.

2) You should open the file with <?php instead of just <?. the shortcut <? is only available if you turn it on in your php configs.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Trattles
  • 647
  • 1
  • 6
  • 21
0

To start, having PHP running on the system in question would be a good start. You'll also need some sort of web server to "serve" the PHP file for you (Apache, for example, uses mod_php sometimes).

If you're a total noob, I'd recommend something like MAMP so you can start learning locally with a simple and easily configured "MAMP" (Mac Apache MySQL PHP) environment.

Community
  • 1
  • 1
typeoneerror
  • 55,990
  • 32
  • 132
  • 223