470,815 Members | 1,343 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

Incorporating images with Oracle to create a PHP web site.

I'm trying to write a web site for storing staff ID's together with
their photos using a PHP form. A user of the site should be able to
enter his ID, name, and select a file from a directory as his personal
photo.

Step 1. Created the table in SQL.
SQL> create table file_test (
StaffID number(5) NOT NULL,
StaffName VARCHAR2(30),
Staff_pic bFile);

Step 2. Register a directory alias with Oracle.
SQL> CREATE DIRECTORY Staff_Images_Dir AS 'C:\Staff_Images';

Step 3. Insert some BFile names using PHP.
<?php
// etc.

// Build an INSERT for the BFILE names
$sql = "INSERT INTO file_test ( StaffID, StaffName, Staff_pic )
VALUES ( '12345', 'Scott Tiger',
BFILENAME('Staff_Images_dir','12345.jpg') )";

$stmt = oci_parse($conn, $sql);

// Open the directory
$dir = 'C:\Staff_Images';
$dh = opendir($dir)
or die("Unable to open $dir");

// Loop through the contents of the directory
while (false !== ( $entry = readdir($dh) ) ) {

// Match only files with the extension .jpg, .gif or .png
if ( is_file($dir.'/'.$entry) &&
preg_match('/\.(jpg|gif|png)$/',$entry) ) {

// Bind the filename of the statement
oci_bind_by_name($stmt, ":filename", $entry);

// Execute the statement
if ( oci_execute($stmt) ) {
print "$entry added\n";
}
}

}
?>

However, the above PHP code did not work, so I wrote the following
lines straight into SQL and I got 1 row created.

SQL> insert into file_test (StaffID, staff_name, staff_pic)
values ('12345', 'Scott Tiger', BFILENAME('Staff_images_dir',
'12345.jpg'));

My questions.
1. How do I create a PHP code to let the user enter his details (ID and
name) in a form and select a file image (as his personal photo) from a
particular directory and save everything in the Database.
2. How do I go back then (as an admin) and view staff details and
pictures based on a SELECTed staff ID?

Any help will be highly appreciated.

May 7 '06 #1
2 1908
On Sat, 06 May 2006 23:01:53 -0700, K. A. wrote:
However, the above PHP code did not work,
What was the problem? Did you get any errors? I'll probably be able
to help you out, once you give me all the necessary information.
so I wrote the following
lines straight into SQL and I got 1 row created.

SQL> insert into file_test (StaffID, staff_name, staff_pic)
values ('12345', 'Scott Tiger', BFILENAME('Staff_images_dir',
'12345.jpg'));
Did that work?

My questions.
1. How do I create a PHP code to let the user enter his details (ID and
name) in a form and select a file image (as his personal photo) from a
particular directory and save everything in the Database.
2. How do I go back then (as an admin) and view staff details and
pictures based on a SELECTed staff ID?


Here is a little ADOdb snippet that I wrote to solve precisely the
same problem. Uploaded the file to a CLOB field, which makes it possible
for me to have web server and Oracle instance on two different machines.

<html>
<head>
<title>Upload File (ADOdb)</title>
</head>

<body>
<?php
require_once('adodb/adodb.inc.php');
require_once('adodb/adodb-exceptions.inc.php');
session_start();
$DSN=$_SESSION['DSN'];
$db=NewADOConnection("oci8");

$INS="insert into poetry(file_desc,file_data)
values (:pdesc,:cont)";
$clob=file_get_contents($_FILES['file']['tmp_name']);

try {
$db->Connect($DSN['database'],
$DSN['username'],
$DSN['password']);
$db->BeginTrans();
$sth=$db->PrepareSP($INS);
$db->InParameter($sth,$_POST['desc'],'pdesc');
$db->InParameter($sth,$clob,'cont',-1,OCI_B_CLOB);
$db->Execute($sth);
}
catch (Exception $e) {
$db->RollbackTrans();
die($e->getTraceAsString());
}
$db->CommitTrans();
?>

<center>
<h2>File <?=$_FILES['file']['name']?>
uploaded successfully!</h2>
</center>
</body>
</html>

I wholeheartedly advise you to use ADOdb as it is logical, reliable,
easy to use, covers different drivers and works with PHP5, which gives
you the ability to utilize exceptions.

--
http://www.mgogala.com

May 8 '06 #2
"K. A." <ka****@hotmail.com> writes:
I'm trying to write a web site for storing staff ID's together with
their photos using a PHP form. A user of the site should be able to
enter his ID, name, and select a file from a directory as his personal
photo.


Harry Fuecks has an article on using LOBS and BFILE with Oracle at:
http://www.oracle.com/technology/pub...ecks_lobs.html

--
Christopher Jones, Oracle Corporation, Australia.
Email: Ch***************@oracle.com
Blog: http://blogs.oracle.com/opal/
May 10 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by susmita_ganguly | last post: by
4 posts views Thread by Amardeep Verma | last post: by
reply views Thread by Jim | last post: by
61 posts views Thread by phil-news-nospam | last post: by
12 posts views Thread by John Kotuby | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.