0

I am posting a text file from python script to PHP from where I want to store them in a directory. here is my python code from where i am posting file

import requests

url = 'http://localhost:80/project/php/files.php'
file_path = 'C:\\wamp64\\www\\project\\python\\New folder\\directory_listing.txt'

with open(file_path, 'rb') as file:
    files = {'file': file}
    headers = {'Content-Type': 'multipart/form-data'}
    response = requests.post(url, files=files, headers=headers)

print(response.text)

below is my php code which is receiving the file and then moving it to specified folder

<?php
$target_dir = "files_list/";
if (isset($_FILES["file"])) {
    $target_file = $target_dir . basename($_FILES["file"]["name"]);

    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
        echo "The file " . basename($_FILES["file"]["name"]) . " has been uploaded and moved.";
    } else {
        echo "Error uploading file.";
    }
} else {
    echo "No file uploaded.";
}
    

       

?>

my issue is when i run my code i get No file uploaded. as output. I have already verified all path and filenames these all are ok. I don't know why my file is not receiving at php side.If anyone has any idea kindly guide me what is wrong.

sam
  • 309
  • 2
  • 9
  • 1
    Do not set the Content-Type header yourself (it is likely to be missing the necessary `boundary` part then), let the library handle that on its own. – CBroe Aug 16 '23 at 08:38
  • I removed the header part but still same issue – sam Aug 16 '23 at 08:40
  • 1
    Do a `var_dump($_FILES);` then, and see what that gets you. – CBroe Aug 16 '23 at 08:44
  • You can also print the request body on the Python side (as shown in the linked answer). – Olivier Aug 16 '23 at 08:47
  • i did `var_dump($_FILES);` this what i get `C:\wamp64\www\project\php\files.php:3: array (size=0) empty` – sam Aug 16 '23 at 09:17

0 Answers0