Solved

Php file upload doesn't work

Posted on 2012-04-04
4
438 Views
Last Modified: 2012-04-05
Hello,

I have a large blob field in a Mysql table.

When upload a file with phpmyadmin into this field, it works fine.

But when i use the script below to upload, the file is broken when downloading.
I tested with small and big files too.

Upload script :
$tmpName = $_FILES["file"]["tmp_name"];
$fileName = $_FILES["file"]["name"];
$fileType = $_FILES["file"]["type"];
$fileSize = $_FILES["file"]["size"];
$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
$insert = "INSERT INTO bizt_fileok (ajanlat_id, file_name, file_size, file_type, content)  VALUES ('$maxId', '$fileName', '$fileSize', '$fileType', '$content')";

Database Structure: id (int 11 auto_inc), ajanlat_id (int 11),  file_name(varchar45), file_size (int 11), file_type (varchar45), content (longblob).

Download script:
header('Content-Type: '.$file["file_type"].'');
header('Content-Disposition: attachment; filename="'.$file["file_name"].'"');
echo $file["content"];

The script above worked a few weeks ago and then it stopped working properly.
Can it be a server side problem or my code is bad?

Thank you
0
Comment
Question by:starhu
  • 2
4 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
I have a large blob field in a Mysql table.
Don't do that.  Instead store the "large blob" in the server file system.  Data bases are not well designed to store blobs.
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
Maybe you have simply forgot to write this part in your question, but I don't see a $maxId definition in your code: if that is missing, your query won't work. Have you tried to run your script using mysql_error() function?

$insert = "INSERT INTO bizt_fileok (ajanlat_id, file_name, file_size, file_type, content)  VALUES ('$maxId', '$fileName', '$fileSize', '$fileType', '$content')";

mysql_query($uinsert) or die("Problem with the query :" . mysql_error() . "<br>The query was:<br>" . $insert);

This could show you where is the problem...

Cheers
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
There are many things that can go wrong with the script, and they are largely dependent on the external data that the script receives.  You may want to try getting rid of addslashes and using mysql_real_escape_string() instead.  You may want to use base64_encode() to make the external data safe for binary transport.  You may want to read about the risks associated with file uploads, particularly since the code does not have any checks to see if the information is actually an uploaded file!
http://us.php.net/manual/en/function.is-uploaded-file.php
0
 

Author Closing Comment

by:starhu
Comment Utility
base64_encode() solved the problem
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Read about why website design really matters in today's demanding market.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now