【发布时间】:2017-03-23 08:29:00
【问题描述】:
我有一个问题。我需要使用 PHP 从文件夹中的图像中获取图像尺寸。我知道这可能是使用 PHP 函数 getimagesize 执行此操作的最佳方法,但我不知道将这段代码放在我的脚本中以保存到数据库的位置。你能帮帮我吗?
这是我的 PHP 脚本:
<?php
$server = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'dbname';
$connect = mysql_connect($server,$dbuser,$dbpass);
mysql_select_db($dbname,$connect);
$path = "gb-bilder-pics/augen/";
$files = array_map('mysql_real_escape_string',array_filter(glob("{$path}*.*"),'is_file'));
if(empty($files)){
echo "There were no matching files to insert into the database.";
}else{
$insertValues = array();
foreach($files as $file)
{
$insertValues[] = "('Titel', 'augen', '{$file}', '{$width????}', '{$height????}')";
}$query = "INSERT INTO `gbpics` (`gbpictitel`, `gbpiccat`, `gbpicurl`, `gbpicwidth`, `gbpicheight`) VALUES " . implode(', ', $insertValues);
if(!mysql_query($query)){
echo "There was a problem inserting the data.";
trigger_error("Query failed: $query<br />Error: " . mysql_error());
} else {
echo "The data was inserted successfully.";
}
}?>
【问题讨论】:
-
你用谷歌搜索过“php 图像大小”吗? MySQL 扩展在 PHP 5.5.0 中被弃用,并在 PHP 7.0.0 中被删除。
-
是的,我用谷歌搜索过,我使用的是 php 5.6,所以没关系
-
或者有其他选择吗?当我切换到 php 7 时?
-
getimagesize 根据 PHP.net 在 PHP 7.0 上可以正常工作。来源:php.net/manual/en/function.getimagesize.php
-
显而易见的替代方法是使用 php 版本 7 并将您的代码移植到更安全的 mysql 适配器
mysqli或PDO。无论如何你都必须这样做,所以现在就开始。
标签: php mysql dimensions getimagesize