Ok, check /app/config/app.php to be sure that debug is set to true then, if this is still your table structure:

CREATE TABLE docs (
    gov_docs_id int(11) NOT NULL AUTO_INCREMENT,
    personnel_id int(11) DEFAULT NULL,
    docs_name varchar(255) DEFAULT NULL,
    docs_num int(11) DEFAULT NULL,
    docs_date datetime DEFAULT NULL,
    filename varchar(255) DEFAULT NULL,
    data longblob,
    PRIMARY KEY (gov_docs_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Change it, to add the columns size and mime, just run these commands in a MySQL client:

alter table docs add size int unsigned not null default 0;
alter table docs add mime varchar(255) not null;

Then change your code to:

Route::get('/getfile/{id}', function($id)
{
    $file = DB::select('SELECT * FROM docs WHERE gov_docs_id = ?', array($id));
    $data = $file[0]->file;
    return Response::make($data, 200, array('Content-type' => $file[0]->mime, 'Content-length' => $file[0]->size));
});

In order to work fine you have to change also the script used to save the file to the database and add the mine and size columns:

$mime = Input::file('upload')->getMimeType();
$size = Input::file('upload')->getSize();

DB::table('docs')->insert(array(
    'doc_attachment'=> $data,
    'mime' => $mime,
    'size' => $size
    )
);

Remember to change tablename to your current table name (i.e. docs), and also refer to the correct id, which in your table is gov_docs_id. If the problem persists check che error log in /app/storage/logs/ and Apache error log.

hi cereal

what 200 stands for on the get file function.

i tried to download doc files but it look like a tmp file when i tried to open it displays the filename of the docs.

When i tried to download the jpg files it goes to the next page and error .
the error message was cannot be displayed because it contains error

thanks fo help

what 200 stands for on the get file function.

200 is the HTTP status used for successful HTTP requests, for more information check:

I suppose there is some broken data in your table, because of the previouses tests. Could you truncate the table and start some new uploads? Use:

truncate table docs;

This will delete all the previous rows. If, after doing this operation, it doesn't work, please share your updated and working code, related to your updated database table.

it doesnt work

here is my updated code:

For downloading

Route::get('/getfile/{docs_id}', function($id)
{
    $file = DB::select('SELEC FROM doc_files WHERE docs_id = ?', array($id));
    $data = $file[0]->filename;
    return Response::make($data, 200, array('Content-type' => $file[0]->mimetype, 'Content-length' => $file[0]->size ) );
}
);

For uploading :

  $data = file_get_contents(Input::file('upload')->getRealPath()));
        $filename = Input::file('upload')->getClientOriginalName();
        $mimetype = Input::file('upload')->getMimeType();
        $size = Input::file('upload')->getSize();
         if (Input::hasfile('upload'))
          {
            DB::table('doc_files')->insert( 
                array(
                'data'=>$data,
                'mimetype' =>$mimetype,
                'filename'=>$filename,
                'size' => $size

                )
            );  

          } 
            else 
{
                echo "not successful";
}

resending the code for download

Route::get('/getfile/{docs_id}', function($id)
{
    $file = DB::select('SELECT * FROM doc_files WHERE docs_id = ?', array($id));
    $data = $file[0]->filename;
    return Response::make($data, 200, array('Content-type' => $file[0]->mimetype, 'Content-length' => $file[0]->size ) );
}
);

Ok, the variable $data is used for the binary data not for the file name, so change the line 4:

$data = $file[0]->filename;

to:

$data = $file[0]->data;

And it should work fine.

i already tried to change it but it seems it was corrupted .
when i tried to download it. image will be corrupted.

and when i tried to upload videos.
I experience error here is the error

Call to a member function getRealPath() on a non-object

Wait, how big are these files?

Also, try to download one of these files through a test script, for example:

<?php

# database credentials
$dbhost = 'localhost';
$dbname = 'dbname';
$dbuser = 'dbusername';
$dbpass = 'dbpassword';

try
{
    $db = new PDO("mysql:host={$dbhost};dbname={$dbname}", $dbuser, $dbpass);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}

catch(PDOException $e)
{
    echo "<p>".date('Y-m-d G:i:s') . " [ERROR]: " . $e->getMessage() . "</p>\n\r";
    $db = null;
}

$id = $_GET['id'];
$stmt = $db->prepare("SELECT * FROM docs_file WHERE docs_id = :id limit 1");
$stmt->execute(array(':id' => $id));

$row = $stmt->fetch(PDO::FETCH_ASSOC);

header('Content-type: '. $row['mimetype']);
header('Content-length: '. $row['size']);

echo $row['data'];

Save it as getfile.php in /public/ and then from the browser call:

http://localhost/getfile.php?id=1

If it still does not work, for example returns file corrupted, open the web developer console, it should open by pressing CTRL + SHIFT + J in Google Chrome, select the Network tab and reload the link, it will appear something like this:

getfile.php?id=1

right click on it and select Copy Response Headers, you should get something like this:

HTTP/1.1 200 OK
Date: Fri, 27 Jun 2014 15:47:27 GMT
Server: Apache
Content-Length: 334549
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/pdf

A better tool to debug these kind of scripts is httpie, if you want to try:

Or Postman, an extension for Google Chrome:

Both will return also the body response and if there is an error in the code, you will see it, for example:

Notice: Undefined index: data in /path/getfile.php on line 30
Call Stack:
0.0001     327424   1. {main}() /path/getfile.php:0

Hi Sir ,

why is it that i cant anymore upload file.

i've encountered getRealPath() error

why is it that i cant anymore upload file.

Hi, it can depend on many factors. How big are these files? The default file size limit for the uploads in PHP is 2MB. Check in your PHP configuration the values of post_max_size, upload_max_filesize and memory_limit:

You can access to this information reading the file php.ini or through phpinfo():

<?php

    phpinfo();

Also try to return the $_FILES array from the upload method, just place the return at the top of the method to output directly the array contents:

Route::post('/upload_file', function()
{
    return "<pre>".print_r($_FILES, true)."</pre>";

    # ... other code ...

}

If it works fine you get:

Array
(
    [upload] => Array
        (
            [name] => userguide.pdf
            [type] => application/pdf
            [tmp_name] => /tmp/phpW1NMHq
            [error] => 0
            [size] => 448075
        )

)

If you get an empty array, then you're probably hitting one of the above limits. And if you check the error log of your web server you will find the occurring error:

[Sat Jun 28 13:30:14 2014] [error] [client 127.0.0.1] PHP Warning:  POST Content-Length of 22982481 bytes exceeds the limit of 20971520 bytes in Unknown on line 0, referer: http://localhost/upload_form

Note: regarding my previous example the table name is not correct, I wrote docs_file instead of doc_files... so if you want to try be aware of this mistake.

Hi Cereal ,

I can now upload large files the only problem is that the Video,Docs and image are corrupted everytime i tried to download.

when i tried to save it from database in a mysql workbench it is ok.

thanks

when i tried to view it on Mysql Workbench Files is ok

only Pdf file is ok when i download

using your code getfile.php
it is sucessfully downloaded no corrupted files

but when i use our code on downloading the files when i tried to retrieve docs,video, and image it was corrupted .
thanks.

hi Sir Cereal Thanks for the help i can now download and upload using your codes as my reference thank you so much

You're welcome and, please, mark the thread as solved. Bye! :)

Wow thks both of you, because of asking and answering, it saved my life :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.