Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Basic PHP Website (2018) Building a Media Library in PHP Using PHP Code on Our Site

Andrew Meininger
Andrew Meininger
7,615 Points

Working remotely: after I saved the file as a .php extention did my file show up on Chrome as code instead of a webpage?

I saved my file as .php, opened with Chrome and it spits out the code rather than the site itself

10 Answers

If you open it as a file then you'll see the file contents. You need to have it served up by a server that can handle PHP pages. E.g., if you have MAMP installed on a Mac, make sure the servers are started, then open the page from localhost.

Calvin Secrest
Calvin Secrest
24,815 Points

I am having trouble with this issued also, after I saved the file from my Atom text editor with my /Documents/Sites/dev/Personal Media Library/s1v1 path. While inside Root Document from my MAMP local server it still doesn't open the index.php file in Chrome browser. I'm not sure what this causing this issue even after I turn on my MAMP server the index.php file will not display.

Make sure the address in your browser's address bar is localhost/rootdocumentname/pagename and not file;//...

OR, if you are using MAMP with the default port numbers: localhost:8888/rootdocumentname/pagename

Here rootdocumentname is the root folder for your web server. For MAMP, unless you've changed it, it is MAMP/htdocs/

OR, if you have created a subfolder under htdocs it would be: MAMP/htdocs/subfoldername/

and pagename is the name of the php file.

Sean, if you changed the port number as TH instructed, then you are using the default and you don't need to put in a port number, and, indeed, then, if you do add 8888 localhost:8888 won't work.

So, the check list is as follows: (1) make sure MAMP is running (that is, that both the web server and the database server are running), (2) make sure your URL is correct, (3) make sure the php page is in the folder you put in the URL, (4) make sure that folder is either inside MAMP > htdocs, or the folder to changed the web server's root directory to, and (5) make sure the php page is really a php page. It sounds like you've done 1 - 4, so the spotlight turns to the php itself. Try a very simple php page. Something like this:

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'; ?> 
 </body>
</html>

Name it test.php, put it in the root folder (which appears to be s1v1), put http://localhost:8888/s1v1/test.php, and see if it works.

Sean Pierce Sumler
Sean Pierce Sumler
18,968 Points

jcorum I set up the test like you instructed and it worked! I feel a little bit like a crazy person, because now when I also navigate to http://localhost/s1v1/ the php file is served properly as well. I'm not complaining, I just don't understand what I was doing wrong to begin with. I suppose that knowledge will come with experience.

Thanks for sticking with me @jcorum I appreciate you patience and your help!

Calvin Secrest
Calvin Secrest
24,815 Points

@jocrum: I didn't realize my port wasn't config to host the index.php on my local server. Thanks for sharing your input on this topic.

Sean Pierce Sumler
Sean Pierce Sumler
18,968 Points

I'm also having trouble with this. Using MAMP I'm able to serve up the index.html file no problem, but after renaming the file to index.php it no longer works. If anyone has any ideas of what I can do to solve this please let me know. Thanks!

Sean, can you copy the entire URL in the address bar of your browser and paste it in a comment so I can see it?

Sean, I don't see any php page in your URL. Plus, if you are using MAMP with the defaults, it would be:

http://localhost:8888/s1v1/pagename.php

assuming s1v1 is a directory or folder, and pagename is the name of the php file.

Sean Pierce Sumler
Sean Pierce Sumler
18,968 Points

Hi jcorum . When MAMP is serving I can successfully navigate to the project folder-

ie: http://localhost/s1v1/ or http://localhost/anyprojectfoldername/

and my index.html will be properly served without adding the port number or page name. I set up MAMP by following along with Treehouse's "Setting Up a Local Development Environment" course and that is how I was instructed to use MAMP.

I did try adding the correct port number and page name anyway, (http://localhost:8888/s1v1/index.php) and still nothing. Neither my index.html nor my index.php would be served.

On a Mac, if you've reset the ports to 80 and 3306, all you need to type as the url is "localhost". Checkout the setting up a server environment link from the first video in this module to see how to reset the ports.

Note, you need to delete the index.html file (this confuses my Mac), so that all you have is index.php in your root folder.

Tiffany Robb
Tiffany Robb
10,140 Points

You are amazing! Thanks Etienne :D

Matt Bryant
Matt Bryant
10,164 Points

Hi folks, I'm having the same issue that Sean had above - the index.html page shows up just fine in the MAMP My Website preview, but when I rename the file to index.php, I just get a blank page. I am on a Mac. I went through the local MAMP setup video course for Mac, and it has been working fine even before this course. My root folder in the MAMP setup is pointing to this project (like I said, the default index.html shows up just fine), ports are set to 80/80/3036, and my URL per the setup video is localhost. Any other ideas? Thanks!

UPDATE: Nevermind, I figured it out. But I will leave this here in hopes that it might help others. I made a mistake in the PHP code that the instructor has us add for the copyright at the bottom of the page..I forgot the semicolon after the date("Y"); Clearly all it takes is for one character or symbol to be wrong in the PHP for the page not to load at all...like she said, PHP is way more unforgiving than HTML!