Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.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 将映像目录插入MySQL_Php_Mysql - Fatal编程技术网

Php 将映像目录插入MySQL

Php 将映像目录插入MySQL,php,mysql,Php,Mysql,我在让PHP将图像路径或目录插入MySQL数据库时遇到问题。它只存储图像名称,而不是图像的路径和名称。此外,我正在重命名图像,以匹配它存储在下的项目的标题(例如,蓝色汽车-标题,蓝色汽车-图像名称)。它只存储原始图像名称,而不是上载新图像名称 这是我用于重命名和上载图像等的代码: // The directory that will recieve the uploaded file $dir = 'uploads/'; //variables for images $allowedExts

我在让PHP将图像路径或目录插入MySQL数据库时遇到问题。它只存储图像名称,而不是图像的路径和名称。此外,我正在重命名图像,以匹配它存储在下的项目的标题(例如,蓝色汽车-标题,蓝色汽车-图像名称)。它只存储原始图像名称,而不是上载新图像名称

这是我用于重命名和上载图像等的代码:

// The directory that will recieve the uploaded file
$dir = 'uploads/';

//variables for images
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["picture"]["name"]);
$extension = end($temp);        

if(isset($_POST['submit'])) {
    if (strlen($title)>0 && strlen($description)>0) {
            move_uploaded_file($_FILES['picture']['tmp_name'], $dir . "/" . $title ."." . $extension);


            // Query database and insert data into item table
            $itemQry = 'INSERT INTO items (title, picture, startPrice, category, description, quantity, location, sellingFormat, duration, paymentType, postageDetails)
                        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
            $statement = $conn->prepare($itemQry);
            $statement->bind_param('sssssssssss', $title, $_FILES['picture']['name'], $startPrice, $category, $description, $quantity, $location, $sellingFormat, $duration, $paymentType, $postageDetails);
            $statement->execute();
试试这个:

// The directory that will recieve the uploaded file
$dir = 'uploads/';

//variables for images
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["picture"]["name"]);
$extension = end($temp);        

if(isset($_POST['submit'])) {
    if ((strlen($title) > 0) && (strlen($description) > 0)) {
        $newFilePath = $dir . "/" . $title . "." . $extension;
        move_uploaded_file($_FILES['picture']['tmp_name'], $newFilePath);

        // Query database and insert data into item table
        $itemQry = 'INSERT INTO items 
            (title, picture, startPrice, category, description,
             quantity, location, sellingFormat, duration, 
             paymentType, postageDetails) 
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';

        $statement = $conn->prepare($itemQry);
        $statement->bind_param('sssssssssss', $title, $newFilePath, 
                        $startPrice, $category, $description, 
                        $quantity, $location, $sellingFormat, 
                        $duration, $paymentType, $postageDetails);

        $statement->execute();
    }
}

注意:这里我使用了
$newFilePath
作为一个新变量,它保留路径和新文件名,并使用它们插入到数据库中。希望这对您有所帮助:)。

它只存储图像名称,因为这是您告诉它要执行的操作。$\u文件['picture']['name']将只返回图像的名称(例如file.jpg)。如何更改此名称?请不要仅使用扩展名进行检查。改用mime类型。在移动上传的文件后,您需要通过
$file\u path=$dir获取路径。basename($_文件[“图片”][“名称])然后保存$file\u路径非常好,非常感谢!!很高兴这有帮助!:)但是是的,对于@Akin,“请不要只使用扩展名来检查,而是使用mime类型。您拥有的内容很容易被欺骗”。照顾好这些东西。