Can you view a PHP file inside a browser using FTP protocol?

  • Thread starter Thread starter Hope
  • Start date Start date
H

Hope

Guest
I have a theory and trying to figure it out.

You load your browser (i.e. Internet Explorer, Firefox, Safari) to view a website (http://example.com). You view a PHP file under the \htdocs directory (http://example.com/htdocs/example.php. The PHP file refers to a database connection file and a CSS file in a different directory. The page displays like you expect it to, in different browsers. The table is populated from the database connection, and the layout is perfect.

Now if you try to access that same site via FTP, your browser typically turns into an FTP session. You can upload/download files using the FTP protocol inside the browser without the need of an external FTP program.

You then try to view the same PHP file by navigating to the \htdocs\ directory and clicking on the file. My question is, should it display with the same layout and table populated?

I've tested this theory with a couple PHP files of my own, and depending on the browser, was asked to open or save the file. Every time, I was faced with errors and an incomplete page is populated, or the page would not display at all. It seems as if it would try to download the into a temporary cache for viewing and the directories and files get screwed up (it would look for /connections/connect.php inside the file and it thinks the root directory is C:\local\ie5\internetcache\ ..... resulting in a file not found error)

So.... am I doing something wrong? Is there something wrong in the PHP code that is preventing seeing the page as it's supposed to in an FTP session? Maybe a setting inside a browser? Should it display the same in FTP as it would if you viewed it via HTTP?

When responding, I would appreciate an explanation of why you say what you say, as opposed to "it doesn't work" answer.

If you do have an argument for it working in FTP, could you please provide a detailed explanation or references?

The best answer will be given to someone who is most detailed in their answer, proving for or against the idea that you are able to view the same PHP file via FTP browser as you would via HTTP.

DO NOT SAY "Use an external FTP program because it's easier". I am trying to test a theory within browser FTP functionality.
 
The short answer is "no". FTP is the "File Transfer Protocol", and is doing exactly as you would expect by asking if you want to download the file.

Your easiest test of this is to just put a .txt file on the server and try viewing it via FTP. You can't do it.

If I'm understanding what you want to do, you want to be able to see your unprocessed (unparsed) PHP in the browser. In essence, you want to tell the server "don't parse this PHP, just show me the script".

By default, you can't do this for security reasons. Many PHP scripts are proprietary code that the author doesn't want you seeing. Other reasons include the database username and password, etc... which is typically has full rights to modify the database, which would be a HUGE security hole.

You CAN, however, view your PHP files using normal HTTP by renaming them to phps I believe... but you only want to do this to reveal the PHP temporarily, for the security reasons I just mentioned.

(Edit: The H in PHP doesn't stand for anything. PHP is not an acronym, and is of dubious origins.)
 
Unfortunately, there isn't really any way to answer this question other than "it doesn't work that way". FTP is for transferring files, not displaying dynamically generated HTML. Why would you want to do something like this?

The H in "PHP" stands for "Hypertext". It works over Hypertext Transfer Protocol (HTTP). The PHP interpreter installed on the HTTP server processes the PHP file if it receives an HTTP request for it. If you make an FTP request for the same file, then it goes to a different port, where a different application (an FTP server) is running. The FTP server will not have a PHP interpreter capable of running the script in a PHP file — that's not what an FTP server is for.

You are doing something akin to putting a CD in an envelope and expecting the envelope to play the music on the CD.
 
Back
Top