Showing posts with label PHP Mime types. Show all posts
Showing posts with label PHP Mime types. Show all posts

Monday, 3 September 2018

PHP how to use the content type: image / jpeg correctly? advertisements

I've created a page "student_picture.php" using the content-type:image/jpeg. im having problem when I want to add other text in that same page..

here is the sample of my code:
<?php
session_start();

if(!isset($_SESSION["StudentNo"])){
    header("location:login.php");
}

$StudentNo = $_SESSION['StudentNo'];

require("includes/connection.php");

$sql = "SELECT StudentPicture from dbo.Students where StudentNo = '$StudentNo'";
$stmt = sqlsrv_query( $conn, $sql );

if( $stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
    $img = $row['StudentPicture'];

    if ($img == null ) {
        echo "<img src='img/default_pic.gif'>";
    } else {
        $img =  trim($img);
        header('Content-Type: image/jpeg');
        echo $img;
    }

    echo $StudentNo;
}
?>

The image is successfully displaying but the echo $StudentNo is not displaying.. can anyone help me with my prob? thanks in advance.

You might need to use following code :
<?php 

$image = 'http://www.example.com/image.jpg';

$info = getimagesize($image);

header('Content-Type: '.$info['mime']);

echo file_get_contents($image);

exit;

?>

Tuesday, 14 August 2018

PHP Headers and Popular Mime Types

Like my Create a Basic Web Service Using PHP, MySQL, XML, and JSON illustrates, even though a file's extension ends in PHP, you can still tell the browser that you're outputting a different content type. Here are a few of the more popular content types used on the internet.

Atom
header('Content-Type: application/atom+xml');
CSS
header('Content-Type: text/css');
Javascript
header('Content-Type: text/javascript');
JPEG Image
header('Content-Type: image/jpeg');
JSON
header('Content-Type: application/json');
PDF
header('Content-Type: application/pdf');
RSS
header('Content-Type: application/rss+xml; charset=ISO-8859-1');
Text (Plain)
header('Content-Type: text/plain');
XML
header('Content-Type: text/xml');
Just because a file ends in .PHP doesn't mean it responds with XHTML -- respond however you'd like!