When doing an include or require connection the file download with php does not start and opens a page full of strange characters [duplicate]

-1

I have the following code

require_once('connection.php');

    $query = ("SELECT * FROM tabela Where id='".$_GET['id']."'");
    $result = mysqli_query($link,$query);

    if (mysqli_num_rows($result) > 0) {
        $arquivo = 'imagem.jpg';
        header('Content-type: octet/stream');
        header('Content-disposition: attachment; filename="'.$arquivo.'";'); 
        readfile($arquivo);
    }else{
        echo "Link inválido.";
    }

mysqli_close($link);

I have tried with include too and I did not succeed!

    
asked by anonymous 18.02.2017 / 11:36

3 answers

1

Good morning,

Place the headers at the beginning of the code, do not require the absolute path of the file.

Inserting the direct database connection data into the file is not a good choice, it is the right thing to import the connection file.

I also recommend playing the query inside a try to handle the exceptions.

<?php 

    ini_set('display_errors', 1);
    ini_set('log_errors', 1);
    ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
    error_reporting(E_ALL);

    header('Content-type: octet/stream');
    header('Cache-Control: max-age=0');
    header("Content-type: application/force-download");  
    header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
    header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
    header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
    header ('Pragma: public'); // HTTP/1.0

     require_once(dirname(__FILE__).'/../../application/models/Conexao.php');


try{

 $id = filter_input(INPUT_GET, "id");
 $sql = "SELECT * FROM tabela WHERE id = {$id} ";
$result = mysqli_query($link,$sql);

    if (mysqli_num_rows($result) > 0) {
        $arquivo = 'imagem.jpg';
        header("Content-Disposition: attachment;filename={$arquivo}");
        readfile($arquivo);
    }else{
        echo "Link inválido.";
    }

}catch(Exception $ex){

print_r($ex);

}finally{
mysqli_close($link);
}

?>
    
18.02.2017 / 15:55
1

First let's dissect your code, to which day the part of the error, because syntactic analysis in computing is for the debugger. We'll follow in stages, okay?

Let's talk about each type of php file:

Re-evaluate the use of these functions and try adding the connection script directly to the page that searches the database

require - > require

  

The require statement is identical to include except that on failure it will also produce a fatal error of level E_COMPILE_ERROR. In other words, it will stop the script while the include will only issue an alert (E_WARNING) allowing the script to continue.

include - > include

  

The include statement includes and evaluates the reported file.   The following documentation also applies to the require statement.

     

Files are included based on the path of the given file or, if not entered, the specified include_path. If the file is not found in the include_path, the include statement will check the directory of the script that executes it and the working directory before it fails. The include constructor will issue a warning if it does not locate the file; behaves differently from the require constructor, which will issue a fatal error.

     

If a path is defined - either absolute (starting with drive letter or \ on Windows, or / on Unix / Linux), or relative to the current directory (starting with .or ..) - the include_path will be completely ignored . For example, if the file name starts with ../, the interpreter will look for the file in the parent directory.

Already another problem that does not really exist, these letters are what you want. It will not open the image, the script is opening the uncompressed jpg file and throwing the text file into the screen:

    '$arquivo = 'imagem.jpg';
    header('Content-type: octet/stream');
    header('Content-disposition: attachment; filename="'.$arquivo.'";'); 
    readfile($arquivo);'

If you want to open an image you should do so:

<?php
// supondo que $caminho seja o nome da variável
echo '<img src="' . $caminho . '" />';
?>

See if that's what you need and vote for it if my suggestion is reasonable to solve your problem!

    
18.02.2017 / 16:26
-1

Try to place the connection code directly on the page instead of the require

    
18.02.2017 / 14:50