By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,687 Members | 2,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,687 IT Pros & Developers. It's quick & easy.

Incorporating images with Oracle to create a PHP web site.

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
"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.