I am trying to insert image in database. The database has a field named images and its type is BLOB. I am trying to insert the image, but only the first 2.2KB is stored. Even if I insert another image it stores only 2.2KB in the database.
When I try to show this image in my application it doesn't show; it's just a small icon, not the image. How can I insert the image in the right way?
use CGI;
my $file = $q->param("file");
$file = 'C:/wamp/bin/apache/apache2.2.22/cgi-bin/images/2.jpg';
open(my $fh, $file);
my $data;
binmode($fh);
read($fh, $data, (stat($fh))[7]);
close($fh);
my $Data = {
table =>'student',
data => {
Image => $fh,
}
};
Data::Insert($Data);
print $q->header;
print $q->start_html(
-title => "student",
);
print $q->end_html;
showImage.pl
my $q = new CGI();
my $handle = Dbm::connection();
$id = $q->param('id_person');
$getimage = $handle->selectrow_array (<<SQLEOF);
SELECT Image
FROM student
WHERE ID = '$id'
SQLEOF
print "Content-Type: image/jpeg\n";
print "Content-length: \n\n";
binmode STDOUT;
print STDOUT $getimage;
My recomendation is keep the image as base 64 encrypted to the db with the MIME type of the image. When you need it, just decrypt it by saying MIME type. This is the mostly used to upload files using ajax. So why can't we use the same way to store image directly to DB?
Just give an additional column to keep MIME type in your table and take it along with the encoded data as print it together.
From a file extension, we can identify the type of file. The MIME type for images are mainly
image/gif: GIF image
image/jpeg: JPEG JFIF image;
image/pjpeg: JPEG JFIF image;
image/png: Portable Network Graphics;
image/svg+xml: SVG vector image;
image/tiff: Tag Image File Format (only for Baseline TIFF);
You can create a new column by giving name as mime_typ . Now when you enctrypt a file using base 64 encryption, keep it as a string like we store usernames and passwords in a table. Similarly add the MIME type to the mime_typ column. when you want to show the image, print the encrypted content after decoding it along with the content in the MIME type, which is stored in the same row in the mime_typ column. You can search google for the way to show an image which is encrypted in base 64 encryption.
You need to read the file in binary mode - i.e.
open(my $fh, $myfile);
my $data;
binmode($fh);
read($fh, $data, (stat($fh))[7]);
close($fh);
I'm not sure why you deleted the database-insert code from the question, but I found it in the revision history.
The issue could be because you aren't not using bind variables, and the binary image contains an escape character which is causing a problem.
I recommend using DBIx::Simple to help create your insert statements that will help create the bind variables for you. DBIx::Simple works with both SQL::Abstract and SQL::Interp. I find SQL::Interp more flexible.
It also appears to be a bug that you are inserting the file handle into the image field instead of the file data. Try adding use File::Slurp (which you may need to install), and then putting this in your %data hash:
Image => scalar read_file($file, { binmode => ':raw' });
Your SELECT statement is also vulnerable to a SQL injection attack because you did not validate the outside input before passing it to the database, and you did not you bind variables again. Using DBIx::Simple, the same code would look like this:
my $db = DBIx::Simple->new($handle);
$getimage = $db->iquery("SELECT Image FROM student WHERE ID = ",\$id)->list;
Also, I recommend omitting the Content-Length header, or properly calculating it, rather than leaving it present in an invalid state.
Related
My file is already save. I have file name and file url. for generate waveform i want this type of code.
[0.75827,0.502991,0.765717,0.68399,0.798004,.....]
i think this is json of audio file. how can i get this type of json of audio file
I think what you want is to create an array of numbers based on an audiofile to represent the contents of your audio file. You should probably just check the answer on this question which seem to work just fine for that:
https://stackoverflow.com/questions/2381243/how-does-soundcloud-com-generate-the-waveform-for-their-mp3-player
They mention this project does the job (to generate the waveform):
https://github.com/afreiday/php-waveform-png
If you specifically want the array of numbers, you can change the code to store or return the variable $v from this line:
$v = (int) ($data / 255 * $height);
From:
https://github.com/afreiday/php-waveform-png/blob/master/php-waveform-png.php
I'm trying to get & store in a text file all the all addresses from the "address" column in the next page:
http://bitcoinrichlist.com/top100
It should be very simple, but I never worked with something like this before, I always received data from mysql databases or php variables, but never from a html page.
Someone may explain me how should I do this?
Storing the addresses it's easy, fwrite command right to a text file on my server.
But what about receiving the data? How should I do it?
edit:
My question in short is: How should I save all the addresses from the "address" column in a text file?
You can use the PHP Simple HTML DOM Parser to pull in a web page and then traverse its DOM. Example from their quick start guide:
// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');
// Find all images
foreach($html->find('img') as $element)
echo $element->src . '<br>';
// Find all links
foreach($html->find('a') as $element)
echo $element->href . '<br>';
Once you gather up the elements, loop through them and write out to a file.
How can I get all meaning of a word in json response .Preferably without key/signing up/api key .If I type test then all meaning of that word.
I tried :
but its not working.
<?php
$query = 'oracle';
$file = file_get_contents('http://www.google.com/dictionary/json?callback=a&q='.$query.'&sl=en&tl=en&restrict=pr,de&client=te');
// var_dump($file);
$file = substr($file, 2, -10);
$file = preg_replace("/\\\x[0-9a-f]{2}/", "", $file);
echo $file;
$json = json_decode($file);
var_dump($json);
?>
Even this is returning null.
I have tried Only the php above.I would like to knowif I can make rest call without api key just words which match the query word .Is there any rest call you have in mind.I really appreciate any help .Thanks in Advance.
Assuming server side
Get a copy of a dictionary in a computer friendly format i.e. http://www.ibiblio.org/webster/ (XML).
Store said dictionary in a database or in memory and perform a lookup.
Would then be trivial to provide a restful service returning all definitions for a particular word.
Also see: Google's "define: " through an API?
I have a script that will download www pages, and I want to extract the text and store it in a uniform encoding (UTF8 would be fine). The downloading (UserAgent), Parsing (TreeBuilder) and text extraction seem fine, but I'm not sure I'm saving them correctly.
They dont view when opening the output file in for example notepad++; The original HTML views find in a text editor.
The HTML files typically have
charset=windows-1256 or
charset=UTF-8
So I figured if I could get the UTF8 one to work, then it was just an recoding problem. Here is some of what I have tried, assuming I have an HTML file saved to disk.
my $tree = HTML::TreeBuilder->new;
$tree->parse_file("$inhtml");
$tree->dump;
The output from dump captured for STDOUT views correctly in .txt file only after
Switching the encoding to utf8 in the text editor…
$formatter = HTML::FormatText->new(leftmargin => 0, rightmargin => 50);
if (utf8::is_utf8($formatter->format($tree))) {
print " Is UTF8\n";
}
else {
print " Not UTF8\n";
}
Result Shows this IS UTF8 when the content says it is, and Not UTF8 otherwise.
I have tired
opening an file with ">" and ">:utf8"
binmode(MYFILE, ":utf8");
encode("utf8", $string); (where string is the output of formatter->format(tree))
But nothing seems to work correctly.
Any experts out there know what Im missing?
Thanks in advance!
This example can help you to find what you need:
use strict;
use warnings;
use feature qw(say);
use HTML::TreeBuilder qw( );
use Object::Destroyer qw( );
open(my $fh_in, "<:encoding(cp1252)", $ARGV[0]) or die $!;
open(my $fh_out, ">:encoding(UTF-8)", $ARGV[1]) or die $!;
my $tree = Object::Destroyer->new(HTML::TreeBuilder->new(), 'delete');
$tree->parse_file($fh_in);
my $h1Element = $tree->look_down("_tag", "h1");
my $h1TrimmedText = $h1Element->as_trimmed_text();
say($fh_out $h1TrimmedText);
I really like the module utf8::all (unfortunately not in core).
Just use utf8::all and you have no worries about IO, when you work only with UTF-8 files.
I want to store text/string in a text field in a database.
This string has the variable $name in it.
When I pull it out of the database, I want that variable to be substituted with the value I define before I print the string.
# Variable I want to substitute #
1. $name='John';
# needs to be read from database #
2. $txt{'hello'}="Hello ${name}, How are you?";
3. print "<tag>$txt{'hello'}</tag>";
It prints Hello John, How are you? as required, but when 2nd line is read from database, it displays Hello ${name}, How are you?.
Some things I found are:
Locale::Maketext
$string =~ s/(\$\w+)/$1/eeg;
my $string = sprintf 'Say hello to %s and %s', $foo, $bar;
Can someone guide me about how to go about it?
What you're describing is a template. There are lots of template systems on CPAN of varying degrees of complexity. Text::Template and Template Toolkit are a couple of popular ones. You don't want to let your templates access arbitrary variables; that's a security hole. Instead, put the variables they're allowed to access into a hash.
If all you need is a very simple system, you can do something like this:
sub fill_in_template
{
my ($text, $values) = #_;
$text =~ s/ \$\{ ( [^}\s]+ ) \} /$values->{$1}/gx;
return $text;
}
my %txt;
my %values = (name => 'Your Name');
my $template = 'Hello ${name}, How are you?'; # $name NOT interpolated
$txt{'hello'} = fill_in_template($template, \%values);
print "<tag>$txt{'hello'}</tag>\n";
You might add some error checking in case the template uses a field that's not defined. But if you need something more complicated than that, you're probably better off picking an existing template system from CPAN.
Locale::Maketext is intended for internationalization (so your app can produce output in multiple languages without your translators needing to work on the code directly) and is not the sort of template engine you're looking for.