[PHP]从本地主机发送文件到网站

问题描述 投票:0回答:2

我想将预选的文件(/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中做到这一点?

javascript php curl
2个回答
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
投票

没有看到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的最佳方法的信息。

我希望这会有所帮助吗?

© www.soinside.com 2019 - 2024. All rights reserved.