我想将预选的文件(/tmp/test.txt)发送到网站(上传站点)并获得响应(显示指向上传文件的链接)。
我在Google上搜索了很多内容,却没有使它正常工作。
以下简单的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中做到这一点?
答案 0 :(得分:1)
您必须使用其他方法将文件发送到另一个网站。您正在通过服务器(技术上是在两台计算机上)传输文件。您可以通过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);
?>
我从未尝试过从本地主机到网站(服务器)。尝试一下,让我知道您对此的反馈。
答案 1 :(得分:1)
在没有看到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>";
我认为您需要对此进行更改
<input type="file" name="testfile" value="select file">
对此
<input type="file" name="testfile" id="testfile" value="select file">
您会注意到即时消息使用JavaScript将浏览器重定向到新上传的文件,您也可以使用本机PHP发送新的HTTP标头。
header("Location: http://example.com/thefile.txt") or die("Failed!");
您可以阅读有关您认为重定向here的最佳方法的信息。
我希望这会有所帮助吗?