0

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).

I have googled so much and didnt get it to work.

the following easy html form is working.

<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 ??

the form ofc wants me to select a file...what i dont want.

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 ?

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
maryland
  • 21
  • 3
  • [You can't](https://stackoverflow.com/questions/1696877/how-to-set-a-value-to-a-file-input-in-html) auto-fill a file input, unless you have some user-triggered event that contains the file information (like a file being dragged into the browser window). Then [you can](https://stackoverflow.com/questions/47515232/how-to-set-file-input-value-when-dropping-file-on-page), but not in all (especially older) browsers. – rickdenhaan Sep 29 '19 at 18:00
  • However, if you're running a webserver on localhost to render the form, then that webserver should have access to the local file system. Instead of sending a HTML form to the remote `upload.php` script, you can have a local script [post a CURL request](https://stackoverflow.com/questions/34604602/multipart-form-file-upload-post-php-curl) to that remote script and output whatever it returns. – rickdenhaan Sep 29 '19 at 18:03
  • yeah i guess some CURL request would do it, but all i found didnt work for me. either an error with "wrong data type" or nothing happened :/ – maryland Sep 29 '19 at 18:30

2 Answers2

1

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. Fortunately, PHP provides you functionality for this.

<?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.

Vantiya
  • 612
  • 1
  • 4
  • 11
1

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.

//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.

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

You can read about what you think is the best approach for the redirect here.

I hope this helps?

omeanwell
  • 1,847
  • 1
  • 10
  • 16
  • i dont own the external website- so i cant edit the upload.php. its jsut something like imgur.com, where you send files to and it gives you the link of the created image as a response. but my site does not have an api. the form i posted is all you need to send - so i guess i need to do it with curl/php - but all i found didnt work for me. but big thx for your effort! – maryland Sep 29 '19 at 18:23