store image path

i want to store image path into the data

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 11971 Views - Last Post: 24 January 2013 - 04:36 PM Rate Topic: -----

#1 chitara  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 89
  • Joined: 16-December 05

store image path

Posted 21 December 2005 - 11:16 AM

hello everybody!!!

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.
Is This A Good Question/Topic? 1

Replies To: store image path

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: store image path

Posted 21 December 2005 - 11:42 AM

Can you post the code you were using? there several ways to accomplish this...it would help if we knew what type of set up you were using. If you could post the code you've attempted, that would be most appreciated.
Was This Post Helpful? 0
  • +
  • -

#3 Wizzy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 408
  • Joined: 20-November 05

Re: store image path

Posted 21 December 2005 - 03:52 PM

It is actually easy to achieve this, here is an example:

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.
Was This Post Helpful? 2
  • +
  • -

#4 chitara  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 89
  • Joined: 16-December 05

Re: store image path

Posted 22 December 2005 - 06:05 AM

I have tried to work on this. Here i can store the file name in the database but cann't store the images in the folder that i assign.

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 "&nbsp;&nbsp;&nbsp;&nbsp;<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>&nbsp; | &nbsp;<a class=lnk href='../services/add_category.php?$session[userid]'>Add Service Category</a>&nbsp; | &nbsp;<a class=lnk href='view_category.php?$session[userid]'>View Service Information</a>&nbsp; | &nbsp;<font class=body>Add Project Information</font>&nbsp; | &nbsp;<a class=lnk href='view_project.php?$session[userid]'>View All Projects</a>&nbsp; | &nbsp;<a class=lnk href='../project/add_foto.php?$session[userid]'>Add Project's Photograph</a>&nbsp; | &nbsp;<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&nbsp;&nbsp;</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%">&nbsp;</td>
</tr>
<tr>
<td width="48%" align="right">&nbsp;</td>
<td width="55%">&nbsp;</td>
<td width="3%">&nbsp;</td>
</tr>
<tr>
<td class=body class=textForm vAlign=top width="48%" align="right">Client Serial&nbsp;&nbsp;</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%">&nbsp;</td>
</tr>

<tr>
<td width="48%" align="right">&nbsp;</td>
<td width="55%">&nbsp;</td>
<td width="3%">&nbsp;</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&nbsp;&nbsp; </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%">&nbsp;</td>
</tr>
<tr>
<td width="48%" align="right">&nbsp;</td>
<td width="55%">&nbsp;</td>
<td width="3%">&nbsp;</td>
</tr>
<tr>
<td class=body class=textForm vAlign=top width="48%" align="right">Project Name&nbsp;&nbsp;</td>
<td width="55%">
<INPUT class=body style="WIDTH: 200px" name=p_nam size="20"></td>
<td width="3%">&nbsp;</td>
</tr>
<tr>
<td width="48%" align="right">&nbsp;</td>
<td width="55%">&nbsp;</td>
<td width="3%">&nbsp;</td>
</tr>
<tr>
<td class=body class=textForm vAlign=top width="48%" align="right">Project Photo&nbsp;&nbsp;</td>
<td width="55%">
<INPUT class="body" style="WIDTH: 270px" name="photo_filename" size="20" type="file" /></td>
<td width="3%">&nbsp;</td>
</tr>
<tr>
<td width="48%" align="right">&nbsp;</td>
<td width="55%">&nbsp;</td>
<td width="3%">&nbsp;</td>
</tr>
<tr>
<td class=body class=textForm vAlign=top width="48%" align="right">Display Position&nbsp;&nbsp;</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%">&nbsp;</td>
</tr>
<tr>
<td width="48%" align="right">&nbsp;</td>
<td width="55%">&nbsp;</td>
<td width="3%">&nbsp;</td>
</tr>

<tr>
<td class=body class=textForm vAlign=top width="48%" align="right">Description&nbsp;&nbsp;</td>
<td width="55%">
<textarea class=body rows="6" name="des" cols="60" wrap=hard></textarea></td>
<td width="3%">&nbsp;</td>
</tr>
<tr>
<td width="48%" align="right">&nbsp;</td>
<td width="55%">&nbsp;</td>
<td width="3%">&nbsp;</td>
</tr>

<tr>
<td width="48%">&nbsp;</td>
<td width="55%">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<INPUT type=submit value=submit name=Submit onclick='return CheckUser()'></td>
<td width="3%">&nbsp;</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 "&nbsp;&nbsp;&nbsp;&nbsp;<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.
Was This Post Helpful? 0
  • +
  • -

#5 Wizzy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 408
  • Joined: 20-November 05

Re: store image path

Posted 22 December 2005 - 08:06 AM

okay there are things that are wrong in that script
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.
Was This Post Helpful? 1
  • +
  • -

#6 chitara  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 89
  • Joined: 16-December 05

Re: store image path

Posted 23 December 2005 - 04:28 AM

thank Wizzy!!!

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???
Was This Post Helpful? 0
  • +
  • -

#7 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: store image path

Posted 23 December 2005 - 07:49 AM

That's a good chunk od code to look through...can you isolate the section where you are actually uploading the file? I'm assuming you have an error handler around that section...checking to see if the file has successfully uploaded?
Was This Post Helpful? 0
  • +
  • -

#8 Wizzy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 408
  • Joined: 20-November 05

Re: store image path

Posted 23 December 2005 - 12:55 PM

Okay I made a system for you, it uses a session to retrieve the image id number, when the system is created various information are added to the database.

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.
Was This Post Helpful? 1
  • +
  • -

#9 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4503
  • View blog
  • Posts: 28,409
  • Joined: 21-March 01

Re: store image path

Posted 23 December 2005 - 01:20 PM

as nice as that is wizzy, around here we like to help people solve the problems in their code rather than writing code for them ;)
Was This Post Helpful? 0
  • +
  • -

#10 Wizzy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 408
  • Joined: 20-November 05

Re: store image path

Posted 23 December 2005 - 01:58 PM

well no offense, but that code was messy and had problems...but mine is only an example.
Was This Post Helpful? 0
  • +
  • -

#11 chitara  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 89
  • Joined: 16-December 05

Re: store image path

Posted 25 December 2005 - 10:16 PM

is that a big problem if you (php-boss) people help the novice like us by code writing???

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?
Was This Post Helpful? 0
  • +
  • -

#12 Wizzy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 408
  • Joined: 20-November 05

Re: store image path

Posted 25 December 2005 - 10:47 PM

lol, supersloth just makes alot of comments on these things; mainly part of his job on this forum. But it is okay for you to use my code, I didn't copyright it, I just want you to appreciate that I took the time to write it for you :)
Was This Post Helpful? 0
  • +
  • -

#13 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: store image path

Posted 26 December 2005 - 06:01 AM

Actually, the reason that supersloth made that comment is that one of the goals of the forum is to educate people when it comes to writing code. that has the end result of the user understanding the code, and being able to do it again. If code is simply provided as a cut and paste, the user learns nothing, and is not able to reproduce the code (except for another cut and paste), does not understand where the mistakes were made in the first place, and therefore does not increases their skill level.

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.
Was This Post Helpful? 0
  • +
  • -

#14 Wizzy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 408
  • Joined: 20-November 05

Re: store image path

Posted 26 December 2005 - 11:25 AM

Well I clearly commented what each script does and I never even said it works for I haven't tried it, I only wrote it as a guideline. Anyone who knows PHP will know all the functions I used and how to make my simple code, and how to make it better.

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

That's a good chunk of code to look through...can you isolate the section where you are actually uploading the file? I'm assuming you have an error handler around that section...checking to see if the file has successfully uploaded?


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.
Was This Post Helpful? 0
  • +
  • -

#15 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: store image path

Posted 27 December 2005 - 07:29 AM

If you take a look through the posts, you'll notice that no one has accused you of doing anything wrong at all.

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.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2