-3

I'd like set a simple HTTP service (with PHP) to receive files from another computer with Linux curl and Windows Powershell. I have read internet resource, including PHP can't upload files to server? and Using cURL to upload POST data with files. These posts help me with parameters issues but not all.

here is my code (refer to here)

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
?>

and here is the command that I used and got error response.

# bash
curl -X POST -F "id=fileToUpload" -F "[email protected]" http://127.0.0.1/upload.php

Here is the /var/apache2/error.log

[Sun Aug 27 05:13:13.392185 2023] [php7:warn] [pid 77733] [client 127.0.0.1:54732] PHP Warning:  move_uploaded_file(uploads/null.txt): failed to open stream: No such file or directory in /var/www/html/upload.php on line 5
[Sun Aug 27 05:13:13.392251 2023] [php7:warn] [pid 77733] [client 127.0.0.1:54732] PHP Warning:  move_uploaded_file(): Unable to move '/tmp/phpynhUuv' to 'uploads/null.txt' in /var/www/html/upload.php on line 5

the uploads status

$ ll   
> total 8
> drwxr-xr-x 2 root     root     4096 Aug 27 05:08 html
> drwxrwxrwx 2 www-data www-data 4096 Jun  2 22:38 uploads

Can anyone tell what's wrong with my code? Any opinions will be appreciated.

P.S. ThanksADyson and hanshenrik generous guidance. This problem is caused in two aspects: (1) use -F for curl command and (2) correct the PHP path to fit my folder setting.

8
  • According that post, the ans is to apply -F. I'd tried like this curl -X POST -F "[email protected]" http://127.0.0.1/upload.php and got the same error. But still thanks for your reply. Commented Aug 27, 2023 at 8:41
  • What error did you get from the curl command? Are there any errors on the php side too (e.g. in log files)? Commented Aug 27, 2023 at 8:41
  • Thanks for reminding, I should have noticed this. I just read the /var/log/apache2/error.log and get some interesting hint. I'll work by myself first to see I can get it done. Commented Aug 27, 2023 at 8:53
  • Thanks. -F "id=fileToUpload"...this doesn't look like a filename from your device Commented Aug 27, 2023 at 9:23
  • Also your php code needs to check $_FILES["fileToUpload"]["error"] before attempting to use move_uploaded_file, to validate that it correctly received a file. See php.net/manual/en/features.file-upload.errors.php Commented Aug 27, 2023 at 9:25

1 Answer 1

0

-d sends the data with application/x-www-form-urlencoded format, which PHP automatically parses into the $_POST superglobal, while your code tries to read the uploaded file from the $_FILES superglobal, which to the best of my knowledge, PHP only parses from multipart/form-data-requests, and to make curl send multipart/form-data requests, use -F

curl -F @null.txt http://127.0.0.1/upload.php      
Sign up to request clarification or add additional context in comments.

2 Comments

I apology for this terrible question, will notice to make my question clear next time. Also thanks for the "-F" reminder, it is helpful with the parameter issue. But it seems I got other issues for me to work with to make it work as expected.
@CarltonChen for what it's worth, your question is much better now with the edits :) good job fixing it!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.