473,382 Members | 1,583 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,382 software developers and data experts.

Uploading xlsx file to mysql database?

I am uploading a file to a mysql database table. Now let me say that this works perfectly fine for PDF, XLS, and DOC files. However if I upload an XLSX (office 2007 file), it uploads fine, however when it is downloaded, it cannot be read.

I can open both the original uploaded file and the one I downloaded in wordpad and they look EXACTLY the same.

HELP!

Additionally I am noticing that with Office 2007 files, the mime type is being truncated at 50 in my PHP (verified with a simple echo)

The mime type of "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
is truncated to "application/vnd.openxmlformats-officedocument.spre"

However if I manually set the mime type in the database to the correct value, it still cannot be opened correctly.

Any advice?
Thank you in advance for your time!
(Please be aware that these are not the full scripts. I know theres more to it and I do some validation here and there. I thought I could skip the common fluff)


Insert.php
Expand|Select|Wrap|Line Numbers
  1. $name = mysql_real_escape_string($_FILES['c_filename']['name']);
  2. $mime = mysql_real_escape_string($_FILES['c_filename']['type']);
  3. $data = mysql_real_escape_string(file_get_contents($_FILES['c_filename']['tmp_name']);
  4. $size = intval($_FILES['c_filename']['size']);    
  5.  
  6. // I do some validation between then and now... Not going to bore you with that part
  7.  
  8. $query = "INSERT INTO `$filetable` 
  9.      (filename, filetype, filesize,bin_data)
  10. VALUES
  11.     ('{$name}', '{$mime}', {$size}, '{$data}')";
  12. $result = mysql_query($query)
  13. or die ("Invalid query!!!<br><br>".$query);
  14.  
Download.php:
Expand|Select|Wrap|Line Numbers
  1. //sql query was executed that selected that record.
  2.  
  3. $name   = mysql_result($result,0,"filename");
  4. $size   = mysql_result($result,0,"filesize");
  5. $type   = mysql_result($result,0,"filetype");
  6. $content= mysql_result($result,0,"bin_data");
  7.  
  8. header("Content-type: $type");
  9. header("Content-length: $size");
  10. header("Content-Disposition: attachment; filename=$name");
  11. echo $content;
  12.  
Aug 27 '10 #1
3 7769
Atli
5,058 Expert 4TB
Hey.

Additionally I am noticing that with Office 2007 files, the mime type is being truncated at 50 in my PHP (verified with a simple echo)

The mime type of "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
is truncated to "application/vnd.openxmlformats-officedocument.spre"
Looks like your table is limiting the amount of chars you can put into your "filetype" field. If it is defined like: "filetype VarChar(50)", MySQL would automatically, and silently, discard all but the first 50 chars you try to put into it. You need to up the limit on it if you want to use more.

I can open both the original uploaded file and the one I downloaded in wordpad and they look EXACTLY the same.
How so? I was under the impression that M$ Office files were stored compressed. What does WordPad show you when you open them? (I'd try but I neither have M$ Office, nor a Windows box to try it on xD)

Is there any difference in the sizes of the two files? Does Excel give you any usable error messages when it fails to open the downloaded file?

The two most common errors I've seen in situations like these are:
  1. There is an error/warning/notice being printed in the download page, which is corrupting the source of the file you are trying to download. The quick, dirty and often useless fix for this is to just silence the error by turning of error reporting; the proper way to deal with this is to find the error and fix it.
  2. There is a charset problem. Best way to check this is to make sure everything from the HTML page that shows the form to the columns in the database are set to the same charset. A simple way to test this is to submit a plain text file, encoded in the charset you are using, with special chars and see if they get messed up in transit.
Aug 27 '10 #2
THanks for the quick response.

For the mime type, its not the database that is truncating the value, its PHP. If I do an echo $mime, I get the truncated value.

Second, I was just comparing the files in a HEX editor. It looks like when my files are being downloaded (or even uploaded) there is one extra blank space at the end. That is the only difference I can see from comparing hex values.
Aug 27 '10 #3
Atli
5,058 Expert 4TB
For the mime type, its not the database that is truncating the value, its PHP. If I do an echo $mime, I get the truncated value.
Is that the $mime variable you set on line #2 of the Insert.php code you posted above? If so, then the problem would appear to be with the browser. At that point in the code there has been no modifications made to the value presented by the browser, with the exception of the mysql_real_escape_string function which would not do this.

PHP does not truncate string values out of the blue; there must be something either wrong with the data PHP is receiving (either from the browser or from the database) or some function or logic applied to the data is behaving unexpectedly.

Could you show us all lines that affect the value of the $mime field, from the HTML form where the file is submitted to the header call when the file is downloaded? If PHP is indeed truncating it, that would help us figure out why.

Second, I was just comparing the files in a HEX editor. It looks like when my files are being downloaded (or even uploaded) there is one extra blank space at the end. That is the only difference I can see from comparing hex values.
Seems a very extreme reaction to such a small change. Did you try manually removing the extra space and seeing if the file will open then?

I would try debugging this further by calculating the MD5 checksum for the file at various points in your app. I suggest before uploading, before inserting into MySQL and after fetching it from MySQL. Might tell you at which point in the process the problem is created.
Aug 27 '10 #4

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

Similar topics

2
by: Bartosz Wegrzyn | last post by:
I followed the tutorian on http://www.phpbuilder.com/columns/florian19991014.php3 unfortunalety I receive this error Warning: fread(): supplied argument is not a valid stream resource in...
7
by: aa | last post by:
If I have an MySQL database on my local w2k, how do I transfer it onto my ISP server? The ISP gave me the name of an empty MySQL database they set up for me, the username and the password with no...
10
by: John Smith | last post by:
I know that uploading an image to a database has been covered, oh, about 3 trillion times. However, I haven't found anything covering uploading to a MySQL database with .net. Please don't...
1
by: Glenn Coyle | last post by:
Hi I am having trouble writing the file path of a image to the database,also for some reason it is not uploading the images, anyone have any ideas? the code is below: if($_POST ==...
5
tolkienarda
by: tolkienarda | last post by:
hi all i need to allow people to upload images to my site and then save them in a mysql database. i've looked at some prebuilt scripts using iframes but i don't think that will work for me. I want...
6
by: chiya | last post by:
plz anybody help me in uploading data from excel file to mysql database. data in excel sheet is in vertical format not horizontally. I mean to say that the columns of mysql tables are in rows in...
2
by: wingleader | last post by:
Hi! I'm looking for ways of uploading image file to mysql database. I know, that it should be possible, but don't know how to do it.... could someone help me?
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
2
by: simonyong | last post by:
Hello, anyone I had search for few days with how to save file when user choose a file name from listbox and i will search the file from database and user can save it into their desktop what I had...
0
by: user1980 | last post by:
hello.. i am developing a page in asp to open an .xlsx file and display its contents.. i have used this connection string sConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.