473,473 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Uploading doc,pdf,jpg,png etc as BLOBs

384 Contributor
I've found may scripts and examples on how to upload images but don't have a clue how to upload a file like doc or pdf??
Feb 3 '09 #1
12 2947
Atli
5,058 Recognized Expert Expert
Hi.

Uploading images is pretty much the same process as uploading any other file. The only difference would be how you validate the file once it has been sent.

Check out my article: http://bytes.com/topic/php/insights/...base-using-php.
It explains how to upload any file, not just images.
Feb 4 '09 #2
ziycon
384 Contributor
i'm having a problem checking that something is being entered into the input box, i'm trying the below which is now working:
Expand|Select|Wrap|Line Numbers
  1. if(!isset($_FILES['uploaded_file'])) {
  2.   File not entered into input box.
  3. } else {
  4.   A File has been entered.
  5. }
It just always goes to the else statement???
Feb 5 '09 #3
Markus
6,050 Recognized Expert Expert
The file input is passed regardless of whether there was a file present. You can check if the file was uploaded using the error numbers passed with the array
Feb 5 '09 #4
ziycon
384 Contributor
Got it sorted using error code 4, thanks.
Feb 5 '09 #5
ziycon
384 Contributor
I got files uploading and downloading fine, the only problem i've come across is that when the file is downloaded to the desktop it hasn't got any extension, its an unknown file!???
Feb 5 '09 #6
Atli
5,058 Recognized Expert Expert
You have to tell the browser what to name the file by setting the Content-Disposition header. Some browsers might be able to determine the extensions of well known mime-types, but you should always provide it to be safe.
Feb 5 '09 #7
ziycon
384 Contributor
I'll look into this, can you also point me in the direction of how to restrict certain file types for upload?
Feb 5 '09 #8
Atli
5,058 Recognized Expert Expert
@ziycon
To do that, you would have to check the file after the file is uploaded, see if it is actually what you were expecting. If it doesn't pass this check, you simply don't add it to your database.

Depending on the type of the file you want restricted, you would have to come up with a way to determine whether or not the file is actually what it is supposed to be. It is often tempting to use the mime type sent with the file, but this is sent by the browser so it can not be trusted.

If it is supposed to be an image, PHP's getimagesize function can help you. It provides the actual mime-type, so you can verify against that.

If it is supposed to be a XML file, or something of that nature, you could try parsing it, see if it will parse correctly. If it does, it can be considered valid.
Feb 5 '09 #9
ziycon
384 Contributor
It can be any one the below:
jpg,gif,png,doc,pdf
Feb 5 '09 #10
Atli
5,058 Recognized Expert Expert
For the images, use the getimagesize function I linked in my previous post. If the images are in fact valid, the function should return an array with all the info on it, or FALSE if they are invalid.

For the PDF, if the first 5 characters of the PDF file are "%PDF-", that would indicate that the file is actually a PDF file.
That can be done using the File System functions, like:
Expand|Select|Wrap|Line Numbers
  1. // Get the first 5 chars
  2. $fh = fopen("/path/to/file.pdf", "r") or die("Could not open file");
  3. $head = fgets($fh, 5);
  4. fclose($fh);
  5.  
  6. // Check if they are valid
  7. if($head == '%PDF-') {
  8.   echo "PDF file is valid!";
  9.   // Proceed with the insert process.
  10. }
  11. else {
  12.   echo "This is not a PDF file";
  13. }
Note, however, that this only establishes that the file identifies itself as a PDF file, according to the PDF specification. It does not check if the file is actually a valid PDF file (For example, a text file who's first 5 characters are '%PDF-' would also be considered valid.)

As to the DOC type, I can't really help you. I never use proprietary M$ stuff if I can help it.
But if your server runs on Windows, and the server has Word installed, I suppose you could load the document through a COM object. See if it actually loads properly.
Feb 5 '09 #11
ziycon
384 Contributor
Lovely tutorial by the way, got it all working perfectly.

Thanks,
ziy
Feb 5 '09 #12
Atli
5,058 Recognized Expert Expert
@ziycon
Thank you :]
Glad you got it all to work.
Feb 5 '09 #13

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

Similar topics

5
by: Rob | last post by:
Ummm...I'm a little stuck on how to approach the following.... I have a mySQL table holding images (fields are: data (blob), name (text), type (text), size(text), desc (text) ) If I want to...
0
by: Ole Hansen | last post by:
Hi, Is it at all possible to insert BLOBs using the Array Interface? Today I have an application using the array interface. It works fine but so far I haven't been using BLOBs. I insert...
6
by: Marcus | last post by:
Hi all, Quick question... I am storing jpegs as blobs in a mysql table, which is fine on my live server because I am just uploading them using phpmyadmin (I am working with a small number of...
7
by: Howard Lowndes | last post by:
My situation is that I am interacting PHP 4.1.2 to PostgreSQL 7.2.2 I have no difficulty inserting and managing BLOBs into the Large Object system table, and I have a user table called images...
7
by: Nilabhra Banerjee | last post by:
Hi, I am still not sure whether the BLOBS are actually stored in the database or they have the pointer to the database for that file in the filesystem. If I remove the files (sources) for BLOBS...
0
by: Bing | last post by:
Hi there, I am using the DB2 universal JDBC driver type 4 to insert BLOBs into a DB2 database. The Method I used for supplying the BLOB data value is setBinaryStream(). Everything works fine as...
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...
0
by: .Net Newbie | last post by:
I am currently working with the C# version of the PortalSDK in a text editor and have run across a bug that I need to resolve in order for my app to function properly. Basically, I'm using the...
0
by: .Net Newbie | last post by:
Basically, I'm using the Documents section of the PortalCSSDK to load Blobs into the Portal Database on SQL Server. When I upload small binary documents everything works fine. However, when I try...
2
by: Jerry LeVan | last post by:
Hi, I am just getting into large objects and bytea "stuff". I created a small db called pictures and loaded some large objects and then tried to do a restore. Here is how I got the dump. ...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.