0

I have files like pdf in a directory and i have created a search form where in the user will input the filecode and it will search the source directory if it matches a filename it will allow the user to download the file, if not it will show "No Results" Here's what i did, but I am not sure what code should I use to download a file from my source directory, I just saw the code for downloading a file somewhere and substitute the source file with the variable i use when a user search

<html>
          <head>
            <title>Search  Contacts</title>
          </head>
          <body>

            <h3>Search Client File</h3>
            <form  method="post" action="#"  id="searchform">
              Type the File Code:<br><br>
                  <input  type="text" name="fcode">
            <br>
      <input  type="submit" name="submit" value="Search">
            </form>

<?php
$filecode=$_POST["fcode"];
     if (!empty($filecode))
     {
 $ch = curl_init();
$source = "/sdir/$filecode.pdf";
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);

$destination = "/tdir/afile.pdf";
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);
     }
     else
     {
       echo "No Results";
     }

        ?>
    </body>
    </html>
Xander Vane
  • 197
  • 2
  • 4
  • 20
  • what do you mean by "that it doesnt work and show anything."? Do you see the form, what happens when it doesn't work? error messages, white page, No result? Please be a little more clear. – AgeDeO Jan 30 '15 at 15:16
  • `$file=""` what is this meant to be? – Martin Jan 30 '15 at 15:22
  • Sorry, I have now edited my question, I honestly dont know what code should I use to download the file from my source dir – Xander Vane Jan 30 '15 at 15:31

1 Answers1

0

I think your issue is relating to the line $file="<sourcefile?>" as this doesn't appear (at least to me) to make sense. However, you will get a better idea of your issues if you make sure that PHP has error logging turned on and then look up your PHP error log file.

something like (At the top of your PHP page):

Sorry this code is older:

ini_set("log_errors", 1);
ini_set("error_log", "/tmp/php-error.log");

see: https://stackoverflow.com/a/3531852/3536236

Newer way of doing it:

    ini_set("error_log", "/tmp/php-error.log");
    error_reporting(E_ALL);

This will mean that you can then download and view the log file "/tmp/php-error.log" and this will explain why PHP failed, if it did indeed fail.

I'm sorry I can't give you a more exact answer but this will show you exactly what's wrong rather than a somewhat vague "it doesn't work".

How to download a file

http://www.media-division.com/the-right-way-to-handle-file-downloads-in-php/

https://stackoverflow.com/a/3320743/3536236

I think after following these two links you should have it working.

Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132
  • Sorry, I have now edited my question, I honestly dont know what code should I use to download the file from my source dir – – Xander Vane Jan 30 '15 at 15:32
  • my updated anwser works on the code you'd have to make to find the file, and get the file address/name as a $var. Try using `glob` or similar means to load the correct file. – Martin Jan 30 '15 at 15:37
  • Thanks for this, but Im pretty much a newbie in php, I dont know how to put the one indicated on the links to my script, it seems that they are not complete – Xander Vane Jan 30 '15 at 15:54