This topic is locked

Store File in Database

4/3/2010 3:03:15 PM
PHPRunner General questions
M
mrphp author

Hello,
I have a database for tracking purchases. As part of the purchase record I would like to keep a copy of the PDF receipt. Currently I'm uploading the receipt to a directory but I would like to change to uploading into the database.

I watched the tutorial on uploading files but it doesn't cover uploading to the database. How is this done? On the "Attachment Edit As" window I don't see a way to tell it to upload to the database.
My initial understanding was that if the field type is "text" the file would be linked to an upload directory and if the field type is "blob" the file would be stored in the database. However, I created a field type of blob and it still wants to upload the file to a directory. What am I doing wrong?
Thanks for your help.

S
swanside 4/3/2010



Hello,
I have a database for tracking purchases. As part of the purchase record I would like to keep a copy of the PDF receipt. Currently I'm uploading the receipt to a directory but I would like to change to uploading into the database.

I watched the tutorial on uploading files but it doesn't cover uploading to the database. How is this done? On the "Attachment Edit As" window I don't see a way to tell it to upload to the database.
My initial understanding was that if the field type is "text" the file would be linked to an upload directory and if the field type is "blob" the file would be stored in the database. However, I created a field type of blob and it still wants to upload the file to a directory. What am I doing wrong?
Thanks for your help.


You need three fields, I tried this, Created a table with three fields, In the fields page in PHPRunner, unselect the field Filename in the Add and edit pages.

In the visual editor, double click on the file field, choose edit as File and select the field filename for the filename. On the View as tab select to view as File.

WHen I build the project, I uploaded a file called Rawthorpe and it stores it as a BLOB, with the BLOB data, The File ID as 4 and the filename as RAWTHORPE.txt

-- phpMyAdmin SQL Dump

-- version 3.2.0.1

-- http://www.phpmyadmin.net

--

-- Host: localhost

-- Generation Time: Apr 03, 2010 at 09:37 PM

-- Server version: 5.1.36

-- PHP Version: 5.3.0
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--

-- Database: import

--
-- --------------------------------------------------------
--

-- Table structure for table table1

--
CREATE TABLE IF NOT EXISTS table1 (

file longblob,

ID int(11) NOT NULL AUTO_INCREMENT,

filename varchar(50) DEFAULT NULL,

PRIMARY KEY (ID)

) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
--

-- Dumping data for table table1

--
INSERT INTO table1 (file, ID, filename) VALUES

(0x323031302d30332d32332030303a30303a3030, 3, NULL),

(0x3832303930343036, 4, 'RAWTHORPE.txt');

Sergey Kornilov admin 4/3/2010

Hammond,
simply set 'Edit as' type of BLOB field to 'File/Image'.

M
mrphp author 4/5/2010



Hammond,
simply set 'Edit as' type of BLOB field to 'File/Image'.


I've done this but it doesn't seem to work. The 'Edit As' window asks for a 'Upload Folder' which I leave blank. The file doesn't upload to the database, it uploads to the directory in which the phpRunner web files are located. Is phpRunner supposed to determine whether It uploads the file to the database or to a folder based on the field definition (ie. blob or text)? The field I'm using was originally a Text field which I have changed to a Blob. Could that be the poblem?
The previous post by swanside indicated I need 3 fields to make this work.
Thanks for your help.

S
swanside 4/5/2010

Did you try it the way I said?

Do you want me to send you a 1 table three line project for you to try out?

K
Khris 4/5/2010

Hammond, I've actually setup something very similar to this already which uploads copies of receipts and operation manuals into a directory based upon the username signed in.
Personally I wouldn't recommend storing files directly in the database as it can cause performance issues down the line.
If you'd like, I can setup a username on my database for you to log in and have a look. If it's something you're interested in, I'd be willing to share the code and perhaps it could be improved together.
PM me with your thoughts.

Sergey Kornilov admin 4/5/2010

If you change field type from Text to BLOB you to synchronize your database. Proceed to 'Datasource tables' screen, right click on tables list and choose 'Synchronize'.