/    Sign up×
Community /Pin to ProfileBookmark

How to access and delete a file in javascript

Hi ,

My requirement is to access a file from my javascript funtion.

Let us assume , I have the following javascript function :

function deleteFile()
{

var file = “dest.txt” ;

}

Now this file is inside a directory in my present directory.
My present directory say, /home/myuser/
The dest.txt file is under a directory my_dir in my present directory.

How can I then access it as /home/myuser/my_dir/dest.txt and delete the same.

Please help. Thanks

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@George88Aug 28.2012 — Not possible. This is a PHP question, you'll need to ask on those forums.

You also want to look into the unlink() function. For example [FONT="Courier New"]unlink("dest.txt");[/FONT]

[code=php]if (is_file($path))
{
unlink($path):
}[/code]

If you need to have a button send a URL parameter to the PHP file, you can do so like this:
[code=php]$path = $_GET['some_path'];

if (is_file($path))
{
unlink($path):
}[/code]

<a href="path_to_file.php?some_path=/home/myuser/my_dir/dest.txt">Delete</a>
This is untested, so forgive me if this link produces an error. I can't actually remember off the top of my head whether this is automatically converted to a URL safe string. I believe it is, but if it isn't then you'll just need to run the path through the JavaScript encodeURI function and pass that in as a variable with the [i]href[/i] property of the [i]<a>[/i] element.
Copy linkTweet thisAlerts:
@senisevenAug 28.2012 — If your file access operations are on the client-side, as a client-side script system, Javascript NEVER has access to the filesystem, which includes reading, writing and deleting files.

This is obvious: if you download an HTML file from an untrusted site and it has script function "delete_all_files_on_machine_downloading_this_document()", it would be a nightmare. Same thing for scripts requesting all your files be uploaded and sent to the site.

Now, having said that, there are ways to have scripts access the file system for doing exactly what you want. You usually have to grant some kind of permission as a user loading the web document containing the script, and then it runs. You can also use signed scripts. If your system is Windows, Microsoft JScript has filesystem access functions. I don't know if Mozilla still has file system functions as well, but you can check their site. You may have to write a lot of cross-browser code.

=======

If the file manipulations you want to do are on the computer running a web server, then follow what the other poster said: use PHP. PHP scripts work with files and your web server to customize and deliver documents the way you want them.
×

Success!

Help @myuser spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...