Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Seeking Assistance From David Powers Again, Please

Contributor ,
Jan 20, 2008 Jan 20, 2008
David,

I am using your drop down menu of files script from page 197 of PHP Solutions. I have successfully adapted it to read in a list of PDF's from one of my subdirectories. The list of files is not in a data base, but read in directly from a sub-directory.

However, for the life of me, I can't figure out how to actually make one of the PDF's load. I have tried every syntax/combination in the Action property I can think of with no luck.

What I would like to do is either: 1) select the PDF from the drop down and then have it load into the browser or 2) selec the PDF fromt the drop down and then press a "download" button and have the file load into the browser.

Can you please help me figure this out?

Thanks in advance. Below is my code so far.

Glenn

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Image drop-down</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="PDFs/<?php echo $_POST['pix']; ?>.php">
<p>
<select name="pix" id="pix">
<option value="">Select an image</option>
<?php
include('Includes/buildFileList5.php');
buildFileList5('PDFs');

?>
</select><label></label>
</p>
<p>
<label>Download
<input type="submit" name="Download" id="Download" value="Submit" />
</label>
</p>

</form>
</body>
</html>
TOPICS
Server side applications
578
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jan 21, 2008 Jan 21, 2008
GEAtkins wrote:
> Now I get this error in download.php.
>
> Parse error: syntax error, unexpected T_STRING in
> C:\xampp\htdocs\Garrison\download.php on line 21

This is the sort of error that you should be able to diagnose quickly by
looking at the colouring of the code. The error is mine, because I was
just typing in a newsreader, but the cause becomes immediately apparent
in Dreamweaver code view, because several lines after the trailing slash
are turned red. This means that the followi...
Translate
LEGEND ,
Jan 20, 2008 Jan 20, 2008
GEAtkins wrote:
> What I would like to do is either: 1) select the PDF from the drop down and
> then have it load into the browser or 2) selec the PDF fromt the drop down and
> then press a "download" button and have the file load into the browser.

Scenario 2 is easy. Set the action attribute to download.php, and use
PHP Solution 7-6. Since you're submitting a form, set the method to
post, and use the $_POST array in download.php, rather than $_GET.

Opening a PDF directly in the browser can be done by applying the
Dreamweaver Jump Menu behavior to the drop-down, but it relies on
JavaScript being enabled.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 20, 2008 Jan 20, 2008
David,

Thanks. A couple more things.

When I first ran it I got a ")" expected error in line 3 below.

if (isset($_GET['file'] && basename($_GET['file']) == $_GET['file']) {
$getfile = $_GET['file'];

I added a ")" after if (isset($_GET['file']

to get this

if (isset($_GET['file'])

It worked from there, except now it gives me a "Download not available" message.

I also changed the file path to $filepath = 'PDFs'.$getfile;

as "PDFs" is the folder in which the files are located, but the script never gets to this line because of the "Download not available"

Listed below is my modified download.php code and the imagelist_php5.php code.

I am obviously missing something that is not obvious to me, a beginner.

Can you help me further from here? Thanks

Glenn

<?php
// block any attempt to explore the filesystem
if (isset($_POST['file']) && basename($_POST['file']) == $_POST['file']) {
$getfile = $_POST['file'];
}
else {
$getfile = NULL;
}
// define error handling
$nogo = 'Sorry, download unavailable. <a href="prompt.php">Back</a>.';

if (!$getfile) {
// go no further if filename not set
echo $nogo;
}
else {
// define the pathname to the file
$filepath = 'PDFs'.$getfile;
// check that it exists and is readable
if (file_exists($filepath) && is_readable($filepath)) {
// get the file's size and send the appropriate headers
$size = filesize($filepath);
header('Content-Type: application/octet-stream');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$getfile);
header('Content-Transfer-Encoding: binary');
// open the file in binary read-only mode
// suppress error messages if the file can't be opened
$file = @ fopen($filepath, 'rb');
if ($file) {
// stream the file and exit the script when complete
fpassthru($file);
exit;
}
else {
echo $nogo;
}
}
else {
echo $nogo;
}
}
?>






<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Image drop-down</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="download.php">
<p>
<select name="pix" id="pix">
<option value="">Select an image</option>
<?php
include('Includes/buildFileList5.php');
buildFileList5('PDFs');

?>
</select>
<label></label>
</p>
<p>
<label>Download
<input type="submit" name="Download" id="Download" value="Submit" />
</label>
</p>

</form>
</body>
</html>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 21, 2008 Jan 21, 2008
GEAtkins wrote:
> I added a ")" after if (isset($_GET['file']
>
> to get this
>
> if (isset($_GET['file'])

The code as published in the book does have a closing parenthesis. Are
you saying there's a mistake in the download file?

> It worked from there, except now it gives me a "Download not available"
> message.
>
> I also changed the file path to $filepath = 'PDFs'.$getfile;

That's why you get the error. It needs to be an absolute path, not a
relative one.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 21, 2008 Jan 21, 2008
David,

Yes, there appears to be an error in the download file.

I changed everything to absolute paths and it still does not work. It will successfully load the list of files into the drop down, but when I press the button it gives me the download not available error.

I know this is something simple I am overlooking (beginner and all), but I just can't figure it out.

Below is the code.

I also trie one version using the jump menu but I can only get it to read the root folder, such as www.marketscholar.com/filename. It should be www.marketscholar.com/PDFs/filename. How can I change the jump menu code to the PDFs path?

Thanks for the help.

Glenn


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Image drop-down</title>
<script type="text/javascript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>

<body>

<form name="form" id="form">

<p>
<select name="jumpMenu" id="jumpMenu" onchange="MM_jumpMenu('parent',this,0)">

<option value="">Select an image</option>
<?php
include('Includes/buildFileList5.php');
buildFileList5('PDFs');

?>
</select>
<label></label>
</p>
<p>
<label>Download
<input type="submit" name="Download" id="Download" value="Submit" />
</label>
</p>

</form>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</body>
</html>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Image drop-down</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="download.php">
<p>
<select name="pix" id="pix">
<option value="">Select an image</option>
<?php
include('Includes/buildFileList5.php');
buildFileList5('C:\xampp\htdocs\Garrison\PDFs');

?>
</select>
<label></label>
</p>
<p>
<label>Download
<input type="submit" name="Download" id="Download" value="Submit" />
</label>
</p>

</form>
</body>
</html>



<?php
function buildFileList5($theFolder) {
// Execute code if the folder can be opened, or fail silently
if ($contents = @ scandir($theFolder)) {
// initialize an array for matching files
$found = array();
// Create an array of file types
$fileTypes = array('pdf','jpg','jpeg','gif','png');
// Traverse the folder, and add filename to $found array if type matches
$found = array();
foreach ($contents as $item) {
$fileInfo = pathinfo($item);
if (array_key_exists('extension', $fileInfo) && in_array($fileInfo['extension'],$fileTypes)) {
$found[] = $item;
}
}

// Check the $found array is not empty
if ($found) {
// Sort in natural, case-insensitive order, and populate menu
natcasesort($found);
foreach ($found as $filename) {
echo "<option value='$filename'>$filename</option>\n";
}
}
}
}
?>


<?php
// block any attempt to explore the filesystem
if (isset($_POST['pix']) && basename($_POST['pix']) == $_POST['pix']) {
$getfile = $_POST['pix'];
}
else {
$getfile = NULL;
}
// define error handling
$nogo = 'Sorry, download unavailable. <a href="prompt.php">Back</a>.';

if (!$getfile) {
// go no further if filename not set
echo $nogo;
}
else {
// define the pathname to the file
$filepath = 'C:\xampp\htdocs\Garrison\PDFs'.$getfile;
// check that it exists and is readable
if (file_exists($filepath) && is_readable($filepath)) {
// get the file's size and send the appropriate headers
$size = filesize($filepath);
header('Content-Type: application/octet-stream');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$getfile);
header('Content-Transfer-Encoding: binary');
// open the file in binary read-only mode
// suppress error messages if the file can't be opened
$file = @ fopen($filepath, 'rb');
if ($file) {
// stream the file and exit the script when complete
fpassthru($file);
exit;
}
else {
echo $nogo;
}
}
else {
echo $nogo;
}
}
?>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 21, 2008 Jan 21, 2008
GEAtkins wrote:
> Yes, there appears to be an error in the download file.

OK, thanks. I'll ask foED to change it.

> I changed everything to absolute paths and it still does not work.

That's because you have omitted the trailing slash at the end of the
path. It should be this:

$filepath = 'C:\xampp\htdocs\Garrison\PDFs\'.$getfile;

> I also trie one version using the jump menu but I can only get it to read the
> root folder, such as www.marketscholar.com/filename. It should be
> www.marketscholar.com/PDFs/filename. How can I change the jump menu code to
> the PDFs path?

It's not the jump menu code that you need to change, but the
buildFileList5() function. Since you're pointing to a different folder,
you need to change this section:

> foreach ($found as $filename) {
> echo "<option value='$filename'>$filename</option>\n";
> }

to this:

foreach ($found as $filename) {
echo "<option value='PDFs/$filename'>$filename</option>\n";
}

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 21, 2008 Jan 21, 2008
Thanks,

I put the trailing slash in the download.php file but not in the imagelist_php5.php.

Now I get this error in download.php.

Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\Garrison\download.php on line 21

Line 21 is:

// get the file's size and send the appropriate headers

the whole download.php code is below.

Thanks

Glenn

<?php
// block any attempt to explore the filesystem
if (isset($_POST['pix']) && basename($_POST['pix']) == $_POST['pix']) {
$getfile = $_POST['pix'];
}
else {
$getfile = NULL;
}
// define error handling
$nogo = 'Sorry, download unavailable. <a href="prompt.php">Back</a>.';

if (!$getfile) {
// go no further if filename not set
echo $nogo;
}
else {
// define the pathname to the file
$filepath = 'C:\xampp\htdocs\Garrison\PDFs\'.$getfile;
// check that it exists and is readable
if (file_exists($filepath) && is_readable($filepath)) {
// get the file's size and send the appropriate headers
$size = filesize($filepath);
header('Content-Type: application/octet-stream');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$getfile);
header('Content-Transfer-Encoding: binary');
// open the file in binary read-only mode
// suppress error messages if the file can't be opened
$file = @ fopen($filepath, 'rb');
if ($file) {
// stream the file and exit the script when complete
fpassthru($file);
exit;
}
else {
echo $nogo;
}
}
else {
echo $nogo;
}
}
?>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 21, 2008 Jan 21, 2008
GEAtkins wrote:
> Now I get this error in download.php.
>
> Parse error: syntax error, unexpected T_STRING in
> C:\xampp\htdocs\Garrison\download.php on line 21

This is the sort of error that you should be able to diagnose quickly by
looking at the colouring of the code. The error is mine, because I was
just typing in a newsreader, but the cause becomes immediately apparent
in Dreamweaver code view, because several lines after the trailing slash
are turned red. This means that the following lines are being treated as
part of the string. A backslash in front of a single quote, turns it
into a literal quote inside a string.

The answer is to escape the backslash:

> $filepath = 'C:\xampp\htdocs\Garrison\PDFs\'.$getfile;

Should be this:

$filepath = 'C:\xampp\htdocs\Garrison\PDFs\\'.$getfile;

Alternatively, use forward slashes for the directory separator like this:

$filepath = 'C:/xampp/htdocs/Garrison/PDFs/'.$getfile;

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 21, 2008 Jan 21, 2008
LATEST
David,

That's it. Works as it should. I suspected that was the problem because the code was red and not orange. I just didn't know how to fix it, but I'm learning.

Thanks again for your help.

Glenn
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines