Welcome Guest, Not a member yet? Register   Sign In
read_file: file is being read, but incorrect output. Any ideas?
#1

[eluser]codex[/eluser]
I'm trying to convert 'smart image resizer' for creating thumbnails from one source (it's something similar to phpThumb) into a CI library/controller, but I'm having some difficulties. It seems as if you can't read a file and display it from within a controller.

As a test I have made this:
Code:
class Test extends Controller {

    function Test()
    {
        parent::Controller();    
    }
    
    function Resize()
    {
        $this->load->helper('file');
        $data = read_file('d:/wamp/www/codeigniter/assets/test.jpg');
        
        $this->output->set_header("content-type: image/jpeg");
        $this->output->set_output($data);
    }
}

It should read 'test.jpg' and display it by going to a rerouted url. But the only thing being displayed is the url as an image. No good. Any ideas why this won't work?
#2

[eluser]Randy Casburn[/eluser]
I'm thinking you're kinda messin' with the file when you read it and "cast" it into a string type.

This is the equivalent of FTPing a file in text mode instead of binary mode. Maybe, not sure, if you treat the file as a binary asset it will act better.

This is off the top of my head though.

Randy
#3

[eluser]Randy Casburn[/eluser]
So, this is a start...
-- never mind this --
#4

[eluser]Randy Casburn[/eluser]
Don't know. Can't reproduce your problem. It works both ways for me. Using the file helper exactly the way you have it written work perfectly for me. so it must come down to a configuration thing.

Tried it with jpg, gif, and png mime types and all worked flawlessly.

Randy
#5

[eluser]codex[/eluser]
[quote author="Randy Casburn" date="1216803194"]Don't know. Can't reproduce your problem. It works both ways for me. Using the file helper exactly the way you have it written work perfectly for me. so it must come down to a configuration thing.

Tried it with jpg, gif, and png mime types and all worked flawlessly.

Randy[/quote]

Randy, thank you for testing. I'm thinking configuration too. Going to mess with that.

Thanks!


EDIT: Weird, it still won't work. Everything other than image files works without problems.
#6

[eluser]codex[/eluser]
Ok, I don't get it. When placed in a function/class the code will not work, place it outside a function it will. Why is that?

Code:
class Test extends Controller {

// This function when called will output the url, but as an image
    function Resize()
    {
        $data    = file_get_contents('d:/wamp/www/codeigniter/assets/test.jpg');
    
        header("Content-type: image/jpeg");
        echo $data;
    }
}

// This WILL output the image (=good)
$data    = file_get_contents('d:/wamp/www/codeigniter/assets/test.jpg');
    
    header("Content-type: image/jpeg");
    echo $data;
#7

[eluser]Randy Casburn[/eluser]
Hi Codex,

Maybe we're trying too hard. Let's just go back to a basic set up with a basic controller the way you had it at first. Your original set up was correct. So let's go back to that...

Code:
class Test extends Controller {

    function Test()
    {
        parent::Controller();
        
    }

    function index()
    {
        $this->load->helper('file');
        $data = read_file('user_guide/images/ci_logo.jpg');
        $this->output->set_header("content-type: image/jpeg");
        $this->output->set_output($data);
    }
    
}

Without any routing and a clean install of CI, I know this works without any problem and will display the ci logo.

Let's try this and see what happens.

Randy
#8

[eluser]Randy Casburn[/eluser]
There are a couple of things to point out in the post above. First is the use of the relative path to the image. That isn't important for now, but it might be later. The second is the fact that I'm running this in the index() function. I wasn't sure how you were calling the Resize() function before and that could have some implications too.

So for now, try to run this from the index() function (http://yadayada.com/test) to see if it runs. We can move it to a Resize() function later.

Randy
#9

[eluser]codex[/eluser]
[quote author="Randy Casburn" date="1216839311"]Hi Codex,

Maybe we're trying too hard. Let's just go back to a basic set up with a basic controller the way you had it at first. Your original set up was correct. So let's go back to that...

Code:
class Test extends Controller {

    function Test()
    {
        parent::Controller();
        
    }

    function index()
    {
        $this->load->helper('file');
        $data = read_file('user_guide/images/ci_logo.jpg');
        $this->output->set_header("content-type: image/jpeg");
        $this->output->set_output($data);
    }
    
}

Without any routing and a clean install of CI, I know this works without any problem and will display the ci logo.

Let's try this and see what happens.

Randy[/quote]

Hmm, indeed. This works (didn't think of testing a clean install). I really have no clue what's that different about my other setup that it fails.

I'm using matchbox, but I don't see why that should be a problem.
#10

[eluser]Randy Casburn[/eluser]
It could be as simple as how you are calling Resize() then. You didn't have an index() function in your other Class and CI gets funky when that is the case.

Try this. Put an empty index() function in your original posted code and see what happens...

Code:
function index()
{
}

Randy




Theme © iAndrew 2016 - Forum software by © MyBB