Brian Sowter
asked on
Sample php code to search for link to a file on website.
On my website www.sowter.co.uk I have a number of pdf files (up to 5000) in a folder called /co/ours/xxxx.pdf. I want to have an html search box into which the user can type a 4 digit number and get a link to the file corresponding to the number. I guess I will also need some error indication returned if the file is not present.
ASKER
Looks promising thank you!
I intend to create a new webpage on my site to contain the search box and some notes. i have no problem doing this but could you please help me with some code for the actual search box to enter the type number and a "search" button.
Also does it matter where I put the force-download code? i will have other php code on the page associated with the menu etc.
Am I right in assuming the link "http://www.yoursitename.com/force-download.php?file_number=1234" is generated by your code and would appear where I put the echo statement?
I would like it to echo "XXX.pdf" or "Info not available please email us." i dont think the customer will understand abour force-downloads
I intend to create a new webpage on my site to contain the search box and some notes. i have no problem doing this but could you please help me with some code for the actual search box to enter the type number and a "search" button.
Also does it matter where I put the force-download code? i will have other php code on the page associated with the menu etc.
Am I right in assuming the link "http://www.yoursitename.com/force-download.php?file_number=1234" is generated by your code and would appear where I put the echo statement?
I would like it to echo "XXX.pdf" or "Info not available please email us." i dont think the customer will understand abour force-downloads
You can place force-download.php page wherever you want. But then you will have to generate the URL according to that.
Lets take an example.
Suppose you have placed force-download.php in a folder named 'download_scripts'.
Then you will have to generate URL similar to below URL.
http://www.yoursitename.com/download_scripts/force-download.php?file_number=1234
And above link is not generated by my code, your search results page will generate it.
I will put PHP code of search and generating the URL
Lets take an example.
Suppose you have placed force-download.php in a folder named 'download_scripts'.
Then you will have to generate URL similar to below URL.
http://www.yoursitename.com/download_scripts/force-download.php?file_number=1234
And above link is not generated by my code, your search results page will generate it.
I will put PHP code of search and generating the URL
You will have to create front HTML and if possible use jQuery to validate user input (it should be a 4 digit number ) and use code in the attached file to generate URL.
search-documents.php
search-documents.php
ASKER
I seem to be missing something. I have two php files but I dont see any code to get the XXXX from the user.
You will have to create it. Create HTML to display search box and search button to user. And when user clicks on Search button then you will send request to a page. In that page use code of search-documents.php to generate the URL and display it to user. And if user clicks on generated link then PDF will be downloaded automatically.
Does that make sense?
Does that make sense?
ASKER
I have got the code running so far and obviously it displays the error messages which I can edit as required.
Can you help me please with the HTML for the search button?
Can you help me please with the HTML for the search button?
Post your HTML code.
ASKER
URS is not working.
ASKER
What is URS?
Oh type URL.
ASKER
<form>
Type number: <input type="text" name='file_number' />
</form>
<?php // echo 'file_number' ?>
<?php
// code to include config and other required files
// Check if file_number has been passed or not
if( isset($_POST['search_keywo
{
// code to generate URL
$url = "http://www.sowter.co.uk/scripts/force-download.php?file_number=".$_POST['search_keyword'
echo $url;
}
?>
Insert below code at the top of the page " WEBPAGE COLOURS.php " or whatever name you have given.
// Check if file_number has been passed or not
if( isset($_POST['type_number'
{
// code to generate URL
$url = "http://www.yoursitename.com/download_scripts/force-download.php?file_number=".$_POST['type_number'];
$type_number= $_POST['type_number'];
}
else
{
$url = '';
$type_number= '';
}
And put below code where you want to display the search box.
<form name="frmSearchDocuments" id="frmSearchDocuments" method="post" action="">
Type number: <input type="text" name="type_number" id="type_number" value="<?php echo $type_number;?>" />
<input type="submit" name="btn_search_document"
<?php
if( $url != '' )
{
?>
<a href="<?php echo $url;?>">Download Document</a>
<?php
}
?>
</form>
You will have to make the necessary changes in your file.
Please find attached page for demo page.
search-documents.php
Let me know if you need more help.
ASKER
The search box codes seems to be working fine.
The URL behind the "Download document" looks good and contains the number entered but the force-download code gives ERROR 1 . Here is the code:
<?php
// code to include config and other required files
// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_co mpression' ))
ini_set('zlib.output_compr ession', 'Off');
// Check if file_number has been passed or not
if( isset($_GET['file_number'] ) && $_GET['file_number'] != '' )
{
// Read filename from URL.
$filename = $_GET['file_number'].'pdf' ;
// Code to check if files exists or not
$file_path = 'colours/'.$filename;
if ( !file_exists($file_path) )
{
echo "<html><title>www.sowter.co.uk</title><bod y>ERROR 1: File not found. USE force-download.php?file=fi lenumber</ body></htm l>";
exit;
};
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: application/pdf");
header("Content-Dispositio n: attachment; filename=\"".basename($fil ename)."\" ;" );
header("Content-Transfer-E ncoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
}
else
{
echo "<html><title>Your Sitename</title><body>ERRO R 2: download file NOT SPECIFIED. USE force-download.php?file=fi lenumber</ body></htm l>";
exit;
}
?>
The URL behind the "Download document" looks good and contains the number entered but the force-download code gives ERROR 1 . Here is the code:
<?php
// code to include config and other required files
// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_co
ini_set('zlib.output_compr
// Check if file_number has been passed or not
if( isset($_GET['file_number']
{
// Read filename from URL.
$filename = $_GET['file_number'].'pdf'
// Code to check if files exists or not
$file_path = 'colours/'.$filename;
if ( !file_exists($file_path) )
{
echo "<html><title>www.sowter.co.uk</title><bod
exit;
};
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: application/pdf");
header("Content-Dispositio
header("Content-Transfer-E
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
}
else
{
echo "<html><title>Your Sitename</title><body>ERRO
exit;
}
?>
Put the full path here.
$file_path = 'colours/'.$filename;
or replace below code
echo "<html><title>www.sowter.co.uk</title><bod y>ERROR 1: File not found. USE force-download.php?file=fi lenumber</ body></htm l>";
with
echo "<html><title>www.sowter.co.uk</title><bod y>ERROR 1: File not found. USE force-download.php?file=fi lenumber<b r><br>".$f ile_path." </body></h tml>";
$file_path = 'colours/'.$filename;
or replace below code
echo "<html><title>www.sowter.co.uk</title><bod
with
echo "<html><title>www.sowter.co.uk</title><bod
ASKER
Hi Atique
Must be nearlythere!
Clicking download document gives a 505 server error. I checked the permissions and they are ok (all executable) for force-download.php.
You can try it from the link http://www.sowter.co.uk/WEBPAGE COLOURS.php
force-download.php WEBPAGE-COLOURS.php
Must be nearlythere!
Clicking download document gives a 505 server error. I checked the permissions and they are ok (all executable) for force-download.php.
You can try it from the link http://www.sowter.co.uk/WEBPAGE COLOURS.php
force-download.php WEBPAGE-COLOURS.php
ASKER
I think the problem is in this code:
// Check if file_number has been passed or not
if( isset($_GET['file_number'] ) && $_GET['file_number'] != '' )
{
// Read filename from URL.
$filename = $_GET['file_number'].'pdf' ;
// Code to check if files exists or not
$file_path = 'www.sowter.co.uk/colours/'.$filename;
// temporary code to print file path
echo $file_path;
if ( !file_exists($file_path) )
{
echo "<html><title>www.sowter.co.uk</title><bod y>ERROR 1: File not found. USE force-download.php?file=fi lenumber</ body></htm l>";
It returns the following:
www.sowter.co.uk/colours/0220pdfERROR 1: File not found. USE force-download.php?file=fi lenumber
There is no . before pdf
// Check if file_number has been passed or not
if( isset($_GET['file_number']
{
// Read filename from URL.
$filename = $_GET['file_number'].'pdf'
// Code to check if files exists or not
$file_path = 'www.sowter.co.uk/colours/'.$filename;
// temporary code to print file path
echo $file_path;
if ( !file_exists($file_path) )
{
echo "<html><title>www.sowter.co.uk</title><bod
It returns the following:
www.sowter.co.uk/colours/0220pdfERROR 1: File not found. USE force-download.php?file=fi
There is no . before pdf
ASKER
If I change the second line of code to
$filename = $_GET['file_number']'.pdf' ;
(moving the . to put it before pdf) I get server error 500
I think this is probably it but why do I get a server error?
$filename = $_GET['file_number']'.pdf'
(moving the . to put it before pdf) I get server error 500
I think this is probably it but why do I get a server error?
You are almost their.
$filename = $_GET['file_number'].'pdf' ;
It should be
$filename = $_GET['file_number'] . '.pdf';
$filename = $_GET['file_number'].'pdf'
It should be
$filename = $_GET['file_number'] . '.pdf';
Is it working?
ASKER
Hi Atique
Not quite. I think there is something wrong with the if statement:
if ( !file_exists($file_path) )
if I remove the ! it runs and I can download the pdf file. force-download.php
See: http://www.sowter.co.uk/WEBPAGE%20COLOURS.php (The ! is still in place.)
Another question: I want also to offer the possibility to display the pdf file. Where can I put this statement:
<a href="<?php echo '/colours/' . $type_number . '.pdf' ?> ">pdf file found click here</a>
My concern now is the error conditions with a non existing file.
I really appreciate your help. I am learning a lot!
brian
Not quite. I think there is something wrong with the if statement:
if ( !file_exists($file_path) )
if I remove the ! it runs and I can download the pdf file. force-download.php
See: http://www.sowter.co.uk/WEBPAGE%20COLOURS.php (The ! is still in place.)
Another question: I want also to offer the possibility to display the pdf file. Where can I put this statement:
<a href="<?php echo '/colours/' . $type_number . '.pdf' ?> ">pdf file found click here</a>
My concern now is the error conditions with a non existing file.
I really appreciate your help. I am learning a lot!
brian
ASKER
Basically it all works but one problem remains:
How can I make it give an error message if the file does not exist?
Thanks
How can I make it give an error message if the file does not exist?
Thanks
ASKER
I tried your latest force-download but still gives ERROR 1 even though there is a file there. MSG1 ( line 20) prints out the correct file path.
Also how can I make it display the pdf rather than download it?
Also how can I make it display the pdf rather than download it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I've requested that this question be deleted for the following reason:
Dont need the answer any more. problem went away. Thanks
Dont need the answer any more. problem went away. Thanks
I object to deleting this question. I posted a tested and working example on January 2 of this year. It's now mid-May. To refresh memory, here is a link that works perfectly, and has been working perfectly since January:
http://www.laprbass.com/RAY_temp_sowter.php?q=8345&a=L
Best regards, ~Ray
http://www.laprbass.com/RAY_temp_sowter.php?q=8345&a=L
Best regards, ~Ray
ASKER
I am bit puzzelled by this. i thought I had taken great care to acknowledge the work done by the contributors and awarded maximum points and did so on a timely basis.
As far as I remember there was a solution offered which was not only very complicated but was not actually able to be put on my site so i didn't regard this as solution. Obviously I dont want my business to rely on code I cant control.
The other solutions did not quite work as I hoped.
Have I missed something?
As far as I remember there was a solution offered which was not only very complicated but was not actually able to be put on my site so i didn't regard this as solution. Obviously I dont want my business to rely on code I cant control.
The other solutions did not quite work as I hoped.
Have I missed something?
ASKER
Good. Thanks
ASKER
I thought I had closed it??
http://www.yoursitename.com/force-download.php?file_number=1234
Replace yoursitename and 1234 with user entered value.
In the attached file you will need to change the path of folder.
Let me know if you need more details.
force-download.php