php - 用户可以提交带有照片的数据。但是我这里无法查看图片内容。如何使用mime查看这里

标签 php postgresql mime-types

    <?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))  
{
if ($_FILES["file"]["error"] > 0) 
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
} 
else 
    {
    $imagepath = "propertyimages/".(rand()) . $_FILES["file"]["name"];
    move_uploaded_file($_FILES["file"]["tmp_name"], $imagepath);

    $db = pg_connect("host=localhost port=5432 dbname=ohms user=postgres password=padmin");
     $s="select dcode from dept where dname='$_POST[select]'";   
      $s1=pg_query($s);  
    $row=pg_fetch_array($s1);
     $row[0]; 
    $r = pg_fetch_row($s1);
    $query = "INSERT INTO nsr VALUES ('$_POST[text1]','$_POST[text2]',  
    '$_POST[textarea]','$row[0]','$_POST[textfield3]', '$_POST[text5]','$_POST[text6]','$_POST[text7]','$_POST[text8]','$_POST[textfield]','$_POST[text10]','$_POST[ref_phno]','$d','".$imagepath."')";  
    $result = pg_query($query);
}}

tish 是一个注册页面的代码。用户可以用图像提交数据。这里用户可以提交各种图像扩展名。但是想要插入内容为图像的主机图像。而不是带有 .jpg 扩展名的 txt 如何这样做。请帮忙

最佳答案

您可以在 PHP 中使用 getimagesize 函数。如果文件是有效图像,前两个参数将是宽度/高度。您可以检查这些数字是否可接受(例如,这将接受宽度/高度在 128-2048 像素之间)。

move_uploaded_file之后,插入这段代码:

$arr = getimagesize( $imagepath );
if( $arr[0] >= 128 && $arr[1] >= 128 && $arr[0] <= 2048 && $arr[1] <= 2048 )
{
  // valid image
}
else
{
  // error
}

注意:您应该使用内置的 pathinfo 函数来获取扩展名:pathinfo( $name, PATHINFO_EXTENSION )

关于php - 用户可以提交带有照片的数据。但是我这里无法查看图片内容。如何使用mime查看这里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28372943/

相关文章:

python - 注释导致 ProgrammingError : must appear in the GROUP BY clause or be used in an aggregate function

android - 某些 Android 手机无法打开自定义文件扩展名

python - 带有 ;charset=UTF-8 的 MIME 类型的文件扩展名

php - 在 php 中回显变量的更短方法?

php - 使用 DOJO 自动完成文本框

php - Symfony 2 附加 Javascripts,只是一个请求

postgresql - 在 OpenJPA 中使用 VARCHAR 或 TEXT 作为默认字符串映射

postgresql libpqxx 多个查询作为一个事务

macos - MIME 类型在 OS X 10.5.8 中不显示

php - 冲洗甚至 ob 冲洗不起作用