-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 "fileToUpload=@null.txt" 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.

  • According that post, the ans is to apply -F. I'd tried like this `curl -X POST -F "file=@null.txt" http://127.0.0.1/upload.php` and got the same error. But still thanks for your reply. – Carlton Chen Aug 27 '23 at 08:41
  • What error did you get from the curl command? Are there any errors on the php side too (e.g. in log files)? – ADyson Aug 27 '23 at 08: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. – Carlton Chen Aug 27 '23 at 08:53
  • Thanks. `-F "id=fileToUpload"`...this doesn't look like a filename from your device – ADyson Aug 27 '23 at 09: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 https://www.php.net/manual/en/features.file-upload.errors.php – ADyson Aug 27 '23 at 09:25
  • Are your php files stored in the `html` folder? If so then `uploads` is not a subfolder of that, so your path is wrong. Try `$target_dir = "../uploads/";` instead. – ADyson Aug 27 '23 at 09:49
  • Wow, you are amazing. It's indeed caused by the path error. You save my day, very thanks a lot. – Carlton Chen Aug 27 '23 at 09:58
  • No problem. Read https://phpdelusions.net/articles/paths for discussion on how to use paths correctly and get it right reliably – ADyson Aug 27 '23 at 10:02

1 Answers1

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      
E_net4
  • 27,810
  • 13
  • 101
  • 139
hanshenrik
  • 19,904
  • 4
  • 43
  • 89
  • 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. – Carlton Chen Aug 27 '23 at 09:30
  • problem solved. thanks for the help. – Carlton Chen Aug 27 '23 at 10:07
  • @CarltonChen for what it's worth, your question is much better now with the edits :) good job fixing it! – hanshenrik Aug 27 '23 at 10:56