I am trying to upload an image to server (I am using Google Cloud Virtual Machine) that the PHP gets from a Python script (I intend to retrieve the images from a Raspberry Pi camera sensor, but for now, I am sending a placeholder image to test the functionality of my script) that sends the image along with other parameters through a POST request. However, while the image does get uploaded, it does not get moved to a new location as it is shown in the Output I have provided below along with the code.
A question found here: php uploading a file says it is working but does not actually upload a file does have a similar problem, however the suggested solution there did not help me.
Here's the code that implements the file upload system.
PHP Upload File code:
//Upload the image
$uploadDir = "/home/username/directory/";
$uploadFile = $uploadDir . basename($_FILES["myimage"]["name"]);
if(is_uploaded_file["myimage"]["tmp_name"])
{
echo "File has been UPLOADED
";
if(move_uploaded_file($_FILES["myimage"]["tmp_name"],$uploadFile))
{
echo "File has been MOVED";
}
else
{
echo "File has NOT been MOVED";
}
}
else
{
echo "File has NOT been UPLOADED";
}
Python code which sends a request to the PHP above:
import requests
def postData(directory):
activity = "Someone walked in"
url = 'http://35.198.114.146/index.php/sensor_readings/postdata?activity=' + activity
image = {'myimage': open(imagePath,'rb')}
r = requests.post(url,files=image)
print(r.text)
imagePath = 'cam_images/test.png'
postData(imagePath)
Output:
File has been UPLOADED
File has NOT been MOVED
I have been stuck on this problem for weeks and I am not sure on what to do. Any kind of help is appreciated. Should you need more details or code, feel free to ask me.
Thank you in advance.