Jump to content
Mudsaf

Issue with content-type i assume?

Recommended Posts

So basically i have this code below on test file which works perfectly.

header('Content-Type:'.'image/jpeg');
header('Content-Length: ' . filesize('E:/xampp/uploads/gallery/files/mursa_00d0ad79e535628240a4462948bf31dc.jpg'));
readfile('E:/xampp/uploads/gallery/files/mursa_00d0ad79e535628240a4462948bf31dc.jpg');

But when i try to implement it on my OOP file, i assume the Content-Type gets changed automatically to html/text? Any ideas what might cause this?

<?php
/* Grab image file outside of webroot directory */
if (session_status() == PHP_SESSION_NONE) {
session_start();
}

class LoadImage {
	private $GFilePath;
	private $DPath = "local";
	
	//Fetch single image by ID
	public function fetchIByID($iid) {
		
      	//bit validation
		if (!is_numeric($iid)) {
		die();
		}
		
		require_once("main_db_con.php"); //DB connection file => might cause issue ?
		$conn = new DbCon;
		$con = $conn->connect();
		
		
		
		//Get image by id parameter
		if ($res_gibi = $con->prepare("SELECT `image_url`, `mime_type` FROM images WHERE id = ? AND status = ? LIMIT 1")) {
		$prog_req1 = 1; //Unpublished imagec
		$res_gibi->bind_param("ii",$iid,$prog_req1);
			if ($res_gibi->execute()) {
				$res_gibi->store_result();
				
				if ($res_gibi->num_rows == 1) {
					
				$res_gibi->bind_result($iurl,$im_type);
				if ($res_gibi->fetch()) {
						if ($istatus == 1) {
						
						
						$this->GFilePath = "E:/xampp/uploads/gallery/files/"; //local path
						
							
							
							
							if (file_exists($this->GFilePath.$iurl)) {
							
                            //below not returning image
							header('Content-Type:'.'image/jpeg');
							header('Content-Length: ' . filesize('E:/xampp/uploads/gallery/files/mursa_00d0ad79e535628240a4462948bf31dc.jpg'));
							readfile('E:/xampp/uploads/gallery/files/mursa_00d0ad79e535628240a4462948bf31dc.jpg');

							//header('Content-Type:'.$im_type);
							//header('Content-Length: ' . filesize($this->GFilePath.$iurl));
							//readfile($this->GFilePath.$iurl);
							
							//$this->GFilePath.$iurl . " | " . $im_type;
					
							
							//$ftype;
							//"<br>" . $fpath;
							
							} else {
							
							}
						
						} else {
						// "<p>Unauthorized or image does not exist.</p>";
						}
                  
					} else {
					//
					}
				} else {
				//err_fetch	
				}
			} else {
				// "x";
			//ERR_EXEC	
			}
		} else {
		//ERR_PREP	
		// "y";
		}
		//Closes SQL connection
		unset($conn);
		$con->close();
	}
	
	
	//public function __construct() {
	//$this->fetchIByID(994);	
	//}
}


if (isset($_GET['image']) && is_numeric($_GET['image'])){
$iload = new LoadImage;
$iload->fetchIByID($_GET['image']);
}
?>

Not fully finished code, slightly altered but you get the idea + big ugly code atm but have to fix lated.

Looks like attached file on browser.

 

cap2.PNG

Edited by Mudsaf

Share this post


Link to post
Share on other sites

Still the same issue

if (isset($_GET['image']) && is_numeric($_GET['image'])){
$iload = new LoadImage;
$arr = $iload->fetchIByID($_GET['image']);
	if (!empty($arr[0]) && !empty($arr[1])) {
	echo $arr[0]  . " | " . $arr[1];
	//header('Content-Type:'.$arr[1]);
	//header('Content-Length: ' . filesize($arr[0]));
	//readfile($arr[0]);
	}
}

class blablabla {
//header stuff replaced with below
return array($this->GFilePath.$iurl, $im_type);
}

 

Share this post


Link to post
Share on other sites
print_r(get_headers("http://xxxx/loadimage.php?image=994",1));

returns: [Content-Type] => text/html;

Any idea what could be the cause?

Share this post


Link to post
Share on other sites

Any output before header such as html, php echo will cause header to fail. So you need to have headers as the test file, then replace values with return class variables without! ANY echo.

Share this post


Link to post
Share on other sites

I guess i'll remake the code one by one to see where it fails (if it fails again).  (Checked source code without output, is this the reason? 🤔)

 

class DbCon {
	protected $con;
	private $db_host = ""; //Database host
	private $db_user = ""; //Database username
	private $db_pass = ""; //Database user password
	private $db_name = ""; //Database name
	
	public function connect() {
	$this->con = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
		if ($this->con->connect_error) {
		//die("Connection error, please try again later.");
		} else {
		return $this->con;
		}
	}
}

This seems to jam it, but don't know why? Even when i require_once it without running it via function call. OR something to do with require inside class.

Day 17, i still cant seem to find the cure for this error, i'll continue tomorrow.

Edited by Mudsaf

Share this post


Link to post
Share on other sites

Test it stage by stage starting  from end backwards

1)Test file manually entered text string for header. Done ->Works

2) Test file using variables  for text string (WITHIN header function).

3) Class temporary return predefined text string values to be used for 2.

4) class processes as intended to return text string values identical to 3.

Try to avoid any empty line breaks before header functions.

 

Edited by dsonesuk

Share this post


Link to post
Share on other sites

If there's a problem with the headers you should be able to open that script in a browser to see the error messages.  Trying to send a header if they've already been sent will give you a warning and tell you where the first output was.  You can also use an error log and check that.

Share this post


Link to post
Share on other sites

Found the error, had 2 enter presses in DB connection file after <?php ?>. Such a silly mistake that forced html/text header. 😫

Share this post


Link to post
Share on other sites
On 7/2/2019 at 6:34 PM, Mudsaf said:

Found the error, had 2 enter presses in DB connection file after <?php ?>. Such a silly mistake that forced html/text header. 😫

You can omit a closing PHP tag on a file that doesn't output to prevent this from occurring.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...