I'm trying to allow users to upload an avatar, that will be displayed on the site.
I have a column in a database that is set to binary(1) and I would like to store an image in it, that will be uploaded from a "file field" form input.
Then later retrieved and displayed on a web page.
How can I set the content-type to image, for only that item, without changing the whole document. I need "Content-type: text/html\n\n"; for the rest of the page.
I apologize if this is a vague, or "stupid" question, but I honestly have no idea of how to go about it.
Should I create a variable that contains the image file? Or pipe the image buffer to the <img src="$image"> tag?
Or ... Hummmm??
I have no idea!!
please help.
I'm not necessarily expecting the "actual" code that would do this, but can someone explain (theoretically) how this should/could be done, with perl + Template Toolkit?
6 3614
I would store the image name in the db. Have the file uploaded to the an avatar directory. Then in your template file have an image tag that is populated with the image name from the db for the said user.
--Kevin
I would store the image name in the db. Have the file uploaded to the an avatar directory. Then in your template file have an image tag that is populated with the image name from the db for the said user.
--Kevin
Yes, after much goggling and your response, I believe that MUST be the best solution.
Thanks!!
(Although I have read that mysql 5.0 + can facilitate this. I have yet to find out how!)
Set the type to 'BLOB' or 'MEDIUMBLOB' also make sure that you escape the data before the image into the DB. - Character Length Allowed:
-
BLOB 0 - 65535 about 6meg
-
MEDIUMBLOB 0 - 16777215 about 16meg
--Kevin
Set the type to 'BLOB' or 'MEDIUMBLOB' also make sure that you escape the data before the image into the DB. - Character Length Allowed:
-
BLOB 0 - 65535 about 6meg
-
MEDIUMBLOB 0 - 16777215 about 16meg
--Kevin
Then what? -
use CGI;
-
use DBI;
-
my $dbh = DBI->connect("dbi:mysql:databse:localhost", $handle, $password);
-
my $q = new CGI;
-
-
-
my $file = $q->upload('avatar');
-
-
my $sth = $dbh-prepare(qq~ UPDATE users SET avatar=? WHERE id =? ~);
-
-
$sth->execute($file, 10);
-
-
?????
I am not certain if you can upload the file directly to the database. It is my understanding that you upload the file to the server. Then use read or sysread to read the image file data. Then store the data to store in the database. Below is a script to help illustrate how to do it. If you have MySQL Cookbook, chapter 17 covers this and has a sample script.
This code is untested. You should add the code to do a sanity check on the image file which this script does not do.
I hope this helps. - #!/usr/bin/perl -T
-
-
use strict;
-
use warnings;
-
-
use CGI;
-
use CGI::Carp qw/fatalsToBrowser/;
-
use File::Basename;
-
use DBI;
-
-
my $q = CGI->new;
-
-
print $q->header();
-
print $q->start_html();
-
-
my %config_vars = ( DBServerName => 'xxx',
-
DBName => 'xxx',
-
DBUserName => 'xxx',
-
DBPassword => 'xxx',
-
);
-
-
my ($dbh, $data_source);
-
my $dataSource = 'DBI:mysql:' . $config_vars{DBName} . ':' . $config_vars{DBServerName};
-
my $dbh = DBI->connect( $dataSource, $config_vars{DBUserName}, $config_vars{DBPassword}, {RaiseError=>1});
-
-
my $image_dir = 'some_dir/images/';
-
my $source_file = $q->upload('image_file');
-
-
-
my ($image_name, undef ,$mime_type) = fileparse($soruce_file,qr{\..*});
-
my $image_file = $image_dir . $image_name . $mine_type;
-
my ($image_data, $size);
-
-
-
open (my $UPLOAD, '>', $image_file) ||die "Can't open file $!:\n";
-
binmode $UPLOAD;
-
$size = (stat ($UPLOAD))[7];
-
while (<$UPLOAD>) {
-
print $UPLOAD;
-
}
-
close ($UPLOAD);
-
-
-
open (my $READ_FILE, '<', $image_file) || die "Can't open file $!:\n";
-
binmode($READ_FILE);
-
read($READ_FILE, $image_data, $size) == $size || die "Failed to write entire image file $image_file: $!\n";
-
close ($READ_FILE);
-
-
-
my $insert = $dbh->prepare(q{INSERT INTO image(image_name, mime_type, image_data) VALUES(?,?,?)});
-
$insert->execute($image_name, $mime_type, $image_data);
-
$insert->finish();
-
}
-
-
unlink $image_file || die "Can't unlink $image_file $!:\n";
-
-
-
print $q->end_html();
-
-
1;
--Kevin
Thank You!!
I will have to pick over this, but I am definitely excited.
That is another question I had was to see how large the file is before uploading.
I am limiting to 39kb. I tried loading one larger and it just hung...
I need a way to test, and create error accordingly.
the: -
$size = (stat ($UPLOAD))[7];
-
Looks good.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Mark |
last post by:
I have a website with an increasing amount of articles and news reports and
so I am thinking of moving away from storing each article as a seperate page
to having a single page and storing articles...
|
by: Tom |
last post by:
Hi,
I'm designing a website that will use XML to store all the content of my
web pages. I will use a PHP script to parse the XML. Should I store the
XML in a MySQL database or should I simply...
|
by: dave |
last post by:
Hello there,
I am at my wit's end ! I have used the following script succesfully to
upload an image to my web space. But what I really want to be able to do is
to update an existing record in a...
|
by: mrT |
last post by:
Hi,
My latest web project includes loads of user-customisable features,
lots of which are on/off values. Currently I'm storing each one of the
15 or so properties in tinyint fields, as 1s or 0s....
|
by: Benoit St-Jean |
last post by:
I am looking at options/ways to store 12 million gif/jpg images in a
database. Either we store a link to the file or we store the image
itself in the database. Images will range from 4k to 35k in...
|
by: bissatch |
last post by:
Hi,
I am currently writing a news admin system. I would like to add the
ability to add images to each article.
What I have always done in the past is uploaded (using a form) the
image to a...
|
by: blackg |
last post by:
Here is the whole story. I have a table in my db call Products.
columns are: ProductID, Product, ProductImage. Now in the
Product.aspx page I want the visitor to be able to upload the Image
of...
|
by: netcoder77 |
last post by:
Has anyone tried this in VB .NET or via VBScript? Can it be done? How
do we handle retrieving a binary data format (the photo) using ADSI or
VB .NET? All my searching on the net yielded no useful...
|
by: steelghost |
last post by:
I'm currently trying to store images into my DB. But the collation used
on my DB is big5, since my client wants this default encoding on their
webpage.
So to be able read the retreived data I have...
|
by: shivapadma |
last post by:
i have inserted the image into database using the following code
String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/";
String dbName =...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
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: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
|
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,...
| |