Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload files on remote server through html & javascript

I am trying to upload files through html page in our unix based server but I don't know how to take the files on remote server & save files there.

I write the following code please help me to connect 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>
like image 971
Saurabh Avatar asked Oct 06 '12 21:10

Saurabh


People also ask

How do you upload a file in HTML?

The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the <label> tag for best accessibility practices!

How is website uploaded to a remote server?

Uploading to the server This directory is effectively the root of your website — where your index. html file and other assets will go. Once you've found the correct remote directory to put your files in, to upload your files to the server you need to drag-and-drop them from the left pane to the right pane.


2 Answers

Why using JavaScript? You can simple use the html form to post your file to 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 form action to the location you want to post the file to.

like image 124
Erwin Avatar answered Oct 05 '22 03:10

Erwin


PHP would be a better 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>
like image 24
Ner0 Avatar answered Oct 05 '22 03:10

Ner0