Images with content-type as text/html, how to solve?

Asked

Viewed 142 times

-1

I am making a foreach in the controller to test if it is returning all images the problem is that the images do not appear because the content-type is as text/html, when I am using the index method of the controller has no problem, all files included in html have their content-type correct, the error is also happening in css.

Imagem com content-type: text/html

<?php

  use app\models\Images;

  Class Home{

  public function index(){
    include 'app/views/index.php';
    $images = new Images();
    foreach ($images->all() as $key => $value) {
      echo '<img src="app/views/imgs/'.$value->name.'">';
    }
  }

  public function add(){
    $name = $_FILES['imagem']['name'];
    $image = new Images();
    $image->setName($name);
    if($image->insert()){
      $image->upload();
    }
  }

  public function img(){
    include 'app/views/index.php';
    $images = new Images();
    foreach ($images->all() as $key => $value) {
      echo '<img src="app/views/imgs/'.$value->name.'">';
    }
  }
}
  • It must be some . htaccess in your folder, or it may be that folder /Home/app/views/imgs/... is incorrect

  • 1

    Oh yes 'Home' is the controller and it’s coming before the app in the image path actually it shouldn’t even be there, the right one was just app/views/imgs/..., thank you very much for the help.

1 answer

0


I think it’s a typo, because the images are accessing this:

/Home/app/views/imgs/...

Instead:

/app/views/imgs/...

Correcting:

public function index(){
  include 'app/views/index.php';
  $images = new Images();
  foreach ($images->all() as $key => $value) {
    echo '<img src="../app/views/imgs/'.$value->name.'">';
  }
}

...

public function img(){
  include 'app/views/index.php';
  $images = new Images();
  foreach ($images->all() as $key => $value) {
    echo '<img src="../app/views/imgs/'.$value->name.'">';
  }
}
  • Yes that’s right but it’s not a typo, it turns out that the request url to search for this image is incorrect depending on the path I put in src, because of the controllers, methods and parameters passed in the url, request understands that both are folders to access the image.

  • Typo is a way to refer to missing something in the code or left.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.