【问题标题】:Trouble storing image path into MySQL无法将图像路径存储到 MySQL
【发布时间】:2015-02-27 20:07:25
【问题描述】:

我无法理解将图像文件路径存储到 MySQL 数据库中。 但它正在存储图像详细信息以及将图像文件移动到目录中。我尝试了很多方法,但没有运气。我是 PHP 新手。

问题: 如何正确捕获图像的路径并将其存储到 MySQL 中?

参考链接: http://www.php-mysql-tutorial.com/wikis/php-tutorial/uploading-files-to-the-server-using-php.aspx

另外:请提出总体上更好的编码实践以供考虑。非常感谢!

//MYSQL TABLE
CREATE TABLE `userimages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL DEFAULT '',
  `type` varchar(30) NOT NULL DEFAULT '',
  `size` int(11) NOT NULL,
  `ipath` varchar(250) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

这是我的错误信息。

注意:未定义变量:第 45 行 /useradmin-processor.php 中的路径

注意:未定义索引:第 55 行 /useradmin-processor.php 中的 ipath

注意:未定义变量:第 59 行 /useradmin-processor.php 中的路径

// IMAGE UPLOAD FILE
if(isset($_POST['imagesupload'])) {
    /*
    Note: The below code will upload the image to the images folder.
    Then store the image name in your database. When you want to show the
    image, just append the image name(taken from database) to the image path
    and paste it in the <img>
    */
    $imageFileName       =   $_FILES["imageFileName"]["name"];
    $tmpImageFileName    =   $_FILES["imageFileName"]["tmp_name"];
    $imageSize           =   $_FILES["imageFileName"]["size"];
    $imageType           =   $_FILES["imageFileName"]["type"];

    move_uploaded_file($tmpImageFileName,"images/".$imageFileName);

    /*
    Note: You can make sure that the images are not overritten by checking
    if there is a different image with the same file name, by using the
    below code prevents overwriting of images due to same Image names.
    You have to store the new Image name in the database.
    */
    $newImageFileName    =   $imageFileName;

    loop1:

    if(!file_exists("images/".$newImageFileName)){
        move_uploaded_file($tmpImageFileName, $path.$newImageFileName);
    } else {
        $newImageFileName .= "_1";
        goto loop1;
    }

    /*
    Note: store the image details into the database and make the connection.
    */
    $params = array(
        ':$path'=>$_POST['ipath']
    );

    $sql = "INSERT INTO userimages ( name, size, type, ipath ) ".
    "VALUES ( '$imageFileName', '$imageSize', '$imageType', '$path' )";

    // the connection to db
    executeSQL($sql, $params);
}

【问题讨论】:

  • 图片路径应该是 TEXT 或 BLOB 而不是 varchar,dev.mysql.com/doc/refman/5.0/en/blob.html
  • 据我所见,$path 是未定义的,一切都由此而来。另外,你想在这里做什么 => :$path,绑定?
  • @Grasper,他在userimages 的行中存储了图像的路径(文件名),而不是图像的内容。 VARCHAR(250) 应该是一个非常好的路径名数据类型。
  • 还有一个问题; $path 定义在哪里;或者为什么没有定义/显示?
  • @JonnyB -- 您已将 ipath 列定义为表中的 blob / 二进制大对象。应该是 varchar()。

标签: php mysql image-uploading imageurl


【解决方案1】:

这行得通。

我添加了它,现在一切正常。我还更改了连接到数据库的方式。我确信它可以做得更好,因为我现在将它挂在代码中。 $params = 数组();

// IMAGE UPLOAD
$uploadDir = 'images/';

if(isset($_POST['imagesupload'])) {

    $imageFileName       =   $_FILES["imageFileName"]["name"];
    $tmpImageFileName    =   $_FILES["imageFileName"]["tmp_name"];
    $imageSize           =   $_FILES["imageFileName"]["size"];
    $imageType           =   $_FILES["imageFileName"]["type"];

    //move_uploaded_file($tmpImageFileName,"images/".$imageFileName);

    $filePath = $uploadDir . $imageFileName;

    $result = move_uploaded_file($tmpImageFileName, $filePath);
    if (!$result) {
        echo "Error uploading file";
        exit;
    }

    if(!get_magic_quotes_gpc())
    {
        $imageFileName = addslashes($imageFileName);
        $filePath = addslashes($filePath);
    }

    $newImageFileName    =   $imageFileName;

    loop1:

    if(!file_exists("images/".$newImageFileName)){
        move_uploaded_file($tmpImageFileName, $filePath.$newImageFileName);
    } else {
        $newImageFileName .= "_1";
        goto loop1;
    }

    $params = array();

    $sql = "INSERT INTO userimages ( name, size, type, ipath ) ".
    "VALUES ( '$imageFileName', '$imageSize', '$imageType', '$filePath' )";
    executeSQL($sql, $params);
}

【讨论】:

  • 感谢@Fred-ii- 和@OllieJones 的帮助!
  • 不客气。很高兴它成功了。 ♫ 新年快乐!
猜你喜欢
  • 1970-01-01
  • 2016-04-15
  • 2012-04-09
  • 2011-04-04
  • 2020-11-08
  • 2014-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多