Solved

store image ina database via html form

Posted on 2004-09-05
6
270 Views
Last Modified: 2013-12-12
Im going to store image in a oracle database. The image is required to upload via a HTML form. I create a table with three collum which are ID (number), name(varchar2) and body (blob). when im submit my imahe via form it gives three errors as follows.

1.Warning: ociparse(): OCIParse: ORA-00972: identifier is too long in /usr/local/apache/htdocs/image_store.php on line 19

2.Warning: ociexecute(): supplied argument is not a valid OCI8-Statement resource in /usr/local/apache/htdocs/image_store.php on line 30

3.Warning: Cannot modify header information - headers already sent by (output started at /usr/local/apache/htdocs/image_store.php:19) in /usr/local/apache/htdocs/image_store.php on line 40

Already Im strugling 1 1/2 day with these errors and 2 weeks to develop that image upload part. But still i couldnt make it sucsess. please help!!!! following is my script

<?php

$c=OCILogon("scott", "tiger");
  if ( ! $c ) {
    echo "Unable to connect: " . var_dump( OCIError() );
    die();
  }
$name = $_REQUEST["name"];
$image = $_REQUEST["image"];
$ffile = fopen($_FILES['image']['tmp_name'], "rb");
$content = fread($ffile,$_FILES['image']['size']);
$eimage = addslashes($content);

// data to insert into the project table

$qu = "insert into test_images (ID, NAME, BODY) values ('2','$name','$eimage')";
$sql = OCIParse($c,$qu);

// verify that SQL is valid
      $arrError = OCIError($sql);
      if ($arrError['code'])
      {
        print $arrError['message'];
        OCIRollback($c);
        exit;
      }
// execute the query
OCIExecute($sql,OCI_DEFAULT);
// make sure there were no errors with our query
      $arrError = OCIError($sql);
      if ($arrError['code'])
      {
        print $arrError['message'];
        OCIRollback($c);
        exit;
      }
      OCICommit($c);
header("Location: thank_you.htm");
?>



0
Comment
Question by:hkdarshana
  • 3
6 Comments
 
LVL 32

Expert Comment

by:ldbkutty
ID: 11988993
I dont have a direct answer for you. The comments in PHP Manual should be of help to you.

>> 1.Warning: ociparse(): OCIParse: ORA-00972: identifier is too long in /usr/local/apache/htdocs/image_store.php on line 19
http://uk.php.net/ociparse

>> 2.Warning: ociexecute(): supplied argument is not a valid OCI8-Statement resource in /usr/local/apache/htdocs/image_store.php on line 30
http://uk.php.net/manual/en/function.ociexecute.php

>> 3.Warning: Cannot modify header information - headers already sent by (output started at /usr/local/apache/htdocs/image_store.php:19) in /usr/local/apache/htdocs/image_store.php on line 40

This error is clear. You *cannot* have header() function after any information sent to the browser. (i.e.  * NO echo or print statements or HTML tags* should be present before the header() function. )

You should have even a single white space before the <?php.

Example:
----------

1. No whitespace before the "<?php" (remove double-quotes) tag. I mean

<?php  --->RIGHT.

whereas

 <?php  -->WRONG. (see the white space before <?php )

2. There should not be any HTML tags or echo statements before the header. But, You can have in the die() statement, because die() terminates the application.
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 11988997
>> You should have even a single white space before the <?php.

Actually, i mean:

You should *not* have even a single white space before the <?php.
0
 

Author Comment

by:hkdarshana
ID: 11995045
nyway if fixed above errors. Now i can store image in a oracle table via  "insertimage.php"  page. When Im query the db it shows ID and NAme fields. I cant query for BLOB data in sqlplus. So that there is no such a way to validate the image upload.

My table is

Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID                                                 VARCHAR2(5)
 NAME                                               VARCHAR2(30)
 BODY                                               BLOB
 
Im try to view image using view.htm and getimage.php files. There are no any error massages. But it doesnt show anything. my pages as follows.

insertimage.php
.....................................................................................................................................
<?php
session_start();
if($_POST['submit']){

$c=OCILogon("scott", "tiger");
  if ( ! $c ) {
    echo "Unable to connect: " . var_dump( OCIError() );
    die();
  }
$image = '$_POST["image"]';
$ffile = fopen($_FILES['image']['tmp_name'], "rb");
$content = fread($ffile,$_FILES['image']['size']);
$eimage = addslashes($content);


// data to insert into the project table
$qu = "insert into test_images (ID, NAME, BODY) values('3','".$_POST['name']."','$eimages')";
$sql = OCIParse($c,$qu);
// verify that SQL is valid
      $arrError = OCIError($sql);
      if ($arrError['code'])
      {
        print $arrError['message'];
        OCIRollback($c);
        exit;
      }
// execute the query
OCIExecute($sql,OCI_DEFAULT);
// make sure there were no errors with our query
      $arrError = OCIError($sql);
      if ($arrError['code'])
      {
        print $arrError['message'];
        OCIRollback($c);
        exit;
      }
      OCICommit($c);
header("Location: thank_you.php");
} else{
?>

<html>
<head><title>Store binary data into SQL Database</title></head>
<body>


    <form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data">
    File Description:<br>
    <input type="text" name="name"  size="40">
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
    <br>File to upload/store in database:<br>
    <input type="file" name="image"  size="40">
    <p><input type="submit" name="submit" value="submit">
    </form>

<?php

}

?>

</body>
</html>

view.htm
.......................................................................................................................................
<html>
<head><title>Image</title></head>
<body>
<img src ="getimage.php?NAME=dinushi">
</body>
</html>

getimage.php
....................................................................................................................................
<?php
      header("Content-type: image/gif");

         // now select back our data
         $c=OCILogon("scott", "tiger");
         $qu ="select body from test_images";
         $sql = OCIParse($c, $qu);
         @OCIExecute($sql, OCI_DEFAULT);
         // check our query
         $arrError = OCIError($sql);
         if ($arrError['code'])
       {
           print $arrError['message'];
           exit;
         }

      //      $nrows = ocirowcount($sql);

      //      if($nrows == 1)
//      {


            OCIFetchInto($sql, $row, OCI_B_BLOB);
      
            echo $row['BODY'];
            
//      }
.................................................................................................................................................................

Please help me to fix this. Im already spend more than 2 weeks for this bit. Any help I really appreciate.

0
 
LVL 32

Accepted Solution

by:
ldbkutty earned 500 total points
ID: 12001498
I am sorry, i dont have any experience in using sqlplus with PHP. But, storing image directly and retrieveing it is unnecessary and it will make your script harder.

My suggestion is upload the image in some folder and store the location of the image file in the database. So, if you want to display the image, you just need to retrieve the file path from the database.

I hope you understand. If not, let me know and i will try my best.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to send multiple emails at the same time in PHP 12 58
Dynamic varibles 5 32
mysqli_query() expects parameter 1 to be msqli, string given in 4 31
PHP Syntax Error 4 27
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question