import img...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Georg Andersson

    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_DRAWI NG.jpg";
    $type="image/pjpeg";
    $file = fopen("C:/Temp/51_DRAWING_2.jp g", "r");
    $filesize = filesize("C:/Temp/51_DRAWING_2.jp g");

    $sql="INSERT INTO images(ID_IMAGE ,ID_GCP,ID_LEVE L_IMG,IMAGE_BIN ,IMAGE_NAME,IMA GE_SIZE,IMAGE_T YPE)";
    $sql .=" VALUES('503','5 1','4','$img',' $name','$filesi ze','$type')";

    $result=mssql_q uery($sql);

    ?>

    </HTML>
  • Alvaro G Vicario

    #2
    Re: import img...

    *** Georg Andersson wrote/escribió (3 Jun 2004 00:30:13 -0700):[color=blue]
    > $name="51_DRAWI NG.jpg";
    > $type="image/pjpeg";
    > $file = fopen("C:/Temp/51_DRAWING_2.jp g", "r");
    > $filesize = filesize("C:/Temp/51_DRAWING_2.jp g");
    >
    > $sql="INSERT INTO images(ID_IMAGE ,ID_GCP,ID_LEVE L_IMG,IMAGE_BIN ,IMAGE_NAME,IMA GE_SIZE,IMAGE_T YPE)";
    > $sql .=" VALUES('503','5 1','4','$img',' $name','$filesi ze','$type')";[/color]

    Where is $img supposed to come from?

    --
    --
    -- Álvaro G. Vicario - Burgos, Spain
    --

    Comment

    • Pedro Graca

      #3
      Re: import img...

      Georg Andersson wrote:[color=blue]
      > 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?[/color]
      ....[color=blue]
      > $sql="INSERT INTO images(ID_IMAGE ,ID_GCP,ID_LEVE L_IMG,IMAGE_BIN ,IMAGE_NAME,IMA GE_SIZE,IMAGE_T YPE)";
      > $sql .=" VALUES('503','5 1','4','$img',' $name','$filesi ze','$type')";
      >
      > $result=mssql_q uery($sql);[/color]

      No error-checking here?
      Are you sure your $sql is correct?

      You may need to use mysql_escape_st ring() on $img
      [color=blue]
      > ?>
      >
      ></HTML>[/color]


      --
      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 :

      Comment

      • Georg Andersson

        #4
        Re: import img...

        Hi Alvaro
        [color=blue]
        >
        > Where is $img supposed to come from?
        >
        > --[/color]

        it should actualy be $file instead of $img. sorry, i've forgot to change that...

        Comment

        • Alvaro G Vicario

          #5
          Re: import img...

          *** Georg Andersson wrote/escribió (3 Jun 2004 06:45:29 -0700):[color=blue]
          > it should actualy be $file instead of $img. sorry, i've forgot to change that...[/color]

          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
          --

          Comment

          • David Mackenzie

            #6
            Re: import img...

            On 3 Jun 2004 00:30:13 -0700, georg.andersson @swisstopo.ch (Georg
            Andersson) wrote:
            [color=blue]
            > $name="51_DRAWI NG.jpg";
            > $type="image/pjpeg";
            > $file = fopen("C:/Temp/51_DRAWING_2.jp g", "r");
            > $filesize = filesize("C:/Temp/51_DRAWING_2.jp g");
            >
            > $sql="INSERT INTO images(ID_IMAGE ,ID_GCP,ID_LEVE L_IMG,IMAGE_BIN ,IMAGE_NAME,IMA GE_SIZE,IMAGE_T YPE)";
            > $sql .=" VALUES('503','5 1','4','$img',' $name','$filesi ze','$type')";[/color]

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

            $sql="INSERT INTO
            images(ID_IMAGE ,ID_GCP,ID_LEVE L_IMG,IMAGE_BIN ,IMAGE_NAME,IMA GE_SIZE,IMAGE_T YPE)";
            $sql .=" VALUES('503','5 1','4','$file', '$name','$files ize','$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 )

            Comment

            • Georg Andersson

              #7
              Re: import img...

              Hi David
              [color=blue]
              > fopen() returns only a resource to the file. Use fread() to get its
              > contents.[/color]

              i added fread() line to my code:

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

              $name="51_DRAWI NG.jpg";
              $type="image/pjpeg";
              $file = fopen("C:/Temp/51_DRAWING_2.jp g", "r");
              $filesize = filesize("C:/Temp/51_DRAWING_2.jp g");
              $fileread = addslashes(frea d($file, $filesize));

              $sql="INSERT INTO images(ID_IMAGE ,ID_GCP,ID_LEVE L_IMG,IMAGE_BIN ,IMAGE_NAME,IMA GE_SIZE,IMAGE_T YPE)";
              $sql .=" VALUES('503','5 1','4','$filere ad','$name','$f ilesize','$type ')";
              $result=mssql_q uery($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?

              Comment

              • Alvaro G Vicario

                #8
                Re: import img...

                *** Georg Andersson wrote/escribió (4 Jun 2004 01:02:01 -0700):[color=blue]
                > 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?[/color]

                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
                --

                Comment

                • David Mackenzie

                  #9
                  Re: import img...

                  On 4 Jun 2004 01:02:01 -0700, georg.andersson @swisstopo.ch (Georg
                  Andersson) wrote:
                  [color=blue]
                  >Hi David
                  >[color=green]
                  >> fopen() returns only a resource to the file. Use fread() to get its
                  >> contents.[/color]
                  >
                  >i added fread() line to my code:
                  >
                  ><?php
                  > $db = mssql_connect(" xxxx","xxx","xx x") or die("Can't connect to
                  >mssql server.");
                  > mssql_select_db ("xxx", $db) or die("Can't select database.");
                  >
                  > $name="51_DRAWI NG.jpg";
                  > $type="image/pjpeg";
                  > $file = fopen("C:/Temp/51_DRAWING_2.jp g", "r");
                  > $filesize = filesize("C:/Temp/51_DRAWING_2.jp g");
                  > $fileread = addslashes(frea d($file, $filesize));
                  >
                  > $sql="INSERT INTO images(ID_IMAGE ,ID_GCP,ID_LEVE L_IMG,IMAGE_BIN ,IMAGE_NAME,IMA GE_SIZE,IMAGE_T YPE)";
                  > $sql .=" VALUES('503','5 1','4','$filere ad','$name','$f ilesize','$type ')";
                  > $result=mssql_q uery($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?[/color]

                  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 )

                  Comment

                  Working...