Is there a way that i can get all the files and directories on the server using js?
Lets say there is a folder on the server called Files, inside the files folder, there are other folders and files but not a set value and can change constantly. Is there a way to scan the Files folder?
you can achieve this wiht a server side script, for example PHP
http://php.net/manual/en/function.scandir.php
this function returns the dirs, you could nested to get all the dirs and files in directories
then you can return to the javasript with
<? echo json_ecnode($array_with_dirs);?>
with an ajax Request
If you are asking about client-side JavaScript only, no, you cannot do that. You can read URLs using Ajax. If a URL corresponds to a file, you've read the file. If a URL corresponds to a directory and the server responds server-generated index, you could parse that index and recursively read files and indices. The "crawler" programs used by, e.g. Google, employ a similar technique, but do not depend on server-generated indices; they just follow links.
If the files you ask about are not accessible to the web server program, i.e. outside the server's document root, then you cannot read them using only client-side code.
Related
I'm building a flask app called Neuroethics_Behavioral_Task.
I also have an s3 bucket called neuroethics-task. In the root directory, I uploaded a file called experiment.js and an image, test.png.
I followed the instructions in these two parts of Heroku's documentation about s3:
https://devcenter.heroku.com/articles/s3
https://devcenter.heroku.com/articles/s3-upload-python
The first link says the following about how to access assets you've uploaded to s3
After assets are uploaded, you can refer to their public URLs (such as http://s3.amazonaws.com/bucketname/filename) in your application’s code. These files will now be served directly from S3, freeing up your application to serve only dynamic requests.
So I have this line in the header of one of the html templates.
<script href="http://s3.amazonaws.com/neuroethics-task/experiment.js"></script>
I also tried to include the image from copying the path directly on s3 (which is different from the heroku docs). Here's that line.
<img src='s3://neuroethics-task/test.png'
The issue is that nothing happens when I access the web page that's supposed to use the javascript from experiment.js currently when I'm trying to use the Flask application LOCALLY.
I suspect that maybe things will work if I push to heroku... But I need to get a local debugged solution up and running first and foremost. So i need to figure out how to correctly reference these files.
I had gotten error messages before when I used src= and when I had variants of the url's prefix. But now, nothing happens when I get to the webpage that's supposed to load experiment.js. experiment.js uses a javascript framework called JsPsych that basically works like a static application -- no redirects occur from jspsych. You have to create an html template for flask's sake, but all you have to do for that template is include the reference to the experiment.js file.
Since experiment.js just isn't loading yet, and since there's no other html on that template because all of it is within experiment.js, nothing happens.
I have my environmental variables set:
$ export AWS_ACCESS_KEY_ID=jhdfshfjskdhfj
$ export AWS_SECRET_ACCESS_KEY=jlsjfklksfjlfh
I'm not sure about what permissions settings I need on s3. For my bucket, I have
Block public access to buckets and objects granted through new access control lists (ACLs) -- Off
Block public access to buckets and objects granted through any access control lists (ACLs)-- Off
Block public access to buckets and objects granted through new public bucket or access point policies -- On
Block public and cross-account access to buckets and objects through any public bucket or access point policies -- On
So... what's going wrong here? I just want my javascript to load at least.
I am creating a local web application where I want to be able to clear the content of my directory.
How can I remove the files from a directory without deleting the directory itself?
I need it to be empty.
Is it better to remove the whole dir and create it again?
If javascript can delete directory contents, that would a pretty big security concern. Javascript by itself cannot delete system contents. You can make an AJAX call to the server which in turn can delete the files
For deleting the files of your local system you need a server-side scripting language. Vanilla Js cannot do that for you as it is a client-side scripting language.
If you are using Nodejs, as it is a server side you can definitely do that using unlink() for deleting file asynchronously or use unlinkSync() for deleting file synchronously you can learn about how to use that function here...
Demo code unlinkSync() -
const fs = require('fs');
let filename = "D:\\temp\\temp.zip";
fs.unlinkSync(filename);
I am trying to run a script that will run every 5 minutes in a shared hostings wordpress folder that will rename the newest CSV file in that folder.
/wp-content/csv/sample.csv
I tried putting a js file in the folder within that folder and run it.
var fs = require('fs');
function runClear()
{
fs.readdir("", (err, files) => {
files.forEach(file => {
console.log(file);
});
})
}
runClear();
setInterval(runClear, 300*1000);
However, it seems like I got client side and server side scripting confused. It seems like I need node.js.
What would be the best approach for this?
Regards,
Yes you are right you are confused in client side and server side script.
Javascript is a client side script which deal with all the user interactions like what will happen user click something or submit a form, hover over some element, scroll the web page etc.
Where as server side script like php deals with data stored on server like mysql records or the physical files.
what you are trying to do is to change the server resource from client side script. and you can not do that directly.
Instead you can call an ajax function which send an HTTP request to some script placed on server. And in that server script write the code to read the existing files in a directory and rename them using file handling operations.
On my site I have my resources folder outside of the root, for example:
/var/www/html/ is the root directory
/var/www/resources/
I currently have a config file that sets the location of the library so I can include it with php like so:
defined("LIBRARY_PATH")
or
define("LIBRARY_PATH", realpath(dirname(__FILE__) . '/library'));
which works perfectly when I use:
<?php include_once(LIBRARY_PATH . "/file.php"); ?>
but it doesn't work when trying to add Javascript files:
e.g.
<script src="../resources/library/js/test.js"></script>
links to 'www.website.com/resources/library/js/common.js'
or
<script src="<?php echo LIBRARY_PATH; ?>/js/test.js"></script>
links to 'www.website.com/var/www/resources/library/js/test.js'
neither of which work.
Any suggestions on how I can do this without having the js files in or above the root?
Your JavaScript files have to be accessible to the browser because they are executed by the browser and not by the server.
This requires that they have a URL.
Putting the files under the webroot is the standard way to give a static file a URL.
Alternatively, you could write a program (e.g. in PHP) that will read the file and then output it's content to the browser. This is more complicated and makes dealing with cache control headers more fiddly and is not recommended.
Assuming you understand what you're doing and security implications of that!..
You create the linkjs.php script that takes the relative path to the script (from some root dir, perhaps /var/www/resource/js) as a parameter, like:
<script src="/linkjs.php?p=test.js">
In your PHP script you resolve the full file path, check that it's indeed a file under the root dir (to protect against ../ in the parameter), that it's readable by you PHP user, read the content and output it into the response. Don't forget to set content type to text/javascript of course.
Ideally, you should also provide proper caching headers based on the source file modification time, but that is a topic in itself. See the guidelines in other SO questions about proper caching headers for dynamic content.
The upside is that you can do on-the-fly script minification/combining/wrapping/substitutions if you like/need.
I was wondering if it is possible in JS to open a directory, read an image file and display it to Html? I believe JS restricts from being able to open any file in a directory directly, but what I want is:
I have a XML file which will contain the path to a image file in the web server root folder
so my hierarchy is like this
webserver root folder--->|
html
js
css
images
xml
, and I will use XmlHttpRequest and feed the directory tag and file name tag to my JS file which has to display the image to my frame in the Html page.
[My image is also in the same webserver root folder but in a different folder from html]
Any pointers on how to go about it? I guess we can store the image file also in XML as a base64 encoded data, but that would make the data exchange huge, also don't know if this is a ideal method (is it? please suggest)
Please give me some tips for this.
Thanks
Balaji R
JavaScript does not have access to filesystem on server, since it runs on the client side.
But with JavaScript or Ajax you can call some php code on server which will read the image from the file system and then it will pass this image back to the JavaScript.
I have described here how to do this.
If I am following you correctly, example.com/js/somefile.js is trying to access something like example.com/images/image.jpg?
If so then i would either use the absolute URL of the image:
"http://www.example.com/images/image.jpg" or the relative path "../images/image.jpg"
When referencing the images in your code you could actually use a plain text file, one image path per line. Then in your onreadystatechange function:
pictures = var.responseText.split("\n");
now pictures is an array of picture paths.
JavaScript only has access to the information & priviledges that the browser has access to, so unless the image is in a directory that would normally be accessible on the web site, you're not going to have much luck using just JavaScript.
Is there any way that you can make the path in the filesystem available to the web document root folder? Maybe by using an Alias or Symlink?