duanluanlang8501 2014-05-15 01:09
浏览 109

带有'image / pjpeg'mime类型的图像不能通过PHP使用GD库显示

I’m learning PHP & I’m working on a simple upload and display image exercise out of a Apress PHP book. I’m using the latest Eclipse program, XAMPP, Apache, etc…

The problem I’m having is that the image being uploaded is a JPEG but it is being read as type PJPEG. It pushes my IF statement to the FALSE section with my error messages.

If I change the all the criteria to pjpeg, then for some reason I get weird raw code instead of a picture.

How do I ensure that my image being loaded stays a jpeg or how can I rewrite my code to upload and output a pjpeg and actually display in browser.

UPDATE: the code provided by Jake is working but the image itself is not displaying in browsers. Tested in Chrome, Firefox and IE.

<?php 

error_reporting(E_ALL);

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    if ( isset($_FILES['photo']) && 
        is_uploaded_file($_FILES['photo']['tmp_name']) && 
        $_FILES['photo']['error']==UPLOAD_ERR_OK ){

            if ($_FILES['photo']['type'] == 'image/jpeg'){

                $j = imagecreatefromjpeg($_FILES['photo']['tmp_name']);

                header('Content-Type: image/jpeg'); 

                imagejpeg($j);

                imagedestroy($j);

            } else {
                echo "Uploaded file was not a jpeg. <br />";
                echo "The file ", $_FILES['photo']['name'], " uploaded was a ", $_FILES['photo']['type'];
            }
    } else {
        echo "No photo upload"; 
    }
} else {

?>

<form action="test.php" method="post" enctype="multipart/form-data">
    <label for="photo">User Photo:</label>
    <input type="file" name="photo" />
    <input type="submit" name="submit" value = "Upload a JPEG" />
</form>

<?php } ?>
  • 写回答

2条回答 默认 最新

  • dou12754 2014-05-15 01:15
    关注

    change

    if ($_FILES['photo']['type'] == 'image/jpeg'){
    

    to

    if (preg_match('@^image/[a-z\-]jpeg@',$_FILES['photo']['type']) === true) {
    

    this will match any type starting with 'image/' and ending with 'jpeg' and containing only lowercase letter and - characters

    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办