Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Images

Problem using header(image/jpeg)...

images php coding issues or problems here.

Moderators: macek, egami, gesf

Problem using header(image/jpeg)...

Postby eduardor2k » Sat Aug 13, 2005 10:11 am

Hi to everyone...
I wrote a script who upload a picture to a folder and now i wrote another to load the picture, but they had a lot of bugs and started to fix them, the file who uploads the picture works fine, but the one in charge of loading the picture, example script.php?picture=1.jpg doesn't seem to work the way it should.

This is my old script.
Code: Select all
<?php
$imagen = "SECRETO".$_GET['imagen'];


$servidor = "redr2k.com";
$referer = $_SERVER['HTTP_REFERER'];

$dominio = substr_count("$referer", "$servidor");

if ($dominio != "0"){
      readfile($imagen);
           exit;
   }
   else{
      readfile("error.jpg");
           exit;
   }
?>


And this is the new one:

Code: Select all
<?php
$imagen = "SECRETO/".$_GET['imagen'];
$dominio = substr_count($_SERVER['HTTP_REFERER'],"redr2k.com");

    if (!file_exists($imagen)){$dominio = 0;}
    $dominio = 1;


   if($dominio == "0" OR empty($_GET['imagen'])){
          if(!empty($_SERVER['HTTP_REFERER']) AND !empty($_GET['imagen'])){
               $sql = "";
                @mysql_query($sql);
            }
           $len = filesize("error.jpg");
           header('Content-type: image/jpeg');
           header('Content-Length: $len');
           header('Content-Disposition: inline;  filename=\"error.jpg\"');
           readfile("error.jpg");
            exit;
   }
   else{
            $len = filesize($imagen);
           header('Content-type: image/jpeg');
           header('Content-Length: $len');
           header('Content-Disposition: inline;  filename=\"$imagen\"');
           readfile($imagen);
            exit;
   }
?>


The old script works fine, ok let's say it works because even if the browser loads the picture correctly if a access the url directly it shows a lot os symbold, thats because the server tells the browser to load the file, so i tried to fix it using the header sentence, this way i tell the browser he is loading a picture.

The second script, when you load a picture they are all broken (corrupted data) and i don't know how to fix it.

Thanks for your help and sorry if the variables are al in spanish.
eduardor2k
New php-forum User
New php-forum User
 
Posts: 3
Joined: Sat Aug 13, 2005 10:01 am
Location: Spain

Postby eduardor2k » Tue Aug 16, 2005 12:57 pm

Nobody Knows!

If i define only:

Code: Select all
 if ($dominio != "0"){
       readfile($imagen);
       exit;
    }
    else{
       readfile("error.jpg");
       exit;
    }


Without the header tags it works fine.
But it shouldn't work this way and should work with this!

Code: Select all
    //The only thing important in the code is the code inside the if sentence.
   
    if($dominio == "0" OR empty($_GET['imagen'])){

            $len = filesize("error.jpg");
            header('Content-type: image/jpeg');
            header('Content-Length: $len');
            header('Content-Disposition: inline;  filename=\"error.jpg\"');
            readfile("error.jpg");
            exit;
    }
    else{
            $len = filesize($imagen);
            header('Content-type: image/jpeg');
            header('Content-Length: $len');
            header('Content-Disposition: inline;  filename=\"$imagen\"');
            readfile($imagen);
            exit;
    }


Please help.

Thanks.
eduardor2k
New php-forum User
New php-forum User
 
Posts: 3
Joined: Sat Aug 13, 2005 10:01 am
Location: Spain

Postby WiZARD » Tue Aug 16, 2005 5:35 pm

Code: Select all
<?php
// $file = filename
// $path = path to file
header("Content-Disposition: attachment; filename=$file");
header("Content-Length: " . filesize($path));
header("Content-Type: " . filetype($path));
readfile("$path");
?>
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol


Return to PHP coding => Images

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron