[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Php file upload doesn't work

Posted on 2012-04-04
4
Medium Priority
?
474 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37805705
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 31

Expert Comment

by:Marco Gasi
ID: 37805724
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 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 37805725
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
ID: 37810617
base64_encode() solved the problem
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video teaches users how to migrate an existing Wordpress website to a new domain.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

649 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