Difference between reading files client-side and server-side - javascript

What exactly is the difference between accessing a file through HTML (e.g. <img src="xxx.jpg">) or JavaScript, and using PHP to read a file. The file is still just on the server in both cases, isn't it?
What makes using PHP to access the server's file system different to someone just typing in the URL of the file (provided they know what it is)?

javascript is client side and runs in the browser.
php is server side and runs in the server.
note that there are now server side javascript interpreters like node.js or phantom.js

For static files like (images, HTML, etc) there is no difference, if you directly enter the file URI or read it internally and print the buffer through PHP (at this case the file URI will change for sure).
For dynamic files (PHP files), the same concept is true, if you enter the file URI directly you will get the output of that file, and if you read it internally (There are two types: include and read its source). but in most cases, PHP files are designed for execution not for printing the source, therefore, in both cases:
When you try to read static files by PHP its just resource wasting if there is no need to use PHP.

In the two cases you mentioned the file is on the server. If the file is static such as images, you can have its URL directly in the HTML.
In some cases you need to to have a dynamic URL for the content, for example a download service that generates a temporary URL for the content after authenticating the user. In such case you would serve the file using PHP since you do not have a fixed URL for the content

Like you are saying, php might really access the FILE SYSTEM, while a client just can access files the Server handles to them.

Related

How to read file client-side without using a file input?

I'm struggling to know how to read a file (client-side) in JS without using a file input, as I only need to read a local file in my project.
What I want to achieve is to get the content from a .less file in order to put it into a parser of some kind (I need to access its content in a javascript file).
I cannot use "fs" as I'm not using node, but I don't seem to be able to use the FileReader API either as it needs a Blob object which would be obtained by the file input.
Does someone have an idea ?
client side doesn't have access to manage file system, you can read file with input but cannot write or delete from client side. it is only possible with a backend application written in node js or java, from their you can expose those services and call it from client side through http requests.

reading a php file with javascript on the same domain

What I'm trying to do is simple. I have a read.html on the domain and it has a Javascript code in it that will read a php file on the same domain. I was able to get the code of another html file. But when I try to read a php file I only get what I see in the browser.
What I get as output:
admin
What I want:
<?
echo "admin";
?>
JavaScript, running in a browser, can only access data on a server in the same way that any other remote application can (with some additional limitations caused by it being run in a sandbox, since you don't want arbitrary websites to be able to run any old code on your computer).
Since web servers are usually configured to run PHP programs and serve their output to the client, it is the output that your JavaScript program can see. This is generally desirable, server side code often contains confidential business logic and data such as credentials that shouldn't be exposed to just anybody with a browser.
If you want the PHP source code then you will need to either:
provide a different URL that serves the source code instead of running the program (the easiest way to do this is to copy the PHP into a .txt file)
configure your server to serve the PHP script from where it is instead of running it

Javascript Get Page Source (server-side)

Is it possible to get the source of a server-side page from a javascript script? I want to be able to get the source of a page that is in the same folder on the server. Is it possible without using anything other than javascript?
If you want to get the result of the execution of some server side script (often HTML) from javascript you could use AJAX.
If you want to get the source code of some server side script using javascript (such as the C#, Java, PHP, ... code), you can forget about it unless you publish this source code as text file so that the server doesn't try to interpret it and then use AJAX to fetch this text file from javascript. Of course anyone will be able to access it, not only javascript hosted on your site.

With JavaScript is it possible to Read/Write from/to a file on the server

I have a series of JSON Objects I want to save locally on my server. I am attempting to avoid any server-side script like PHP as required per demand of whats being built. I know its a security risk, but that in this case is not a particular worry. So that said is it possible to actually write to a file and or get its contents via javascript with or without the help of a lib such as jquery?
I should mention I am attempting to avoid ActiveX as I know this is an IE only feature and the software we are developing is planned to be Cross Browser supported
So that said is it possible to actually write to a file and or get its contents via javascript with or without the help of a lib such as jquery?
Nope. You will need something running on server side that can receive your JavaScript input and write it to the server.
Internet Explorer's proprietary file writing functionality is for writing local (client-side) files only.
You can read a file using ajax, but without a server side language you cannot write a file to the server.
https://developer.mozilla.org/en/ajax
No. Javascript runs on the client. You need server-side code to access the server's file system.
Client-side JavaScript can only send data to a server, there's no way for it to tell the server what to do with the data.
To save data to a file or db on a server, you'll require a server-side script of some sort (could be server-side JS with Node.js). If all you need is persistent data, you could store some JSON strings in localStorage or in cookies as needed. They wouldn't be shareable that way though.
Yes, you can use AJAX requests in JavaScript without using jQuery. However, jQuery will save you an ungodly amount of time and cross-browser testing.
But, as others have already said, you can't write server files without server code.

Is there any way to do an AJAX call to a file above public_html?

I'm making a script that lets my users open the page, vote for our site, and then get a password to some restricted content on the site. However, I plan on storing the password in a file outside public_html so it cannot be read directly from the source code.
Is there any way to do an AJAX call to a file above public_html? I don't want to AJAX to a file inside public_html that will read the file, it'll just defeat the purpose.
Not directly, no. And, frankly, thank goodness for that (since js is executed client-side, and the client should never have access to the web-server above public_html).
You can, however, use Ajax to call a php script inside the web root that has access to documents outside of the web-root. This way you're still keeping the password out of public reach, but still allowing your users to make use of it.
The down-side is that the password might make it to the client-side in the Ajax call (depending on what your Ajax call does). Basically, if JS can get access to the password then so can any interested user.
No, you cannot do that.
The web server does not allow you to do that.
Also, it is highly insecure to expose access to non public_html files on the server.
No, you can't have an AJAX call to a file that's not served by the web server (I'm assuming the file above public_html doesn't have an apache ALIAS or virtual directory setup).
To accomplish what you're trying to do, create a script (php?) on your site that AJAX calls and this script will either:
Read the password file wherever it is on the system (assuming the file has the correct file permissions)
Embed the password within the script itself since the source code of the script can't be retrieved.
No. An AJAX request is simply a request like any other that loads a resource from your server. The only difference is that it exposes the result to javascript on an already loaded page instead of loading a new page. So if an AJAX request can get this secure file, than anyone can.
You could setup a proxy script in some web application programming language to fetch the file from disk and send it along for you. But then it wouldn't be much different from putting the file right in the public directory.
You may need to rethink your approach here.
Why don't you do an AJAX call to some view function on the server that can access the file you need and then return whatever data to the AJAX request?

Categories

Resources