7

我正在尝试通过我们基于 unix 的服务器中的 html 页面上传文件,但我不知道如何在远程服务器上获取文件并在那里保存文件。

我写了以下代码,请帮我连接它。

<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>
4

2 回答 2

2

为什么使用 JavaScript?您可以简单地使用 html 表单将文件发布到服务器:

<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>

将表单更改为action您要将文件发布到的位置。

于 2012-10-06T21:17:30.743 回答
2

PHP 将是一个更好的选择。

<?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>
于 2012-10-06T21:23:46.030 回答