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.