Javascript 如何将文件从localhost发送到网站

Javascript 如何将文件从localhost发送到网站,javascript,php,curl,Javascript,Php,Curl,我想发送一个预选文件(/tmp/test.txt)到一个网站(上传站点)并获得响应(显示上传文件的链接) 我在谷歌上搜索了这么多,但都没有成功 下面的简易html表单正在运行 <html> <body> <form action="http://upload-site/upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="testfile

我想发送一个预选文件(/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表格要求我选择一个文件…我不想要的

它必须被预先选择,因为我想自动化上传过程,并处理给我文件链接的响应


如何在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);
?>


我从未尝试过从本地主机到网站(服务器)。试试看,让我知道你对此的反馈。

你必须使用不同的方法将文件发送到其他网站。您正在通过服务器传输文件(技术上是通过两台机器)。你可以通过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
中执行类似操作,这是你的表单操作

//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>";
您可以阅读您认为的最佳重定向方法


我希望这有帮助

没有看到您的
upload.php
很难帮助您,但是如果您想将所选上载文件复制到服务器上的目标位置,然后在浏览器中显示,您需要在文件
upload.php
中执行类似操作,这是您的表单操作

//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>";
您可以阅读您认为的最佳重定向方法


我希望这有帮助

自动填充文件输入,除非您有某个包含文件信息的用户触发事件(如将文件拖到浏览器窗口中)。但是,如果您在localhost上运行Web服务器来呈现表单,那么该Web服务器应该可以访问本地文件系统。不用向远程
upload.php
脚本发送HTML表单,您可以将本地脚本发送到远程脚本,并输出它返回的任何内容。是的,我想一些CURL请求就可以了,但我发现所有这些都不适用于我。“数据类型错误”或未发生任何错误:/auto填充文件输入,除非您有某个包含文件信息的用户触发事件(如将文件拖到浏览器窗口中)。但是,如果您在localhost上运行Web服务器来呈现表单,那么该Web服务器应该可以访问本地文件系统。不用向远程
upload.php
脚本发送HTML表单,您可以将本地脚本发送到远程脚本,并输出它返回的任何内容。是的,我想一些CURL请求就可以了,但我发现所有这些都不适用于我。要么是“错误的数据类型”错误,要么什么也没有发生:/i没有外部网站-因此我无法编辑upload.php。它的jsut类似于imgur.com,您可以将文件发送到其中,并将创建的图像的链接作为响应提供给您。但我的网站没有api。我发布的表单就是您需要发送的全部内容——因此我想我需要使用curl/php来完成它——但我发现的一切都不适合我。但为你的努力大thx!我没有自己的外部网站-所以我不能编辑upload.php。它的jsut类似于imgur.com,您可以将文件发送到其中,并将创建的图像的链接作为响应提供给您。但我的网站没有api。我发布的表单就是您需要发送的全部内容——因此我想我需要使用curl/php来完成它——但我发现的一切都不适合我。但为你的努力大thx!FTP加密可能(在这种情况下很可能不会)是一种可能性…FTP加密可能(在这种情况下很可能不会)是一种可能性。。。
header("Location: http://example.com/thefile.txt") or die("Failed!");