简体   繁体   English

使用python脚本将图像上传到php数据库

[英]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). 在更大的应用程序的一部分中,我希望在我的python脚本中(单击稍后将要执行的按钮)将特定的图像发送到可通过IP地址访问的PHP(postgresql)服务器(对于出于演示目的,它是http://12.34.56.78/images/upload.php (实际上是不同的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. 但是,如上所述,我希望我的python脚本自动将图像上传到我的数据库,而不是手动选择文件并单击“上传”。

So that's my python file for the moment: 所以这是我目前的python文件:

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脚本是:

<?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. 但是,当我执行python代码时,它会暂时加载,然后完成,但是我的数据库中什么也没有添加。 What do I have to change so that my python script automatically uploads the image correctly where other images are already saved? 为了使我的python脚本自动将图像正确上传到已经保存了其他图像的地方,我需要更改什么?

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才能删除此测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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