簡體   English   中英

如何將文件從本地主機發送到網站

[英]How to send a file from localhost to website

我想將預先選擇的文件 (/tmp/test.txt) 發送到網站(上傳站點)並獲得響應(顯示上傳文件的鏈接)。

我用谷歌搜索了很多,但沒有讓它工作。

以下簡單的 html 表格正在工作。

<html>
<body>

<form action="http://upload-site/upload.php" method="post" enctype="multipart/form-data">

  <input type="file" name="testfile" value="select file" >
  <input type="submit" value="Submit">

</form> 
</body>
</html>

所以我只需要發送enctype和帶有信息“testfile = test.txt”的文件我猜?

ofc 的形式要我 select 一個文件......我不想要什么。

必須預先選擇它,因為我想自動化上傳過程並使用為我提供文件鏈接的響應。

我如何在 php/curl/js 中做到這一點?

您必須使用不同的方法將文件發送到另一個網站。 您正在通過服務器傳輸文件(技術上超過兩台機器)。 你可以通過 FTP 來做到這一點。 幸運的是,PHP 為您提供了此功能。

<?php
$file = 'somefile.txt'; // Path of the file on your local server. should be ABSPATH
$remote_file = 'somefile.txt'; // Path of the server where you want to upload file should be ABSPATH.

// set up basic connection
$conn_id = ftp_connect($ftp_server); // $ftp_server can be name of website or host or ip of the website.

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
 echo "successfully uploaded $file\n";
} else {
 echo "There was a problem while uploading $file\n";
}

// close the connection
ftp_close($conn_id);
?>

我從未嘗試過從本地主機到網站(服務器)。 試試看,讓我知道您對此的反饋。

沒有看到您的upload.php很難幫助您,但是如果您想將選定的上傳文件復制到服務器上的目標位置,然后在瀏覽器中顯示它,您需要在文件upload.php中執行類似的操作。你的 forms 動作。

//Get the file name
$file_name = $_FILES["testfile"]["name"];
//Create destination path
$dest = "/path/to/where/you/want/the/file/" . $file_name;
//Move the file
move_uploaded_file($_FILES["testfile"]["tmp_name"], $dest)
//View the file
echo "<script> location.href='http://someip/$dest'; </script>";

我認為你需要改變這個

<input type="file" name="testfile" value="select file">

對此

<input type="file" name="testfile" id="testfile" value="select file">

You will notice im using JavaScript to redirect the browser to the newly uploaded file, you can also just send a new HTTP header using native PHP.

header("Location: http://example.com/thefile.txt") or die("Failed!");

您可以在此處閱讀您認為的最佳重定向方法。

我希望這有幫助?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM