Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP图像上传存储为url_Php_Database_Image Uploading - Fatal编程技术网

PHP图像上传存储为url

PHP图像上传存储为url,php,database,image-uploading,Php,Database,Image Uploading,对于一个项目,我需要使用PHP作为后端构建某种拍卖平台。我目前正在尝试使用PHP上传图像,然后将url存储在sql数据库的“Item”表中。我按照这个上传图像,但是,我不知道现在如何连接到我的数据库。我上传图像的代码如下所示: 服务器>图像>上传.php: // Tutorial: https://www.w3schools.com/php/php_file_upload.asp <?php $target_dir = "uploads/"; $target_file = $target

对于一个项目,我需要使用PHP作为后端构建某种拍卖平台。我目前正在尝试使用PHP上传图像,然后将url存储在sql数据库的“Item”表中。我按照这个上传图像,但是,我不知道现在如何连接到我的数据库。我上传图像的代码如下所示: 服务器>图像>上传.php

// Tutorial: https://www.w3schools.com/php/php_file_upload.asp

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size - if more than 500MB, display error
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats (JPG, JPEG, PNG, and GIF)
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>
//教程:https://www.w3schools.com/php/php_file_upload.asp
如前所述,我希望检索图像url并将其存储在“Item”表中。我通过使用构建API来连接数据库。因此,基本上我的“Item”模型如下所示:server>model>Item.php

<?php

include_once '../sql_functions.php';

class Item
{
    // database connection and table name
    private $conn;

    // object properties
    public $ID;
    public $Name;
    public $Description;
    public $AuctionStart;
    public $AuctionEnd;
    public $AuctionFinished;
    public $StartingPrice;
    public $ReservePrice;
    public $FinalPrice;
    public $PhotoURL;
    public $SellerID;

    // constructor with $db as database connection
    public function __construct($db)
    {
        $this->conn = $db;
    }

    // read products
    function read()
    {

        return p_Item_sel_all($this->conn);
    }

    // search products
    function search($search)
    {

        return p_Item_search($this->conn,$search);
    }

    function create()
    {

        // TODO: Check that start date is in the future, and before the end date
        // check reserver price is positive


        // sanitize
        $this->Name = htmlspecialchars(strip_tags($this->Name));
        $this->Description = htmlspecialchars(strip_tags($this->Description));
        $this->AuctionStart = htmlspecialchars(strip_tags($this->AuctionStart));
        $this->AuctionEnd = htmlspecialchars(strip_tags($this->AuctionEnd));
        $this->StartingPrice = htmlspecialchars(strip_tags($this->StartingPrice));
        $this->ReservePrice = htmlspecialchars(strip_tags($this->ReservePrice));
        $this->PhotoURL = htmlspecialchars(strip_tags($this->PhotoURL));
        $this->SellerID = htmlspecialchars(strip_tags($this->SellerID));
//        $this->created=htmlspecialchars(strip_tags($this->created));

        if (p_Item_ins($this->conn, $this->Name, $this->Description, $this->AuctionStart, $this->AuctionEnd, $this->StartingPrice, $this->ReservePrice, $this->PhotoURL, $this->SellerID)) {
            return true;
        };

        return false;

    }

    // used when filling up the update product form
    function readOne()
    {

        $stmt = p_Item_sel_id($this->conn, $this->ID);

        // get retrieved row
        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        // set values to object properties
        $this->ID = $row['ID '];
        $this->Name = $row['Name '];
        $this->Description = $row['Description '];
        $this->AuctionStart = $row['AuctionStart '];
        $this->AuctionEnd = $row['AuctionEnd '];
        $this->AuctionFinished = $row['AuctionFinished '];
        $this->StartingPrice = $row['StartingPrice '];
        $this->ReservePrice = $row['ReservePrice '];
        $this->FinalPrice = $row['FinalPrice '];
        $this->PhotoURL = $row['PhotoURL '];
        $this->SellerID = $row['SellerID '];
    }

    function increment_views()
    {
        return p_Item_incr_views($this->conn, $this->ID);
    }

    function update(){

        // execute the query
        if (p_Item_upd($this->conn, $this->ID, $this->Name, $this->Description, $this->AuctionStart, $this->AuctionEnd, $this->AuctionFinished, $this->StartingPrice, $this->ReservePrice, $this->FinalPrice, $this->PhotoURL, $this->SellerID)) {
            return true;
        }

        return false;
    }

        // delete the Item
    function delete(){

        // execute query
        if(p_Item_del_id($this->conn, $this->ID)){
            return true;
        }

        return false;

    }

}

我的建议是为文件(或图像)定义一些存储路径,并定义该文件夹的url
比如说

define('STORAGE_DIR',dirname(__FILLE__).'/../storage/');
define('STORAGE_url','http://example.com/storage/');

然后,为您的文件创建一些随机名称并将其保存在数据库中。如果你这样做,你就会知道你的任何文件的确切位置和url。

在你的课堂上,你缺少了你的“setter”

您需要将数据库连接器实例化为适当的对象

    public function setDbObject($domain, $user, $pw, $db) {

        $conn = mysqli_connect("$domain", "$user", "$pw", "$db");

        if (!$conn) {
              return false();
        }

        else {

             return $conn;
        }

    }
这将返回您的连接“对象”,然后您可以使用它来运行查询

希望有帮助。

你的问题是什么?(是“如何阻止人们访问我的网站?”)