Solved

store image ina database via html form

Posted on 2004-09-05
6
262 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now