473,327 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

corrupted file

ddtpmyra
333 100+
When Im downloading a file from mysql database the file is already corrupted below are the code I used on downloading to mysql and uploading from mysql. PDF file are doing okay as long as it is a small size, but the excel and words are totally corrupted.

Can you please take a look if im doing it correctly.

Note: the filedata in mysql = longblob

Uploading to mysql


[PHP]<?php
# Check if a file has been uploaded
if(isset($_FILES['uploaded_file']))
{
# Make sure the file was sent without errors
if($_FILES['uploaded_file']['error'] == 0)
{
# Connect to the database
$dbLink = mysql_connect("xxx", "xxx", "xxx")
or die("Error! Failed to connect to the MySQL server!");
mysql_select_db("xxx", $dbLink)
or die("Error! Failed to select a database!");

# Gather all required data
$name = mysql_real_escape_string($_FILES['uploaded_file']['name'], $dbLink);
$mime = mysql_real_escape_string($_FILES['uploaded_file']['type'], $dbLink);
$size = $_FILES['uploaded_file']['size'];
$author = $_POST['author'];
$requestor = $_POST['requestor'];
$description = $_POST['description'];
$deadlinefeedback = $_POST['deadlinefeedback'];
$DateInput = $_POST['deadlinefeedback'];
$category =$_POST['category'];
$data = mysql_real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name']), $dbLink);

# Create the SQL query
$query = "
INSERT INTO fileStorage (
FileName, FileMime, FileSize, FileData, Created, Description,Author, Requestor, DeadLineFeedback, category
)
VALUES (
'{$name}', '{$mime}', {$size}, '{$data}', NOW(), '{$description}', UPPER('{$author}'), UPPER('{$requestor}'), '{$DateInput}','{$category}'
)";

# Execute the query
$result = mysql_query($query, $dbLink);

# Check if it was successfull
if($result)
{
echo "Success! Your file was successfully added!";
}
else
{
echo "Error! Failed to insert the file";
echo "<pre>". mysql_error($dbLink) ."</pre>";
}
}
else
{
echo "Error!
An error accured while the file was being uploaded.
Error code: ". $_FILES['uploaded_file']['error'];
}

# Close the mysql connection
mysql_close($dbLink);
}
else
{
echo "Error! A file was not sent!";
}

?>[/PHP]

Downloading files from mysql


[PHP]<?php
# Make sure a valid ID was passed
if(isset($_GET['id']))
{
# Get the ID
$id = $_GET['id'];

# Connect to the database
$dbLink = mysql_connect("xxx", "xxx", "xxx")
or die("Error! Failed to connect to the MySQL server!");
mysql_select_db("xxx", $dbLink)
or die("Error! Failed to select a database!");


# Fetch the file information
$query = "
SELECT FileMime, FileName, FileSize, FileData
FROM fileStorage
WHERE FileID = {$id}";

$result = @mysql_query($query, $dbLink)
or die("Error! Query failed: <pre>". mysql_error($dbLink) ."</pre>");

# Make sure the result is valid
if(mysql_num_rows($result) == 1)
{
# Get the row
$row = mysql_fetch_assoc($result);

# Print headers
header("Content-Type: ". $row['FileMime']);
header("Content-Length: ". $row['FileSize']);
header("Content-Disposition: attachment; filename=". $row['FileName']);

# Print data
echo $row['FileData'];
}
else
{
echo "Error! MySQL Query returned invalid results!";
}

# Close the mysql connection
@mysql_close($dbLink);

}
else
{
echo "Error! An invalid ID was passed!";
}
?>[/PHP]
Sep 27 '08 #1
1 1823
ddtpmyra
333 100+
anybody who had same encountered problem like this?
Sep 28 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: r.e.s. | last post by:
No matter which site I download it from, NumTut.tgz appears to be corrupted. Anyone else having this problem? Thanks. -- r.e.s.
1
by: WKC | last post by:
Recently, one of our database's mdf and ldf was corrupted. We were able to bring back the database with the capability of importing and querying the data. However, the data is not the full list. ...
2
by: Byron | last post by:
I am uploading a JPG file using FTP to a remote server and often the file is corrupted at the server end. The file size is the same, but the image often has a block of garbage embedded, or the...
3
by: Bob Hynes | last post by:
Hi All, Does anyone know of a place where a corrupted mdb(front-end) can be sent and have that place be able to tell me what got corrupted within the db? Here's the issue; All pc's are WindowsNT...
28
by: Lee Rouse | last post by:
Hello all, This is going to be a rather lengthy "question". I have an Access 2k database, separated front end/back end. Front end copies are on about 30 workstations and used frequently during...
9
by: advance scout | last post by:
HELP! My database is suddenly corrupted. My computer was acting funny (very sluggish) and was shut down. Access had been already been closed down but computer was acting very slow , so perhaps it...
3
by: Jeremy Chaney | last post by:
It appears that my user.config file got corrupted causing my "InitializeComponent" routine to throw the exception "Root element is missing." when I start my app. I figure I can just go into...
7
by: jc | last post by:
i'm developing a project using vc++. the main exe is a win32 application. it needs two dlls. one is my own implementation of string operations. the other dll is to parse a2l files(it is similar to...
4
by: =?Utf-8?B?U1VOTlk=?= | last post by:
Hi, i am uploading a .docx file into sql2005 and later when i retrieve the file from the database and i open it, i get a error message "The file is corrupted and cannot be open". I am not facing...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.