store image pathi want to store image path into the data
18 Replies - 11371 Views - Last Post: 24 January 2013 - 04:36 PM
#1
store image path
Posted 21 December 2005 - 11:16 AM
here i m again asking for your help!!!
i want to store only the path of image file into the database.
images will store in a folder & only the path will sore in the database.
i've tried it but failed. i couldn't sore or retrieve the image in that way!!
please can anyone help meeeeeeeeeeeeeeeeeeeeeeeeee.
Replies To: store image path
#2
Re: store image path
Posted 21 December 2005 - 11:42 AM
#3
Re: store image path
Posted 21 December 2005 - 03:52 PM
The image link is retrieved from a form
<?php $_POST[ 'imglink' ]; ?>
Obviously, this would need to be verified as a img. This best way to do this is to find the .gif, .jpg
<?php
session_start();
$img = $_POST[ 'imglink' ];
$filetypes = array( 'gif', 'jpg' );
++$filetypes;
$filetype = strstr( $img, '.' );
if( $filetype != $filetypes )
{ echo 'not supported'; }
session_register( 'Image' );
?>
Then it could be inputted into a database
<?php $DB = mysql_connect( 'localhost', 'root', 'password' ); mysql_select_db( 'ImageDB1' ); $Img = $_SESSION[ 'Image' ]; mysql_query( "INSERT into `IMAGES` ( `id`, `url` ) VALUES ( '', '$Img' )" ); session_destroy(); mysql_close( $DB ); ?>
The id in table would need to be at least small int, with auto increment and primary key.
The url would need to be a varchar of 50 characters.
Hope this helped.
#4
Re: store image path
Posted 22 December 2005 - 06:05 AM
My files are as follows:
add_project.php
<?php
include ('../include/newsession.php');
include('../include/db.php');
include('header.php');
?>
<script language=Javascript>
// Script for will reload the the page after select any client name to add the service info in the database
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='add_project.php?cat=' + val ;
}
// -->
</script>
<BODY><BR>
<?
session_start();
echo " <font class=text>Welcome $session[userid] !!!<br><br>";?>
<div align=center>
<table border='0' cellspacing='0' cellpadding='0' width='80%' align=center>
<tr>
<td class=body bgcolor='#f1f1f1' colspan='2'>
<CENTER><a class=lnk href='../client/add_client.php?$session[userid]'>Add Client Information</a> | <a class=lnk href='../services/add_category.php?$session[userid]'>Add Service Category</a> | <a class=lnk href='view_category.php?$session[userid]'>View Service Information</a> | <font class=body>Add Project Information</font> | <a class=lnk href='view_project.php?$session[userid]'>View All Projects</a> | <a class=lnk href='../project/add_foto.php?$session[userid]'>Add Project's Photograph</a> | <a class=lnk href='../logout.php?$session[userid]'>logout</a></CENTER>
</td>
</tr>
</table><br><br>
<font class=text><CENTER><B>Add Project Information</B></CENTER></font>
<br><br>
<FORM name=form action="add_project_action.php" method="post" enctype="multipart/form-data">
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="41%" id="AutoNumber1">
<? //-----------------------------Start Programing-------------------------------------
// Getting the data from table for client list box
$sql=mysql_query("select distinct client_name,id from client_info order by client_name");
// End of query for first list box
// for serial drop down list we will check if client is selected else we will display all the serial/////
if(isset($cat) and strlen($cat) > 0)
{
$sql_s=mysql_query("select distinct sl from client_info where id=$cat");
}
else
{
$sql_s=mysql_query("select distinct sl from client_info order by sl");
}
// end of query for client serial drop down list box
?>
<tr>
<td class=body class=textForm vAlign=top width="48%" align="right">Client Name </td>
<td width="55%">
<?
////////// Starting of client drop downlist /////////
echo "<select class=body name='cat' onchange=\"reload(this.form)\"><option value=''>Select Client</option>";
while($row = mysql_fetch_array($sql))
{
if($row['id']==@$cat)
{echo "<option selected value='$row[client_name]'>$row[client_name]</option>"."<BR>";
}
else
{
echo "<option value='$row[id]'>$row[client_name]</option>";
}
}
echo "</select>";
////////////////// This will end the first drop down list ///////////
//-----------------------------End Programing-------------------------------------
?>
</td>
<td width="3%"> </td>
</tr>
<tr>
<td width="48%" align="right"> </td>
<td width="55%"> </td>
<td width="3%"> </td>
</tr>
<tr>
<td class=body class=textForm vAlign=top width="48%" align="right">Client Serial </td>
<td width="55%">
<? //-----------------------------Start Programing for Client's Serial Number-------------------------------------
// Starting of drop downlist for client serial
echo "<select class=body name='c_sl'><option value=''>--Add Serial--</option>";
while($row1 = mysql_fetch_array($sql_s)) {
echo "<option value='$row1[sl]'>$row1[sl]</option>";
}
echo "</select>";
//-----------------------------End Programing-------------------------------------
?>
</td>
<td width="3%"> </td>
</tr>
<tr>
<td width="48%" align="right"> </td>
<td width="55%"> </td>
<td width="3%"> </td>
</tr>
<?
// The following part will build the Category List
$result = mysql_query( "SELECT category_id,category FROM service_category" );
while( $row = mysql_fetch_array( $result ) )
{
$service_category_list .=<<<__HTML_END
<option value="$row[0]">$row[1]</option>\n
__HTML_END;
}
mysql_free_result( $result );
?>
<tr>
<td class=body class=textForm vAlign=top width="48%" align="right">Project Category </td>
<td width="55%"><select class=body name='category'>
<option selected> - - - - select One - - - - </option>
<? echo <<<__HTML_END
$service_category_list
?>
</select>
</td>
<td width="3%"> </td>
</tr>
<tr>
<td width="48%" align="right"> </td>
<td width="55%"> </td>
<td width="3%"> </td>
</tr>
<tr>
<td class=body class=textForm vAlign=top width="48%" align="right">Project Name </td>
<td width="55%">
<INPUT class=body style="WIDTH: 200px" name=p_nam size="20"></td>
<td width="3%"> </td>
</tr>
<tr>
<td width="48%" align="right"> </td>
<td width="55%"> </td>
<td width="3%"> </td>
</tr>
<tr>
<td class=body class=textForm vAlign=top width="48%" align="right">Project Photo </td>
<td width="55%">
<INPUT class="body" style="WIDTH: 270px" name="photo_filename" size="20" type="file" /></td>
<td width="3%"> </td>
</tr>
<tr>
<td width="48%" align="right"> </td>
<td width="55%"> </td>
<td width="3%"> </td>
</tr>
<tr>
<td class=body class=textForm vAlign=top width="48%" align="right">Display Position </td>
<td width="55%">
<select class=body class=selectField name="post"><font class=res>
<option selected value=Inner>Inner </option>
<option value=Home>Home </option>
</select></td>
<td width="3%"> </td>
</tr>
<tr>
<td width="48%" align="right"> </td>
<td width="55%"> </td>
<td width="3%"> </td>
</tr>
<tr>
<td class=body class=textForm vAlign=top width="48%" align="right">Description </td>
<td width="55%">
<textarea class=body rows="6" name="des" cols="60" wrap=hard></textarea></td>
<td width="3%"> </td>
</tr>
<tr>
<td width="48%" align="right"> </td>
<td width="55%"> </td>
<td width="3%"> </td>
</tr>
<tr>
<td width="48%"> </td>
<td width="55%">
<INPUT type=submit value=submit name=Submit onclick='return CheckUser()'></td>
<td width="3%"> </td>
</tr>
</table>
<BR><BR>
<?
__HTML_END;
include ('../footer.php');
?>
add_project_action.php
<?php
include ("../include/newsession.php");
include('../include/db.php');
include('header.php');
session_start();
$images_dir = "single_pic";
$counter = 0;
// List of our known photo types
$known_photo_types = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/gif' => 'gif',
'image/bmp' => 'bmp',
'image/x-png' => 'png'
);
// GD Function List
$gd_function_suffix = array(
'image/pjpeg' => 'JPEG',
'image/jpeg' => 'JPEG',
'image/gif' => 'GIF',
'image/bmp' => 'WBMP',
'image/x-png' => 'PNG'
);
// Fetch the photo array sent by the form add_project.php
$photos_uploaded = $_FILES['photo_filename'];
//echo $photos_uploaded ;
$date =date("m/d/Y");
$status='yes';
// This is for the line breaking of the description field
$text = nl2br($des);
if(!array_key_exists($photos_uploaded['type'], $known_photo_types))
{
echo "Sorry this file is not supported by this module<br />";
}
else
{
mysql_query( "INSERT INTO project(project_name, project_image, service_id, client_id, photo_id, description, date, position, status) VALUES('$p_nam', '0', '".addslashes($_POST['category'])."', '$c_sl', '0', '$des', '$date', '$status', '$post')" );
$new_id = mysql_insert_id();
$filetype = $photos_uploaded['type'];
$extention = $known_photo_types[$filetype];
$filename = $new_id.".".$extention;
echo $filename;
//echo $extention;
mysql_query( "UPDATE project SET project_image='".addslashes($filename)."' WHERE project_id='".addslashes($new_id)."'" );
$result = mysql_query( $sql);
//include "../include/newsession.php";
//session_start();
//echo "helloooooooooooooo";
echo " <font class=text>Welcome $session[userid] !!!<br><br>";?>
<BR><BR><BR><BR><BR><BR><BR><BR>
<div align=center>
<CENTER><font class=text>Hello Admin (<?echo"$session[userid]";?>)</font></CENTER> <BR><BR>
<FONT class=body>Service Informarion has been added.</font><BR><BR>
<A class=lnk href="add_project.php">--- Insert more Service information ---</a><BR><BR>
<A class=lnk href="view_service.php">--- View Service information ---</a>
</FONT>
</div>
<BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR>
<?
include ('../footer.php');
}
?>
my table structure>>>>>
CREATE TABLE `project` (
`project_id` int(11) NOT NULL auto_increment,
`project_name` varchar(100) default NULL,
`project_image` varchar(100) default NULL,
`service_id` bigint(20) unsigned default NULL,
`client_id` bigint(20) unsigned default NULL,
`description` text,
`date` varchar(25) default NULL,
`position` varchar(25) default NULL,
`status` varchar(25) default NULL,
PRIMARY KEY (`project_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
please help me!!!!!
one more thing. I am a little bit confuse to use session that one of u said. BCouse i m useing it at admin login. so can i use it in this case???
i m confuse about it.
#5
Re: store image path
Posted 22 December 2005 - 08:06 AM
make these changes:
$sesssion[] to $_SESSION['']
you can't use php in an html script if your in the html structure
<a class=lnk href='../client/add_client.php?$session[userid]'>Add Client Information</a> to <a class=lnk href="../client/add_client.php?<?=$_SESSION[ 'userid' ];?>">Add Client Information</a>
don't hide errors unless you know the code works
@$ to $
share a link with us so we can figure out this problem easier.
#6
Re: store image path
Posted 23 December 2005 - 04:28 AM
but this is not that i have asked for!!
actually i'll work on tht session[] code that you've pointed. but now i need to store & retrieve the imgae from the data base.
as i m new in this field so i m going to work step by step. i know there are lots of things that i have to know.
can anyone help me???
#7
Re: store image path
Posted 23 December 2005 - 07:49 AM
#8
Re: store image path
Posted 23 December 2005 - 12:55 PM
Although this system is made simple you can justify it to make it work for your table.
Here is a table I made, it adds a new row foreach image and in some of the scripts below, shows every image in the db.
CREATE TABLE `imagedb` ( `id` smallint( 1 ) NOT NULL auto_increment, `authorip` varchar( 15 ) default NULL, `filename` varchar( 10 ) default NULL, `filetype` enum( 'gif','jpg,'jpeg',bmp' ), `url` varchar( 30 ), PRIMARYKEY( `id` ) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
To create the image try this: (I AM PRESUMING IN THIS SCRIPT THAT A FORM WITH AN INPUT NAME OF file WAS SUBMITTED)
<?php
if( $submit ) {
$file = $_POST[ 'file' ];
$yourip = $SERVER[ 'REMOTE_ADDR' ];
$filename = strrchr( $file, '/' );
$filetype = filetype( $file );
// or $filetype = strstr( $_POST[ 'file' ], '.' );
$createimg = mysql_query( "INSERT INTO `imagedb` ( `id`, `authorip`, `filename`, `filetype`, `url` ) VALUES ( '', '$yourip', '$filename', '$filetype' )" );
if( !$createimg ) {
echo 'error creating image';
}
}
?>
To show all images in gallery try this:
<?php
// Firstly connect to mysql as root, it has no password
$db = mysql_connect( 'localhost', 'root', '' );
mysql_select_db( 'imagedb' );
// Define our query:
// We need to retrieve information about a set image
$query = mysql_query( "SELECT url FROM `imagedb`" );
$fetchimg = mysql_fetch_row( $query );
for( $i=0; count($query) > $i; $i++ ) {
echo "<img src=\"$fetchimg\" alt=\"Image From Database\" />";
}
mysql_close( $db );
?>
Alternatively you could retrieve the image from the session, but we could be smarter by retrieving all images that match their ip address, try this:
<?php
session_start();
$db = mysql_connect( 'localhost', 'root', '' );
$yourip = $SERVER[ 'REMOTE_ADDR' ];
mysql_select_db( 'imagedb' );
$query = mysql_query( "SELECT url FROM `imagedb` WHERE ip='$yourip'" );
$fetchimg = mysql_fetch_row( $query );
for( $i=0; count($query) > $i; $i++ ) {
echo "<img src=\"$fetchimg\" alt=\"Your Image From Database\" />";
}
mysql_close( $db );
?>
I hope my coding shed some light on this matter.
#9
Re: store image path
Posted 23 December 2005 - 01:20 PM
#10
Re: store image path
Posted 23 December 2005 - 01:58 PM
#11
Re: store image path
Posted 25 December 2005 - 10:16 PM
We know the main things are the logic but sometime me who is working alone in this world may deserve any kind of help from you. Is it too much bad?
#12
Re: store image path
Posted 25 December 2005 - 10:47 PM
#13
Re: store image path
Posted 26 December 2005 - 06:01 AM
Additionally, dream in code in the resource preferred by many students, and it is the policy of the site not to provide direct answers to academic assignments, as it defeats the purpose of the assignments in the first place.
Wizzy, the rules of each of the programming forums are clearly documented in pinned topics at the head of each forum...they include guidelines on providing code. Supersloth does not simply make these comments to increase his post count.
#14
Re: store image path
Posted 26 December 2005 - 11:25 AM
I try to show everyone with a php problem, this same edge. Its like another alternative if you like. Because I state nowhere that my code is even any good. I just appeciate the thanks if it did work.
I help out mostly in this section with other 60 odd posts here, by showing the most common use of functions and commands for simple programs to work. It might still help people, because no one will read through that code to make it work, thats why I rewrote it, because it was to long; and for evidence:
Quote
If someone personally asked it of me, then maybe I would, but otherwise I just show them what would have been easier to do. Please understand that this was like "theory" code untill that user looked through his file for the correct part, as Amadeus stated. So I did nothing wrong.
#15
Re: store image path
Posted 27 December 2005 - 07:29 AM
My post was meant only to point out the goals for the site, and to let you know that the admins do not post merely for the sake of posting.
You seem like you may want to have the last word on the subject, so if you wish to discuss further, we can take it to PM.
|
|

New Topic/Question
This topic is locked


MultiQuote






|