最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to copy the file from one folder to another using js - Stack Overflow

programmeradmin2浏览0评论

i am facing the problem in coping the images from one folder to another . It is possible through JS means please guide me, i had the image path (eg : C:\Program Files\xampp\htdocs\gallary\images\addnew.gif ) just i want to copy the images to another folder using js .thanks in advance.

i am facing the problem in coping the images from one folder to another . It is possible through JS means please guide me, i had the image path (eg : C:\Program Files\xampp\htdocs\gallary\images\addnew.gif ) just i want to copy the images to another folder using js .thanks in advance.

Share Improve this question edited Nov 6, 2010 at 12:09 fmark 58.7k27 gold badges102 silver badges107 bronze badges asked Nov 6, 2010 at 6:40 MeenaMeena 9675 gold badges19 silver badges35 bronze badges 1
  • Does your JS have chrome privileges? If yes, in which browser? – NikiC Commented Nov 6, 2010 at 9:44
Add a ment  | 

2 Answers 2

Reset to default 6

You cannot use javascript to do this within the web browser. Javascript can only execute code in the browser of the person viewing the web page, not on the web server. Even then, javascript is "sandboxed" for security so it cannot access the users files, etc. Imagine the privacy problems if every webpage you visited had access to your My Documents folder!

PHP, however, can do this on the web server (I assume you have PHP instaled because you have XAMPP in the path to your image). The relevant PHP function is copy:

bool copy ( string $source , string $dest [, resource $context ] )

In your case, you probably want to call it like this:

   success = copy('C:\\Program Files\\xampp\\htdocs\\gallary\\images\\addnew.gif', 'C:\\images\\addnew.gif')
   if (!success){
      echo "Could not copy!"
   }

The simplest way to trigger this file copy is when a PHP web page is loaded. However, if you want to trigger this file-copy via javascript, you might want to look into using an AJAX style technique, where a javascript event sends an HTTP request to your web-server in the background. The web-server can then do the file copy in PHP. If you do take this approach, I would remend that you:

  1. Use a javascript API like jQuery which has built in functions to make this easier.
  2. Be very very careful about security. You don't want someone snooping around on your website to be able to delete or copy arbitrary files.

You could use MS JScript http://msdn.microsoft./en-us/library/e1wf9e7w(VS.85).aspx

fso = new ActiveXObject("Scripting.FileSystemObject");
fso.CopyFile ("c:\\mydocuments\\letters\\*.doc", "c:\\tempfolder\\")

this can't be done from a browser, but you can run it in windows (using the windows script host) directly. You could also do it with node.js (server side javascript) which would be a more cross platform way. If you're trying to do it from in the browser on the client side it is not possible from any language for obvious security reasons.

发布评论

评论列表(0)

  1. 暂无评论