简体   繁体   English

如何将文件从本地主机发送到网站

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

I want to send a preselected file (/tmp/test.txt) to a website (upload site) and get the response (shows a link to the uploaded file).我想将预先选择的文件 (/tmp/test.txt) 发送到网站(上传站点)并获得响应(显示上传文件的链接)。

I have googled so much and didnt get it to work.我用谷歌搜索了很多,但没有让它工作。

the following easy html form is working.以下简单的 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>

so i all need to send is the enctype und the file with info "testfile=test.txt" i guess??所以我只需要发送enctype和带有信息“testfile = test.txt”的文件我猜?

the form ofc wants me to select a file...what i dont want. ofc 的形式要我 select 一个文件......我不想要什么。

it has to be preselected since i want to automate the upload procedure and work with the response that gives me the link to the file.必须预先选择它,因为我想自动化上传过程并使用为我提供文件链接的响应。

how do i do that in php/curl/js?我如何在 php/curl/js 中做到这一点?

You have to use different approach to send your file to another website.您必须使用不同的方法将文件发送到另一个网站。 You're transferring file over the server(technically over two machine).您正在通过服务器传输文件(技术上超过两台机器)。 You could do it by FTP.你可以通过 FTP 来做到这一点。 Fortunately, PHP provides you functionality for this.幸运的是,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);
?>

I've never tried from localhost to website (Server).我从未尝试过从本地主机到网站(服务器)。 Give it try and let me know your feedback for this.试试看,让我知道您对此的反馈。

Without seeing your upload.php its hard to help you, but if you want to copy the selected uploaded file to a destination on your server and then display it in the browser you need to do something like this in the file upload.php which is your forms action.没有看到您的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>";

I think you need to change this我认为你需要改变这个

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

To this对此

<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. 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!");

You can read about what you think is the best approach for the redirecthere .您可以在此处阅读您认为的最佳重定向方法。

I hope this helps?我希望这有帮助?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM