1

How to change picture name in php page i have file "photo.php" which get id of a picture. and when i access link location name on title bar show as "photo.php". that means when i click save image as.. a name is "photo.php". i want its name for some field. I've tried header('Content-Disposition: attachment; filename="'.$name.'"'); but the header sent a picture as download I want to see picture in direct link of it URL.

include("conn.php");
    $userimgid = $_GET['id'];
    $sql = "SELECT * FROM `file` WHERE `userid` = '$userimgid' ";
    $sqlquery = mysql_query($sql)or die(mysql_error());
    $num = mysql_num_rows($sqlquery);
    if($num!=0){
            $type = mysql_result($sqlquery, 0, "mime");
            header("Content-type: $type");  
            $content = mysql_result($sqlquery,0,"data");
            echo $content;
    }else
        echo "Invalid requested.";

Thanks all answers

Andreas Baumgart
  • 2,647
  • 1
  • 25
  • 20
brendan
  • 19
  • 2
  • 8
  • 1
    You probably need to do this via htaccess. – putvande Nov 14 '13 at 07:56
  • 1
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Nov 14 '13 at 07:59

1 Answers1

1

I think your approach using the Content-Disposition header is right. You just have to replace attachment with inline and it will open the image in the browser window.

Edit: So that would be header('Content-Disposition: inline; filename="'.$name.'"');.

Andreas Baumgart
  • 2,647
  • 1
  • 25
  • 20
  • thanks Andreas it worked, i just overlook for it and it will be better if a picture name appears to title bar too. – brendan Nov 14 '13 at 08:41
  • Unless you wrap the image in a HTML document, it is up to the browser what to display in the title bar. In general this should be the plain filename. – Andreas Baumgart Nov 14 '13 at 10:54