Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

store image ina database via html form

Posted on 2004-09-05
6
Medium Priority
?
294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

715 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