gpt4 book ai didi

php - 将图像路径存储到 MySQL 时遇到问题

转载 作者:行者123 更新时间:2023-11-29 12:29:51 25 4
gpt4 key购买 nike

我无法理解将图像文件路径存储到 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;

这是我的错误消息。

注意: undefined variable :/useradmin-processor.php 第 45 行中的路径

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

注意: undefined variable :/useradmin-processor.php 第 59 行中的路径

// 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);
}

最佳答案

这有效。

我添加了它,现在一切正常了。我还更改了连接数据库的方式。我确信它可以做得更好,因为我现在已经在代码中保留了这一点。 $params = array();

// 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);
}

关于php - 将图像路径存储到 MySQL 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27726125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com