简体   繁体   中英

Upload image to php database with python script

In a part of a bigger app, I would like it that (on a click of a button that I will do later) in my python script, it sends a specific image to a PHP (postgresql) server accessible with an IP address (for demonstration purposes, it is http://12.34.56.78/images/upload.php (it's actually different IP).

Right now, I can add an image to my database when I click the link above: it leads me to this page see screenshot here made to displays with this code:

<html>
<head>
</head>
<body style="border:2px solid Black; padding: 20px; border-radius: 8px; background-color: Teal;">
    <center>
        <form action="upload.php" method="post" enctype="multipart/form-data">
            <input type="file" name="upload" />
            <br/><br/>
            <input type="submit" name="submit" value="upload">
            <br/><br/>
            <?php
                echo '<a href = "http://' . $_SERVER['SERVER_NAME'] . '/images/list.php">Return to the Index.</a>';
            ?>
        </form>
    </center>
</body>

However, as said above, instead of manually selecting the file and clicking upload, I want my python script to automatically upload an image to my database.

So that's my python file for the moment:

import requests
url = 'http://12.34.56.78/images/upload.php'
files = {'file': open('The_Great_Archiver.png', 'rb')}
r = requests.post(url, files=files)

My current .php script to upload an image is:

<?php
include "include/connect.php";

if(isset($_POST['submit']))
{
    $ip = $_SERVER['SERVER_NAME'];

    $filename = $_FILES['upload']['name'];
    $filetmp = $_FILES['upload']['tmp_name'];
    $filebase = basename($_FILES['upload']['name']);

    $len = strlen($filebase);
    $arr1 = str_split($filebase, 1);
    $finalfilebase = "";

    $filetypes = array('png','jpg','gif');
    $ext = pathinfo($filename, PATHINFO_EXTENSION);
    if (!in_array($ext,$filetypes)){
        echo 'File Type Invalid. Try Again With An Allowed File Type. <br>';
    }
    else {
        echo 'File Scan Valid';
    }

    for ($i = 0; $i <= $len - 1; $i++){
        if (" " == $arr1[$i] || "?" == $arr1[$i] || "!" == $arr1[$i]){
            $arr1[$i] = "_";
            echo "/!\ Had to modify file name for database.";
        }
        $finalfilebase = $finalfilebase . $arr1[$i];
    }
    echo "<br><br>";

    $dir = "uploads/";
    $final_dir = $dir.$finalfilebase;

    if(in_array($ext,$filetypes) && move_uploaded_file($filetmp , $final_dir))
    {
        echo "Uploading Successful <br><br>";

        $size = getimagesize($final_dir);
        $img_basename = pathinfo($final_dir, PATHINFO_FILENAME);

        $crop_name = $finalfilebase."_crop";
        $ratio = $size[0]/$size[1]; #Width / Height
        if ($ratio > 1) {
            $width = 100*$ratio;
            $height = "100";
        }
        else {
            $width = 100*$ratio;
            $height = "100";
        }
        $src = imagecreatefromstring(file_get_contents($final_dir));
        $dst = imagecreatetruecolor($width, $height);
        imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
        if ($ext = "png") {
            $dst_addr = "previews/$crop_name.png";
            imagepng($dst,$dst_addr);
            echo "$dst_addr<br><br>";
        }
        elseif ($ext = "jpg") {
            $dst_addr = "previews/$crop_name.jpg";
            imagejpeg($dst,$dst_addr);
        }
        else {
            echo "RESIZE ERROR: $finalfilebase";
        }

        #Insert into database
        $query = pg_query($dbh , "INSERT INTO image_name (file_name , file_path, pre_path) VALUES ('$finalfilebase' , '$final_dir' , '$dst_addr')");
        if ($query)
        {
            echo "Image Inserted <br><br>";
            echo '<a href = "http://' . "$ip" . '/images/list.php">Go To Index.</a><br>';
            echo '<a href = "http://' . "$ip" . '/images/img_upload_form.php">Insert Another Image.</a>';
        }
        else
        {
            echo "Insertion Failed <br><br>";
            echo '<a href = "http://' . "$ip" . '/images/list.php">Return To The Index.</a><br>';
            echo '<a href = "http://' . "$ip" . '/images/img_upload_form.php">Try Again.</a>';
        }
    }
    else
    {
        echo "Error While Uploading. Check If Host Is Running. <br><br>";
        echo '<a href = "http://';
        echo "$ip";
        echo '/images/list.php">Go To Index.</a>';
    }

}
else
{   
    echo "Some Error Occurred. Check Code.";
    #echo '<a href ="localhost"></a>';
}

?>

<html>
    <body style="border:2px solid Black; padding: 20px; border-radius: 8px; background-color: Teal; margin: 50px 100px 50px 100px;">
    </body>
</html>

However, when I execute the python code, it loads for the moment and then it's done, but nothing is added in my database. What do I have to change so that my python script automatically uploads the image correctly where other images are already saved?

By the way, here is a screenshot of how the database displays the image I have already uploaded: click here

Thanks for your help!

isset($_POST['submit'])之后未读取,此变量未在python中设置,因此,您必须修改PHP才能删除此测试

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM