1,105,286 Community Members

Saving mage in a path instead of storing the real image in mysql

Member Avatar
mrcniceguy
Posting Whiz in Training
285 posts since Mar 2008
Reputation Points: 5 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

Is there anyone who can explain to me how can i store image in a folder to the server then the path to mysql.
i dont know even how to get started,i will appreciate for your help.
Or links where i can find the tutorials.

Member Avatar
sikka_varun
Junior Poster in Training
94 posts since Dec 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 13 [?]
Skill Endorsements: 1 [?]
 
0
 

Hi..
Check out this tutorial... this will help you...
http://www.htmlgoodies.com/beyond/webmaster/article.php/3548746

When you specify the location to store the images, then its mostly the hard path, for eg. c:\inetputb\wwwroot\mysitename\images...
or
/var/home/sitename/images

So take care of the paths...

Member Avatar
Aamit
Posting Whiz
343 posts since Apr 2008
Reputation Points: -7 [?]
Q&As Helped to Solve: 15 [?]
Skill Endorsements: 0 [?]
 
0
 

Suppose this is your form in aaa.php

<table width="99%"  border="0" cellspacing="0" cellpadding="0">
        <tr>
          <th width="5%" height="62" scope="col">&nbsp;</th>
          <th width="51%" align="left" valign="top" scope="col"><form name="form2" method="post" action="aaa.php">
		//here you can view uploaded image
		  <img src="<?php echo $path ?>" height="150" width="150">
          </form></th>
          <th width="44%" align="left" valign="top" scope="col"><form action="aaa.php" method="POST" enctype="multipart/form-data">
    <p><br>
    <font size="1"></font><br>
    <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
      <tr>
        <td width="246">
          <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
          <input name="userfile" type="file" id="userfile">
        </td>
        <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
      </tr>
    </table>
    <p><br>
    </form></th>
        </tr>
      </table>

create uploads folder in your directory and php code do store

$uploadDir = 'uploads/';

if(isset($_POST['upload']))
{
	
	$fileName = $_FILES['userfile']['name'];
	$tmpName = $_FILES['userfile']['tmp_name'];
	$fileSize = $_FILES['userfile']['size'];
	$fileType = $_FILES['userfile']['type'];
	
	$filePath = $uploadDir . $fileName;

	$result = move_uploaded_file($tmpName, $filePath);
	if (!$result)
	{
	//echo "Error uploading file";
	$filePath="uploads/no_images.jpeg";
	}
		
	if(!get_magic_quotes_gpc())
	{
	$fileName = addslashes($fileName);
	$filePath = addslashes($filePath);
	} 

	$query = "update table set path='$filePath' where user_id='$user_id'";//change query according to you
	
	mysql_query($query) or die('Error, query failed');
	$path=$filePath;
	//echo "<br>File $fileName uploaded<br>";

create column path in your database...to store path of image in database...which is in uploads folder

Member Avatar
mrcniceguy
Posting Whiz in Training
285 posts since Mar 2008
Reputation Points: 5 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you for the Code it works fine))
the problem comes when displaying the photos,so do i display them?
Also how do i save to database?bcoz i saved as varchar when i upload the photo i saw the whole link in my db.
when i saved as longblob i just saw the bait-34(a certain number))
thank you))

Member Avatar
Aamit
Posting Whiz
343 posts since Apr 2008
Reputation Points: -7 [?]
Q&As Helped to Solve: 15 [?]
Skill Endorsements: 0 [?]
 
0
 

Suppose you have to display image which is uploaded for user1
display.php

<?php
//change query according to your table structure, column names
 $sql=mysql_query("SELECT * FROM table where user_fname='$user_fname'");
		  $row = mysql_fetch_array($sql);
		  $user_fname=$row['user_fname'];
		  $pass=$row['user_lname'];
		  $path=$row['path'];//here you get path where you store image for user1
?>

In table you have to write like

<table width="100%" style="height:157%"  border="0" cellpadding="0" cellspacing="0">
         <tr>
             <th scope="col" > 
		<img src="<?php echo $path ?>" height="150" width="150">
             </th>
         </tr>
 </table>

use like path varchar(30) to store image path in database or change size according to you

I think it solves your problem

Member Avatar
mrcniceguy
Posting Whiz in Training
285 posts since Mar 2008
Reputation Points: 5 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

Below is how i did)))but image still dont show up only area with width=500 and height=400 with an X.

<?php
//database connection routine
// Connecting to a MySql Database
include"config.php";
$user='peacemaker';
//selecting the path of the image for specific user
 $sql=mysql_query("SELECT * FROM picha where user='$user'");
		  $row = mysql_fetch_array($sql);
		  $user=$row['user'];
		  $filename=$row['filename'];
		  $path=$row['path'];//here you get path where you store image for user1
echo"<font color=blue> filename:$filename Usename: $user_fname<br></font>";// Display  original filename and username

		?>	  
				  
	<img src=<?php echo"$path";?> width=500 height=400>	<br>		  
		
		
	<?php

	echo"$path";	//here i just tried to view the file path	  
				  
	?>

after trying to view the file path it shows
c:/wamp/www/photo/uploads/Nicwa.jpg
where i`m i getting wrong???

Member Avatar
sikka_varun
Junior Poster in Training
94 posts since Dec 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 13 [?]
Skill Endorsements: 1 [?]
 
0
 

Hi..
Confirm the filename and check if the file is getting properly uploaded.. check the path...

Member Avatar
Aamit
Posting Whiz
343 posts since Apr 2008
Reputation Points: -7 [?]
Q&As Helped to Solve: 15 [?]
Skill Endorsements: 0 [?]
 
0
 

To show image img syntax is

<img src="" height="" width="">

not

<img src=<?php echo"$path";?> width=500 height=400>//wrong

i mentioned in earlier post (5)also ....
so do like that

<img src="<?php echo $path ?>" height="500" width="400">
Member Avatar
ssuave
Newbie Poster
1 post since May 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi I have followed all the steps and I understand the concept behind uploading and displaying an image, however the image still does not display for me.

I believe the problem is with the rights of the file or something. The reason why I think so is because i have tried uploading an image to the "uploads" folder with ftp and i can easily display it however if i upload the same exact image with the upload form and then i try to view it I get the following message "403 Forbidden".

If it makes any difference I am using a hosted site.

Any idea why when i upload an image with the upload form it becomes forbidden in the folder?

Member Avatar
yanmyoe
Newbie Poster
1 post since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank Sir .. I have learn your method and Can upload image successfully!!! :D

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article