Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.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 readfile函数以非ASCII字符返回文件内容_Php_Html - Fatal编程技术网

Php readfile函数以非ASCII字符返回文件内容

Php readfile函数以非ASCII字符返回文件内容,php,html,Php,Html,执行以下代码时,浏览器中不显示图像,而是显示非ASCII符号和字符。几行如下: ����JFIF��6Photoshop 3.08BIMgqarIDeY1blFS2krGbsOU��ICC_配置文件LCMSMTRRGB XYZ�)9acspAPPL���-液晶显示器 描述�^cprt\wtpthbkpt | rXYZ�gXYZ�bXYZ�rTRC�@gTRC�@bTRC�@descc2textFBXYZ ���-XYZ 3�XYZ o�8.��XYZ b����XYZ $����曲线��C�K�

执行以下代码时,浏览器中不显示图像,而是显示非ASCII符号和字符。几行如下:

����JFIF��6Photoshop 3.08BIMgqarIDeY1blFS2krGbsOU��ICC_配置文件LCMSMTRRGB XYZ�)9acspAPPL���-液晶显示器 描述�^cprt\wtpthbkpt | rXYZ�gXYZ�bXYZ�rTRC�@gTRC�@bTRC�@descc2textFBXYZ ���-XYZ 3�XYZ o�8.��XYZ b����XYZ $����曲线��C�K�?Q4!�)�2.�FQw]�kpz���|�我�}���0����C$( %2%(,-/0/#484.7./.��C

我做错了什么

<?php
session_start();

if (isset($_SESSION['logSyscuruser'])) {
// logged in
$id = $_SESSION['logSyscuruser'];


$imgLocation = '/Applications/XAMPP/im/';

include "include/specificproductDB.php";
$data = new specificPro();

  $out = $data->select($query, $id, $pId);
  $count = count($out);


}

else
{
  //not logged in
header("Location: loginSystem/login.php", true, 302);
die();
}



?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="content-type">

    <link rel="stylesheet" href="css/main.css" type="text/css" />
    </head>
<body>
<div id="section">
<div id="header">
 <h1>Heading</h1>
</div>
  <p>
<?php
if($count==0)
{
    echo "Nothing added till now.";
}

else
{

for ($x = $count-1; $x >= 0; $x--)
  {

   if(strlen($out[$x][0]) == 68)
   {
    //file is detected
    $imgPath = $imgLocation . $out[$x][0];

    // Make sure the file exists
    if(!file_exists($imgPath) || !is_file($imgPath))
    {
        header('HTTP/1.0 404 Not Found');
        die('The file does not exist');
    }

    $imgData = substr($out[$x][0],65,3);

    // Set the appropriate content-type
    // and provide the content-length.
    header('Content-type: ' . $imgData);
    header('Content-length: ' . filesize($imgPath));

    // Print the image data
    readfile($imgPath);
    }

   else
   {

    header('HTTP/1.0 403 Forbidden');
    die('Fatal Error: Absurd entry in the database media table');
   }

   echo "<br><br>";

   }

  }

  ?>
  </p>

    <input type="button" value="Add Media" onclick="window.location = 'addMedia.php';">    

</div>



</body>
</html>

标题


那么您将头作为内容类型发送的
$imgData=substr($out[$x][0],65,3);
的值到底是什么呢?这就是告诉浏览器文件是图像还是非图像以及那些“非ASCII”文件的原因字符是图像,由readfile以二进制字符流的形式正确发送。它包含图像文件的扩展名或mime类型。但是,除了图像之外,您还向浏览器发送html标记流……浏览器是愚蠢的小软件,如果您不向其发送正确的信息,很容易混淆内容类型值不是扩展名,但您仍然在同一请求中发送html标记和原始二进制图像的混合…您只能发送一个或另一个,但不能同时发送两个