Uploading files to a remote server via html and javascript

I am trying to upload files via an html page on our unix-based server, but I do not know how to take the files on a remote server and save the files there.

I am writing the following code, please help me link it.

<html> <head> <script type="text/javascript"> function Upload() { var filename = document.getElementById("filename").value; var storepath = "HOSTURL/Foldername"; } </script> </head> <body> <form action="" method="post" enctype="multipart/form-data" > <input type="file" name="filename" /> <input type="submit" value="Upload" onclick="Upload" /> </form </body> </html> 
+6
source share
2 answers

Why use javascript? You can simply use the html form to publish your file on the server:

 <html> <body> <form action="/foo/bar.ext" method="post" enctype="multipart/form-data"> <input type="file" name="filename" /> <input type="submit" value="Upload" /> </form> </body> </html> 

Change the action form to the location where you want to send the file.

+2
source

PHP would be the best choice for this.

 <?php if( isset( $_POST["Upload"] ) ) { $target_path = "uploads/"; $target_path = $target_path . basename( $_FILES['filename']['name']); if(move_uploaded_file($_FILES['filename']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['filename']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } } ?> <form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" > <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> <input type="file" name="filename" /> <input type="submit" value="Upload" name="Upload" /> </form> 
+2
source

Source: https://habr.com/ru/post/927114/


All Articles