By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,216 Members | 1,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,216 IT Pros & Developers. It's quick & easy.

corrupted file

ddtpmyra
100+
P: 333
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
Share this Question
Share on Google+
1 Reply


ddtpmyra
100+
P: 333
anybody who had same encountered problem like this?
Sep 28 '08 #2

Post your reply

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