-1

I am trying to open a .pdf that is associated with a certain product. I am using PHP to display the product and its information. I then want to when a link is clicked have that PDF open in a new window.

I have these variables set up.

<?php
    require_once("connect.php");
    $thisone = $_GET['id'];

    $allMov = "SELECT * FROM tbl_handbags WHERE handbags_id=".$thisone;

    $movResults = mysql_query($allMov);
    $row = mysql_fetch_array($movResults);
    $filename = $row['filename_pdf'];
    $prefix = "pdfs";
?>

And then this

<?php echo "<a target = '_blank'  href='$prefix/$filename' >View and Download Spec Sheet .PDF </a>";?>

What is happening is that a new window opens however its the PDFS "Parent Directory". Here I can click on the proper PDF and it does what I want it to do. I just need to cut out the parent directory page somehow.

Tomasz Kowalczyk
  • 10,472
  • 6
  • 52
  • 68
  • 4
    **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 Feb 19 '14 at 20:35
  • 1
    Sounds like you're storing the "parent directory" into the `filename_pdf` column instead of the actual file name. – Patrick Q Feb 19 '14 at 20:37
  • 1
    This question appears to be off-topic because the poster showed no evidence of having done any research of their own – GordonM Feb 19 '14 at 20:38
  • check $filename, i think it is empty, that is why you are landing in pdfs directory, also place a blank index.html in pdfs directory to save directory browsing. – Pranay Bhardwaj Feb 19 '14 at 20:42
  • I edited my answer to be more clear. Hope it helps. – Paolo Feb 20 '14 at 09:12
  • Thank you for all the help! The issue was that when i imported the Database the filename_pdf was erased or not saved so I had to add another column with the corresponding PDFS – user3329725 Feb 20 '14 at 19:19

1 Answers1

0

The behaviuor you see is because the string

"$prefix/$filename"

-or similarly-

$prefix . '/' . $filename

is a path that instead of pointing to the PDF file points to its parent directory.

Your web server is configured to allow directory listing (one thing that I would avoid in production) so you see all the files and then can click the file and open it...

In the code you posted there is nothing that obviously is wrong and make this happen.

So I suggest to edit the second part of your code (that renders the page) and temporarily replace it with

<?php echo "prefix = " . $prefix . "\n<br />" . "filename = " . $filename;

Either $prefix, $filename or both are wrong, with the above code you dump their content and you'll be able to narrow your debugging.

Paolo
  • 15,233
  • 27
  • 70
  • 91