Hi, i have used this code to save my image directly from the web URL to mysql: - $url=http://www.example.com/img.jpg
-
$image = addslashes(fread(fopen($url","rb"),$filesize));
the $image is inserted into mysql under long blob data type. However, when the size of the uploaded image is viewed using mysql, it show a lesser file size than specified. And during retriving, the picture is not complete and is pixilated which i believe is due to the incomplete file size that is saved in mysql, unless I have error in my retriving code. - $row = mysql_fetch_assoc($rs);
-
$imagebytes = $row[imgdata];
-
header("Content-type: image/jpeg");
-
echo $imagebytes;
It is unable to work for both big and small images
Any help is appreciated, but I am unable to try it until weekend. Thanks
There is no need to encode the URL using the urlencode function unless you plan to print the URL itself. If you only use it within your PHP code, it makes no difference.
As to reading the file, this would be enough: - $file=file_get_contents("http://example.com/img.jpg");
The rest of the parameters of the function are optional. By default it reads the entire image as binary data, so there is nothing more you need to set manually.
If you plan on putting the file data into a MySQL database, the data needs to be escaped to avoid it corrupting the query. That is why we run it through mysql_real_esceape_string first, to make it safe to use. - $file = mysql_real_escape_string($file);
-
mysql_query("INSERT INTO `tbl`(`file`) VALUE('$file')");
An alternative to using mysql_real_escape_query is to use prepared (parametrized) statements. That way you let the MySQL driver itself deal with escaping the data. - $mysqli = new mysqli('host', 'user', 'pwd', 'dbName');
-
if ($stmt = $mysqli->prepare("INSERT INTO `tbl`(`file`) VALUES(?)"))
-
{
-
$stmt->bind_param("b", $file);
-
$stmt->execute();
-
}
7 5281
anyway, using phpmyadmin, i was able to browse to the url and save the pic into the database, and displayed it nicely using the same display codes.
help anyone?
Atli 5,058
Expert 4TB
Hey.
Check out my article on how to upload files into MySQL. Phase #2 is where the file is uploaded into the database. You would just have to edit it to replace the source of the data; instead of using the $_FILES array to fetch it from a form, fetch the file using file_get_contents and determine the require info using filesize and getimagesize.
thanks for the reply, Atli.
I understand one of the reason for using file_get_content is due to the fact that it is faster.
I notice under this method's api, i saw this urlencode() and from your article u put real_escape_string before inserting into the database.
To make things mroe understandable, I am using the image search api, so from the result I get from the search, I allow the user to select those images that they want to save into the database. Using the api, I can also retrieve the imagesize and the filesize.
So is it ok for me to do this? :
$file=file_get_contents("http://urlofpic.com/img.jpg",false,0, $filesizeFromAPI);
then insert $file into the database blob column
So do I need to encode using urlencode(), as I will not know what are the urls of the images since it it generated from the search result using the api
Atli 5,058
Expert 4TB
There is no need to encode the URL using the urlencode function unless you plan to print the URL itself. If you only use it within your PHP code, it makes no difference.
As to reading the file, this would be enough: - $file=file_get_contents("http://example.com/img.jpg");
The rest of the parameters of the function are optional. By default it reads the entire image as binary data, so there is nothing more you need to set manually.
If you plan on putting the file data into a MySQL database, the data needs to be escaped to avoid it corrupting the query. That is why we run it through mysql_real_esceape_string first, to make it safe to use. - $file = mysql_real_escape_string($file);
-
mysql_query("INSERT INTO `tbl`(`file`) VALUE('$file')");
An alternative to using mysql_real_escape_query is to use prepared (parametrized) statements. That way you let the MySQL driver itself deal with escaping the data. - $mysqli = new mysqli('host', 'user', 'pwd', 'dbName');
-
if ($stmt = $mysqli->prepare("INSERT INTO `tbl`(`file`) VALUES(?)"))
-
{
-
$stmt->bind_param("b", $file);
-
$stmt->execute();
-
}
thanks, it really worked.
I noticed that if I put in the url which forced downloads, I am unable to save those images into the database.
One example of the URL i encountered is : http://www.b6t6.com/article/upload/2...1081670731.png
Any way for me to marked these images down so that I will not save them into the database.
Or best if there is a work around for me to directly save into the database first using file_get_content
I used Fopen(), and the url is valid. So I'm thinking is it worth to check the header file? But I'm not sure what to look out for.
Thanks
Thanks anyway, the problem i mentioned is solved.
The reason it could not be inserted is due to the size, not that it is forced to be downloaded. All I did is just changed the max_allowed_packet and its done.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jan Nordgreen |
last post by:
The following code only generates the first csv file. The second
request is just ignored. What am I doing wrong? I am using Mozilla
Firefox, Windows XP, and Xampp.
<?php
require...
|
by: Dev |
last post by:
Dear Friends,
I am passing the image name, size (in bytes) and imgaeformat (like jpg or bmp or pdf) through the network.
I want display the image into picturebox without saving image...
|
by: Peder Y |
last post by:
My code is something like this:
---------------
Image img = Image.FromFile("somefile.bmp");
FileStream fStream = new FileStream("someBinaryFile.dat");
BinaryWriter bw = new...
|
by: |
last post by:
I cant seem to find the commmand that I can use to save an image file
from a url to my local harddrive. I am looking to save a jpg from a
webpage that I have screen scraped.
I have a web...
|
by: Tom |
last post by:
I have a Picturebox into which I load an image. Then I programaticaly add
some text to that image (using graphics.drawstring). I then want to save the
image out to disk -WITH- the text in the...
|
by: pdh12783 |
last post by:
HI All,
I am having a problem saving the image in when using this code. The Language i am using is VB.NET 2005
Dim dlgImage As New OpenFileDialog
dlgImage.Filter = "Image...
|
by: Sl1ver |
last post by:
i've got this piece of coding. i need help with opening a picture box and then saving it as OLE Object in access DB
How would i massage this code tho fit in?
try
{
//save image to byte...
|
by: WellCom34 |
last post by:
hey guys need a little help.
i tried to save image into my database, it saves without error
but when i tried to check if it saves in mysql, the data about my picture id save, but the image i save...
|
by: priyamtheone |
last post by:
Hi there,
I have a form containing a listbox showing a list of image names. It's bound
to the database table. When an image name is clicked it shows the image and
imagename in a picturebox and...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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: 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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |