Jump to content

[SOLVED] can any 1 tell me or show me how i can put images into a database?


runnerjp

Recommended Posts

hey guys i have this upload script all ready

 

<?php
print_r($_FILES);
move_uploaded_file($_FILES["profile_image"]["tmp_name"],
  "profileimages/" . $_FILES["profile_image"]["name"]);
if ($_FILES["profileimages"]["error"] > 0)
    {
    echo "Apologies, an error has occurred. Error Code: " . $_FILES["profileimages"]["error"];
    }
else
    {

    move_uploaded_file($_FILES["profileimages"]["tmp_name"],
  "profileimages/" . $_FILES["profileimages"]["name"]);
}
if (($_FILES["profileimages"]["type"] == "image/gif")
  || ($_FILES["profileimages"]["type"] == "image/jpeg")
  || ($_FILES["profileimages"]["type"] == "image/png" )
  && ($_FILES["profileimages"]["size"] < 10000))
  {
  move_uploaded_file($_FILES["profileimages"]["tmp_name"],
    "profileimages/" . $_FILES["profileimages"]["name"]);
  }
else
  {
  echo "Files must be either JPEG, GIF, or PNG and less than 10,000 kb";
  }
?>

 

how i add it to a database with the users id number so i can find the picture or the userrs id...like avator picture

Link to comment
Share on other sites

In your user's profile table, simply add a field called image or avatar. Then it looks like you store all profile images in a folder called profileimages/ and say your path to that folder is like www.testsite.com/members/profile_images, then you can define a variable called

$profile_images='www.testsite.com/members/profileimages/;

 

Then simply store the filename of the user's avatar in the database and move the file to profileimages. Then when you need to access it to show it on a users page, you do something like

 

<img src=<?=$profile_images.$var_from_database ?> ?>

 

Hope this helps

Link to comment
Share on other sites

yes so do not insert pictures but add there links :)

 

would something like this do then

 

<?



//UPLOAD CHECK

$table_name = "$tbl_images";

	$connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect to database.");



	$db = @mysql_select_db($db_name, $connection) or die("Couldn't select database.");



	$sql = "SELECT image_id, directory, url, image	FROM $table_name

	WHERE image_id = \"$comm_id\"

	";



	$result = @mysql_query($sql,$connection) or die("Couldn't execute query.");

	$num=mysql_num_rows($result);



	while ($row = mysql_fetch_array($result)) {



	$image_id = $row['image_id'];

	$directory = $row['directory'];

	$url = $row['url'];

	$image = $row['image'];

	}

	if(isset($image)) {

	$location ="$directory/$image";

	$showimg="../$url/$image";

	?>

Link to comment
Share on other sites

That looks correct. Try it and see what you come up with. If you have problems post back and we will see what we can do.

 

*note* so that your code is highlighted, please use <?php  instead of  <? in the beginning. It makes it easier to read and find errors.

 

Nate

Link to comment
Share on other sites

U can try this:

 

<?
if ($_POST['submit']) {
    mysql_connect("localhost","root","password");
    mysql_select_db("images");

    $file=$_POST['file'];
    $data = addslashes(fread(fopen($file, "r"), filesize($file)));
    $extension = strtolower(substr(strrchr($file, "."), 1));
    $filesize = filesize($file);
    $filename = basename($file);

    $result=mysql_wuery("INSERT INTO images (filename,filesize,extension,data) ".
        "VALUES ($filename,$filesize,$extension,$data)");

    mysql_close();

} else {
?>

    <form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
    File to upload/store in database:<br>
    <input type="file" name="file"  size="40">
    <p><input type="submit" name="submit" value="submit">
    </form>

<?php
}
?>

Link to comment
Share on other sites

ok i now have the image entered into a database...in the end i used this

 

<?php



//UPLOAD CHECK

$table_name = "$tbl_images";

	$connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect to database.");



	$db = @mysql_select_db($db_name, $connection) or die("Couldn't select database.");



	$sql = "SELECT image_id, directory, url, image	FROM $table_name

	WHERE image_id = \"$comm_id\"

	";



	$result = @mysql_query($sql,$connection) or die("Couldn't execute query.");

	$num=mysql_num_rows($result);



	while ($row = mysql_fetch_array($result)) {



	$image_id = $row['image_id'];

	$directory = $row['directory'];

	$url = $row['url'];

	$image = $row['image'];

	}

	if(isset($image)) {

	$location ="$directory/$image";

	$showimg="../$url/$image";

	?>

	<p><b>Before uploading a new picture, you must first delete the old one:</b></p>

	<form action="remove_pic.php" method="POST">

	<p><input type="checkbox" name="remove" checked>

	<IMG SRC="thumbs/phpThumb.php?src=<?echo $showimg ?>&w=150" border="0">

	<br><input type="submit" name="submit" value="Delete!">

	<input type="hidden" name="filename" value="<?echo $location ?>">

	<input type="hidden" name="image_id" value="<?echo $image_id ?>">

	</form>

	<input type=button value="Cancel" onClick="history.go(-1)"></p>

	<?

	exit;

        }else{

	//END Upload check



$directory = "$scriptpath/$imgdir/comm";



//$acceptable_file_types used by upload() method

//

// Limit acceptable uploads based on MIME type. Common MIME types

// include: text/plain, image/gif, image/jpeg image/png



// To accept ONLY gifs's use the following

//acceptable_file_types = "image/gifs";



// Accept GIF and JPEG files

//$acceptable_file_types = "image/gif|image/jpeg|image/pjpeg";



// Accept all image files

$acceptable_file_types = "image";



// Accept ALL files (NOT recommended!)

//$acceptable_file_types = "";



}

$url = "$imgdir/comm/";



require("fileupload.class.php");



// Path to the directory where uploaded files will be saved. MUST end

// with a trailing slash unless you use $path = ""; to upload to

// current directory. chmod 777 this directory.



$path = "$directory/";







// If no extension is supplied, and the browser or PHP can not figure

// out what type of file it is, you can add a default extension



$default_extension = ".jpg"; // example: ".jpg"



// Handles identically named uploaded files.

//

// OPTIONS:

//   1 = overwrite mode

//   2 = create new with incremental extention

//   3 = do nothing if exists, highest protection



$mode = 1;





/*

**

** UPLOAD LOGIC

** --------------------------------------------------------------------

**

*/

if (isset($_REQUEST['submitted'])) {



	/*

		A simpler way of handling the submitted upload form

		might look like this:



		$my_uploader = new uploader('en'); // errors in English



		$my_uploader->max_filesize(30000);

		$my_uploader->max_image_size(800, 800);

		$my_uploader->upload('userfile', 'image/gif', '.gif');

		$my_uploader->save_file('uploads/', 2);



		if ($my_uploader->error) {

			print($my_uploader->error . "<br><br>\n");

		} else {

			print("Thanks for uploading " . $my_uploader->file['name'] . "<br><br>\n");

		}

	*/



	// Create a new instance of the class

	$my_uploader = new uploader($_POST['language']); // for error messages in french, try: uploader('fr');



	// OPTIONAL: set the max filesize of uploadable files in bytes

	$my_uploader->max_filesize(400000);



	// OPTIONAL: if you're uploading images, you can set the max pixel dimensions

	$my_uploader->max_image_size(600, 600); // max_image_size($width, $height)



	// UPLOAD the file



	if ($my_uploader->upload("userfile", $acceptable_file_types, $default_extension)) {

		$my_uploader->save_file($path, $mode);

	}



	if ($my_uploader->error) {

		echo $my_uploader->error . "<br><br>\n";



	} else {



		// Print all the array details...

		//print_r($my_uploader->file);



		// ...or print the file

		if(stristr($my_uploader->file['type'], "image")) {

			echo "<img src=\"" . $url . $my_uploader->file['name'] . "\" border=\"0\" alt=\"\">";

			?><br><br>

			<?

			echo "<p align=\"center\">View <a href=\"community.php?comm_id=$image_id\">Community</a>!</p>";



			$newimage = $my_uploader->file['name'];

			$mydir = "$directory";

			$table_name = "$tbl_images";

			$connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect.");



			$db = @mysql_select_db($db_name, $connection) or die("Couldn't select database.");



			$sql = "INSERT INTO $table_name

			(image_id, directory, image, url, displayname)



			VALUES



			(\"$image_id\",\"$mydir\", \"$newimage\", \"$url\", \"Community\")";



                $result = @mysql_query($sql,$connection) or die("Couldn't execute query.");



			exit;

		} else {

			$fp = fopen($path . $my_uploader->file['name'], "r");

			while(!feof($fp)) {

				$line = fgets($fp, 255);

				echo $line;

			}

			if ($fp) { fclose($fp); }

		}

		}

	}



?>

<form enctype="multipart/form-data" action="<?= $_SERVER['PHP_SELF']; ?>" method="POST">

<input type="hidden" name="submitted" value="true">

    <input type="hidden" name="image_id" value="<?echo $comm_id ?>">

	<input name="userfile" type="file">

	<br><br>

	<input type="submit" value="Upload File">

	</form>

	  <input type=button value="Cancel" onClick="history.go(-1)">



 

 

but how do i now pull the image out of the database?

Link to comment
Share on other sites

<?php 
$conn = mysql_connect("localhost", "user", "password") 
  OR DIE (mysql_error()); 
@mysql_select_db ("hermawan", $conn) OR DIE (mysql_error()); 
$sql    = "SELECT * FROM image WHERE image_id=".$_GET["iid"]; 
$result = mysql_query ($sql, $conn); 
if (mysql_num_rows ($result)>0) { 
  $row = @mysql_fetch_array ($result); 
  $image_type = $row["image_type"]; 
  $image = $row["image"]; 
  Header ("Content-type: $image_type"); 
  print $image; 
} 
?>

Link to comment
Share on other sites

code like this. u just need to print binary data

 

<?php 
$conn = mysql_connect("localhost", "user", "password") 
  OR DIE (mysql_error()); 
@mysql_select_db ("hermawan", $conn) OR DIE (mysql_error()); 
$sql    = "SELECT * FROM image WHERE image_id=".$_GET["iid"]; 
$result = mysql_query ($sql, $conn); 
if (mysql_num_rows ($result)>0) { 
  $row = @mysql_fetch_array ($result); 
  $image_type = $row["image_type"]; 
  $image = $row["image"]; 
  Header ("Content-type: $image_type"); 
  print $image; 
} 
?>

Link to comment
Share on other sites

wlel i have the plan lol its just been abled to do it... iv set out the upload script (which is not recording the users ids or sum reaosn) the picture has a uniqe id that is same as users id, then when they log on there image will show :):)

 

loaded it into a file because it saves on space....

Link to comment
Share on other sites

Are you relying on the auto increment id's to be the same in both the users table and in the image table? If so, you may end up running into issues there. You would be better off creating a userID field in the images table and inserting the user id into the images table that way.

 

But that's just my opinion.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.