Solved

Php file upload doesn't work

Posted on 2012-04-04
4
461 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 110

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 110

Accepted Solution

by:
Ray Paseur earned 500 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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

724 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