PDA

View Full Version : Downloading Files Stored In MySQL


Acid
04-30-2007, 12:36 PM
I wrote this script a little while ago and it's been working fine, now all of a sudden its decided it doesn't want to work anymore and I have no idea why.

I have a bunch of files stored in a MySQL database using BLOB fields, now for some reason when the files are opened the document is corrupted and wont open. Here's the code I'm using to download the files out the database:

<?php

error_reporting(E_ALL ^ E_NOTICE);
require_once("MySQL.php");

$Host = "**********";
$UserName = "**********";
$Password = "**********";
$Database = "**********";

$Vacancies = new MySQL;
$Vacancies->Connect($Host, $UserName, $Password, $Database);

if ($_GET['JobDescID'] || $_GET['KSFID']) {

if ($_GET['JobDescID']) {
$GetFileQuery = $Vacancies->Query("SELECT `JDFileName`, `JDFileType`, `JDFileSize`, `JDFileData` FROM `Files` WHERE `ID` LIKE '" . $Vacancies->Real_Escape_String($_GET['JobDescID']) . "'");

} else if ($_GET['KSFID']) {
$GetFileQuery = $Vacancies->Query("SELECT `KSFFileName`, `KSFFileType`, `KSFFileSize`, `KSFFileData` FROM `Files` WHERE `ID` LIKE '" . $Vacancies->Real_Escape_String($_GET['KSFID']) . "'");

}

if ($Vacancies->Num_Rows($GetFileQuery) == 0) {
$Vacancies->Close();
header("Location: /Vacancies/");
die();

} else {
list($FileName, $FileType, $FileSize, $FileData) = $Vacancies->Fetch_Array($GetFileQuery);
header("Content-length: " . $FileSize);
header("Content-type: " . $FileType);

if ($_GET['Action'] == "Download") {
header("Content-Disposition: attachment; filename=" . $FileName);

}

echo $FileData;
$Vacancies->Close();

}

} else {
$Vacancies->Close();
header("Location: /Vacancies/");
die();

}

?>You can see the problem for your self at the following address:

http://www.tewv.nhs.uk/Vacancies/

Click on anyone of the jobs listed on that page to pull up the job details, at the bottom of the details page you'll see the links to view or download the job descriptions or KSFs (which are the files that wont open that are stored in the database), for instance:

http://www.tewv.nhs.uk/VacancyDetails/65b0d64feb38840167bd451d5adef9b6/

You'll see the links at the bottom of this page. Any help with this would be great, no idea why this has suddenly decided to stop working.

rafiki
04-30-2007, 12:46 PM
wow worked for me

Acid
04-30-2007, 01:04 PM
It works for you? When I try to view the files it tries to open GetFiles.php and then tells me its corrupted, if I go to download it it states the correct file name but stil tells me its corrupt?

Its definitely working for you? Strange, as if I wasnt confused enough already.

rafiki
04-30-2007, 01:10 PM
it downloaded the file not sure what it contained ill try and post the contents of the file now

rafiki
04-30-2007, 01:14 PM
MIDDLESBROUGH INTEGRATED MENTAL HEALTH SERVICE

JOB DESCRIPTION

Job Title: Crisis Resolution Worker

Managerially Team Manager - Crisis Resolution Team
Accountable to:

Professionally
Accountable to: Team Manager - Crisis Resolution Team

Grade: Band 6

Hours: 37 ½ (flexible – out of hours work and weekend work will be required)

Operational Base: 11a Sunningdale Road
Saltersgill
Middlesbrough

Liaises with: Other Crisis Team members, The Access Team, members of other agencies and disciplines responsible for a client’s care, carers, general public, voluntary agencies, user and carers groups, Patient Liaison, Social Services and Police.

works fine apart from quite a few unknown symbols
ÐÏࡱá
for example

Acid
04-30-2007, 01:30 PM
Still isnt working for me, just gives me corrupt documents whether i try to view or download...

rafiki
04-30-2007, 01:41 PM
try downloading firefox (link in my sig) which i am using using xp home opening with wordpad

Acid
04-30-2007, 01:58 PM
I am using Firefox ;) Tried this in IE as well and get the same result.

Just tried opening it with Wordpad and I see what you see with all the unreconised characters so in effect you are having the same problem as me (try opening the document with Word), it doesnt seem to be streaming it or whatever correctly for some reason.

rafiki
04-30-2007, 02:10 PM
i dont have word sorry

Acid
04-30-2007, 02:17 PM
OK it seems to be working now, played around with the script some more and uploaded a new file, now it works and will open the file correctly, if you save it though it appears to truncate the file name and drop the extension off the end but opens correctly? Ahh well it'll do, the files open. Cheers for the help rafiki ;)