problem to open files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dizeta
Forum Commoner
Posts: 47
Joined: Mon Feb 02, 2004 9:53 am

problem to open files

Post by dizeta »

hi :) ,

i have still problems to complete my news manager script.

well, when i click on a link to open file1 for the first time , it woks.
but if i try to click to another link ( in the same page) to open a new file2, will be open always the file1.... :?

any ideas?

the script:

Code: Select all

<?php

include("dbconnect.php");

$query = mysql_query("SELECT id_sharepdf, DatiBinari, nome, Type, news.id_news FROM pdf, news WHERE pdf.id_sharepdf = news.id_news")or die(mysql_error());

$result = @mysql_fetch_assoc($query);

header("Content-Type: {$result['Type']}");
header("Content-Disposition: filename={$result['Nome']}");
header("Content-Transfer-Encoding: binary");

echo $result['DatiBinari'];

@mysql_close();

 
?>
thanks
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Tricky... Lookup header() and check if adding cache-headers helps in this case. Ie. but not limited to the following:

Code: Select all

header("Pragma: public");
   header("Expires: 0"); // set expiration time
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
User avatar
dizeta
Forum Commoner
Posts: 47
Joined: Mon Feb 02, 2004 9:53 am

Post by dizeta »

JAM wrote:Tricky... Lookup header() and check if adding cache-headers helps in this case. Ie. but not limited to the following:

Code: Select all

header("Pragma: public");
   header("Expires: 0"); // set expiration time
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

same problems.... :cry:

Code: Select all

<?php
header("Content-Type: {$result['Type']}");
header("Content-Disposition: filename={$result['Nome']}");
header("Content-Transfer-Encoding: binary");
header("Pragma: public"); 
header("Expires: 10"); // set expiration time 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
?>
User avatar
dizeta
Forum Commoner
Posts: 47
Joined: Mon Feb 02, 2004 9:53 am

Post by dizeta »

help! :roll:
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

After a second review:

Code: Select all

"SELECT id_sharepdf, DatiBinari, nome, Type, news.id_news FROM pdf, news WHERE pdf.id_sharepdf = news.id_news"
How do you select the two files? The query itself looks like a one-file selection imho.
Ie.: nome is allways the same using only the above. Just making sure that you actually have a way to distinguish between the two files...
User avatar
dizeta
Forum Commoner
Posts: 47
Joined: Mon Feb 02, 2004 9:53 am

Post by dizeta »

hi,
i've tried to do this:

Code: Select all

<?
include("dbconnect.php");

$getnews = mysql_query("select * from news ORDER BY id_news DESC")or die (mysql_error());

$query = mysql_query("SELECT  pdf.Nome, pdf.id_pdf, news.id_news FROM pdf, news WHERE news.id_news = pdf.id_sharepdf  ORDER BY id_news DESC")or die (mysql_error());

$query2 = mysql_query("SELECT  doc.Nome, doc.id_doc, news.id_news FROM doc, news WHERE news.id_news = doc.id_sharedoc ORDER BY id_news DESC")or die (mysql_error());


while ($row = mysql_fetch_array($getnews)) 
{ 

$nome=mysql_fetch_assoc($query);

$nome1=mysql_fetch_assoc($query2);

extract($row);
echo <<< NEWS
 
<div id="riga"><h3>Titolo: $title</h3></div>     
<div id="riga1"=><h4>News inserita il $date -  <b>Autore:</b> $author</h4></div>
<div id="riga2"><h5>$news</h5>

<img src="../../../immagini/pdf.gif" width="16" height="16" style="float: left;" />
[b]<a href="download.php?action=download&type=pdf&Id={$nome['id_pdf']}"/><h6>{$nome['Nome']}</h6></a>[/b]
<img src="../../../immagini/word.gif" width="16" height="16" style="float: left;" />
[b]<a href="download.php?action=download&type=doc&Id={$nome1['id_doc']}"/><h6>{$nome1['Nome']}</h6></a>[/b]</div>

<div id="riga3"></div>
NEWS;

}

 ?>

and download.php ( to openthe news with Reader and/or Word)

Code: Select all

<?php

include("dbconnect.php");
$query = false;
switch($_GET['type']) {
   case 'pdf':
      $query = "SELECT DatiBinari, Nome, Type, news.id_news FROM pdf, news WHERE pdf.id_sharepdf = '{$_GET['id_pdf']}'";
      break;
   case 'doc':
     $query = "SELECT DatiBinari, Nome, Type, news.id_news FROM doc, news WHERE doc.id_sharedoc = '{$_GET['id_doc']}'";
     break;
   default:
      echo "Dati non corretti";
      break;
}
if($query !== false) {
      $result = mysql_query($query) or die(mysql_error());
      list($DatiBinari, $Nome, $Type, $id_news) = mysql_fetch_row($result);
      
header("Content-Type: {$result['Type']}");
header("Content-Disposition: filename={$result['Nome']}");
header("Content-Transfer-Encoding: binary");
//header("Pragma: public"); 
//header("Expires: 10"); // set expiration time 
//header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

   
      echo $DatiBinari;


}

?>
but now it returns a blank page :? ....

[Edit: Added more PHP tags for eyecandy. --JAM]
User avatar
dizeta
Forum Commoner
Posts: 47
Joined: Mon Feb 02, 2004 9:53 am

Post by dizeta »

up :oops:
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

i was having some troubles with downloading files in IE (pdf), i dunno if your still having header troubles, but i can tell you that these are the headers i sent, and pdf will open correctly with them:

Code: Select all

header("Content-Type: application/octetstream");
	header("Expires: ". gmdate("D, d M Y H:i:s") . " GMT");
	header("Content-Disposition: inline; filename="$_POST[file_name]"");
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	header("Pragma: public");
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Because...

Code: Select all

<a href="download.php?action=download&type=doc&Id={$nome1['id_doc']}"/>
You are using Id= is those links, but {$_GET['id_doc']} in the download.php?

Sidenote; I think that:

Code: Select all

header("Content-Transfer-Encoding: binary"); 
// should be
header("Content-Transfer-Encoding: inline");
User avatar
dizeta
Forum Commoner
Posts: 47
Joined: Mon Feb 02, 2004 9:53 am

Post by dizeta »

JAM wrote:Because...

Code: Select all

<a href="download.php?action=download&type=doc&Id={$nome1['id_doc']}"/>
You are using Id= is those links, but {$_GET['id_doc']} in the download.php?
:?:
i'm sorry...but i'm not sure to understand what you're telling me...
could you explain me?
thanks for the patience
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

If you use a link that looks like this,

Code: Select all

<a href="download.php?action=download&type=doc&Id=FOO" />
...you need to use $_GET['Id'] to retrieve the value FOO.

Now look at your script. You use $_GET['id_doc'] in your SQL query, but you do not have that in the URI/link ( no id_doc= ).

Hope I explained abit better this time.
User avatar
dizeta
Forum Commoner
Posts: 47
Joined: Mon Feb 02, 2004 9:53 am

Post by dizeta »

ok, now all scripts work :D

the solution was:

news.php:

Code: Select all

<?php

include("dbconnect.php");
$query = "SELECT news.id_news, news.author, news.date, news.title, news.news, pdf.id_pdf, pdf.Nome, doc.id_doc, doc.Nome
          FROM news
          LEFT JOIN pdf ON (news.id_news = pdf.id_sharepdf)
          LEFT JOIN doc ON (news.id_news = doc.id_sharedoc)";
$result = mysql_query($query) or die(mysql_error());
while(list($id_news, $author, $date, $title, $news, $id_pdf, $nome_pdf, $id_doc, $nome_doc) = mysql_fetch_row($result)) {
    echo "
          <div id="riga"><h3>Titolo: $title</h3></div>
          <div id="riga1"=><h4>News inserita il $date -  <b>Autore:</b> $author</h4></div>
          <div id="riga2"><h5>$news</h5>
           <img src="../../../immagini/pdf.gif" width="16" height="16" style="float: left;" />
           <a href="download.php?action=download&type=pdf&Id=$id_news"/><h6>$nome_pdf</h6></a>
           <img src="../../../immagini/word.gif" width="16" height="16" style="float: left;" />
           <a href="download.php?action=download&type=doc&Id=$id_news"><h6>$nome_doc</h6></a>
          </div>";
}

?>
and for download.php:

Code: Select all

<?php
include("dbconnect.php");

switch($_GET['type']) {
   case 'pdf':
           $query = "SELECT DatiBinari, Nome, Type FROM pdf WHERE id_sharepdf = '{$_GET['Id']}'";
    break;
   case 'doc':
         $query = "SELECT DatiBinari, Nome, Type FROM doc WHERE id_sharedoc = '{$_GET['Id']}'";
  break;
  default:
    echo "Dati non corretti";
    exit;
    break;

}

$result = mysql_query($query) or die(mysql_error());
if($result) {
   $ris = mysql_fetch_assoc($result);
  
   header("Content-Type: {$ris['Type']}");
   header("Expires: ". gmdate("D, d M Y H:i:s") . " GMT"); 
   header("Content-Disposition: inline; filename={$ris['Nome']}");
   header("Content-Transfer-Encoding: inline");
  header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
   header("Pragma: public"); 
  
   echo $ris['DatiBinari'];
   exit;
}
?>
but i still have a little and annoiyng problem.
when i open a doc file from browser (IE), MS Word ask me in a little windows to "convert file from:" with a list of applications inside.

it's a header's problem? how can i solve this problem?
thanks
Post Reply