473,396 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

import img...

hi

i'm pretty new to php. so let me ask you a pretty easy question:

the code below is supposed to import an image to a mssql database.
images are not meant to be stored in databases, i know. anyway, i'm
wondering why my code doesn't work.

can anybody give me some help?

regards
<HTML>
<HEAD>
<TITLE>php test</TITLE>
</HEAD>

<?php
$db = mssql_connect("xxx","xxx","xxx") or die("Can't connect to
mssql server.");
mssql_select_db("xxx", $db) or die("Can't select database.");

$name="51_DRAWING.jpg";
$type="image/pjpeg";
$file = fopen("C:/Temp/51_DRAWING_2.jpg", "r");
$filesize = filesize("C:/Temp/51_DRAWING_2.jpg");

$sql="INSERT INTO images(ID_IMAGE,ID_GCP,ID_LEVEL_IMG,IMAGE_BIN,IMAG E_NAME,IMAGE_SIZE,IMAGE_TYPE)";
$sql .=" VALUES('503','51','4','$img','$name','$filesize',' $type')";

$result=mssql_query($sql);

?>

</HTML>
Jul 17 '05 #1
8 2305
*** Georg Andersson wrote/escribió (3 Jun 2004 00:30:13 -0700):
$name="51_DRAWING.jpg";
$type="image/pjpeg";
$file = fopen("C:/Temp/51_DRAWING_2.jpg", "r");
$filesize = filesize("C:/Temp/51_DRAWING_2.jpg");

$sql="INSERT INTO images(ID_IMAGE,ID_GCP,ID_LEVEL_IMG,IMAGE_BIN,IMAG E_NAME,IMAGE_SIZE,IMAGE_TYPE)";
$sql .=" VALUES('503','51','4','$img','$name','$filesize',' $type')";


Where is $img supposed to come from?

--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #2
Georg Andersson wrote:
the code below is supposed to import an image to a mssql database.
images are not meant to be stored in databases, i know. anyway, i'm
wondering why my code doesn't work.

can anybody give me some help? .... $sql="INSERT INTO images(ID_IMAGE,ID_GCP,ID_LEVEL_IMG,IMAGE_BIN,IMAG E_NAME,IMAGE_SIZE,IMAGE_TYPE)";
$sql .=" VALUES('503','51','4','$img','$name','$filesize',' $type')";

$result=mssql_query($sql);
No error-checking here?
Are you sure your $sql is correct?

You may need to use mysql_escape_string() on $img
?>

</HTML>

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #3
Hi Alvaro

Where is $img supposed to come from?

--


it should actualy be $file instead of $img. sorry, i've forgot to change that...
Jul 17 '05 #4
*** Georg Andersson wrote/escribió (3 Jun 2004 06:45:29 -0700):
it should actualy be $file instead of $img. sorry, i've forgot to change that...


Right, then $file is a file handler returned by fopen(). I've never
inserted files in databases but I'm pretty sure you have to provide the
file content itself, not a file handler used by a scripting language not
related to the database. Once you open the file you have to *read* it.
Check PHP manual for fread(), fgets(), etc.

--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #5
On 3 Jun 2004 00:30:13 -0700, ge*************@swisstopo.ch (Georg
Andersson) wrote:
$name="51_DRAWING.jpg";
$type="image/pjpeg";
$file = fopen("C:/Temp/51_DRAWING_2.jpg", "r");
$filesize = filesize("C:/Temp/51_DRAWING_2.jpg");

$sql="INSERT INTO images(ID_IMAGE,ID_GCP,ID_LEVEL_IMG,IMAGE_BIN,IMAG E_NAME,IMAGE_SIZE,IMAGE_TYPE)";
$sql .=" VALUES('503','51','4','$img','$name','$filesize',' $type')";


From your previous post, replacing $img with $file :

$sql="INSERT INTO
images(ID_IMAGE,ID_GCP,ID_LEVEL_IMG,IMAGE_BIN,IMAG E_NAME,IMAGE_SIZE,IMAGE_TYPE)";
$sql .=" VALUES('503','51','4','$file','$name','$filesize', '$type')";

fopen() returns only a resource to the file. Use fread() to get its
contents.

Also, a quick Google search indicates that some people have had
difficulty using PHP with BLOB fields in MS SQL. If you have the same
trouble, you may have to start storing your images in the filesystem.

--
David ( @priz.co.uk )
Jul 17 '05 #6
Hi David
fopen() returns only a resource to the file. Use fread() to get its
contents.


i added fread() line to my code:

<?php
$db = mssql_connect("xxxx","xxx","xxx") or die("Can't connect to
mssql server.");
mssql_select_db("xxx", $db) or die("Can't select database.");

$name="51_DRAWING.jpg";
$type="image/pjpeg";
$file = fopen("C:/Temp/51_DRAWING_2.jpg", "r");
$filesize = filesize("C:/Temp/51_DRAWING_2.jpg");
$fileread = addslashes(fread($file, $filesize));

$sql="INSERT INTO images(ID_IMAGE,ID_GCP,ID_LEVEL_IMG,IMAGE_BIN,IMAG E_NAME,IMAGE_SIZE,IMAGE_TYPE)";
$sql .=" VALUES('503','51','4','$fileread','$name','$filesi ze','$type')";
$result=mssql_query($sql);
?>
i'm still getting a PHP Warning "Incorrect syntax near '\'". So, there
is no "\" in my sql-statement. i guess this means, the image is
treaten as a text and not as an image. how can this be avoided?
Jul 17 '05 #7
*** Georg Andersson wrote/escribió (4 Jun 2004 01:02:01 -0700):
i'm still getting a PHP Warning "Incorrect syntax near '\'". So, there
is no "\" in my sql-statement. i guess this means, the image is
treaten as a text and not as an image. how can this be avoided?


If you are having problems inserting binary data into the database, you
might solve it using pure text. Check base64_encode() and base64_decode().
This makes stored info about 30% larger though.
--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #8
On 4 Jun 2004 01:02:01 -0700, ge*************@swisstopo.ch (Georg
Andersson) wrote:
Hi David
fopen() returns only a resource to the file. Use fread() to get its
contents.


i added fread() line to my code:

<?php
$db = mssql_connect("xxxx","xxx","xxx") or die("Can't connect to
mssql server.");
mssql_select_db("xxx", $db) or die("Can't select database.");

$name="51_DRAWING.jpg";
$type="image/pjpeg";
$file = fopen("C:/Temp/51_DRAWING_2.jpg", "r");
$filesize = filesize("C:/Temp/51_DRAWING_2.jpg");
$fileread = addslashes(fread($file, $filesize));

$sql="INSERT INTO images(ID_IMAGE,ID_GCP,ID_LEVEL_IMG,IMAGE_BIN,IMAG E_NAME,IMAGE_SIZE,IMAGE_TYPE)";
$sql .=" VALUES('503','51','4','$fileread','$name','$filesi ze','$type')";
$result=mssql_query($sql);
?>
i'm still getting a PHP Warning "Incorrect syntax near '\'". So, there
is no "\" in my sql-statement. i guess this means, the image is
treaten as a text and not as an image. how can this be avoided?


SQL Server does not use \ as an escape character. You escape single
quoues by doubling them up (a ' becomes '' ). Not sure about
unprintable characters.

I used BLOB fields with SQL Server 7. I had to use special ADO
functions to INSERT and retrieve the data in chunks. I didn't use a
normal INSERT or SELECT to get it.

As suggested by Alvaro, you may have to encode it as text somehow.

--
David ( @priz.co.uk )
Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stian Søiland | last post by:
all examples performed with: Python 2.3+ (#2, Aug 10 2003, 11:09:33) on linux2 (2, 3, 0, 'final', 1) This is a recursive import:
0
by: Vio | last post by:
Hi, I've been trying to embed (statically) wxPy alongside an embedded py interpreter on a linux/gtk box. At one point, for some reason misc.o linking reported "multiple definitions of...
0
by: John Roth | last post by:
I've found a case where it seems that Python is importing two copies of a module without any reason or indication. It took me a while to verify that this is what is occuring: I had to write a...
5
by: Steve Holden | last post by:
This is even stranger: it makes it if I import the module a second time: import dbimp as dbimp import sys if __name__ == "__main__": dbimp.install() #k = sys.modules.keys() #k.sort() #for...
1
by: mark | last post by:
In Access 2000 and 2002, I have created an import specification to import the fixed-width recordset below into an existing table. I am having strange problems with the import of the date and time...
4
by: Bruce W. Roeser | last post by:
All, I'm reading a book by Charles Petzold (Programming VS.Net). Pretty good content but am confused about the difference. From the text: ...
2
by: Jon | last post by:
It appears that (windows) python searches in the current working directory before looking in the local site-packages directory, or that '.' comes first in sys.path? The problem arises when I made...
7
by: Ron Adam | last post by:
from __future__ import absolute_import Is there a way to check if this is working? I get the same results with or without it. Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win 32 ...
5
by: W. Watson | last post by:
Is there a single source that explains these statements? ------------------------------ from Tkinter import * from Numeric import * import Image import ImageChops import ImageTk import time...
9
by: rsoh.woodhouse | last post by:
Hi, I'm trying to work out some strange (to me) behaviour that I see when running a python script in two different ways (I've inherited some code that needs to be maintained and integrated with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
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...

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.