Solved

store image ina database via html form

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
setup wamp server for first time 2 41
php call to a non-object 3 31
Session timeout 5 13
How can I echo a PHP variable in AJAX function? 7 27
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 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…

932 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

19 Experts available now in Live!

Get 1:1 Help Now