runnerjp Posted May 12, 2007 Share Posted May 12, 2007 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 Quote Link to comment Share on other sites More sharing options...
gandaliter Posted May 12, 2007 Share Posted May 12, 2007 Hi, you need to add the location of the image to the database, rather than the image itself. So if you have copied the image to http://www.test.com/test.jpg you would just put that string into the database. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted May 12, 2007 Share Posted May 12, 2007 it depends on the database if you have records already (i aussme you do as its a profile) your just use the update syntax for sql! Quote Link to comment Share on other sites More sharing options...
chronister Posted May 12, 2007 Share Posted May 12, 2007 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 Quote Link to comment Share on other sites More sharing options...
john010117 Posted May 12, 2007 Share Posted May 12, 2007 You can not "physically" add images to a MySQL database. Quote Link to comment Share on other sites More sharing options...
chronister Posted May 12, 2007 Share Posted May 12, 2007 You can not "physically" add images to a MySQL database. Actually you can. You would set the data type to binary. But it increases server load, and is costly in terms of performance and such. http://www.codewalkers.com/c/a/Database-Articles/Storing-Images-in-Database/ Quote Link to comment Share on other sites More sharing options...
john010117 Posted May 12, 2007 Share Posted May 12, 2007 Oh, right. My bad. Quote Link to comment Share on other sites More sharing options...
chronister Posted May 12, 2007 Share Posted May 12, 2007 funny.... 2 different threads wanting the exact same thing... this one and http://www.phpfreaks.com/forums/index.php/topic,140290.0.html Both of them are being told, don't insert the image... just store links and file info. Quote Link to comment Share on other sites More sharing options...
runnerjp Posted May 12, 2007 Author Share Posted May 12, 2007 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"; ?> Quote Link to comment Share on other sites More sharing options...
chronister Posted May 12, 2007 Share Posted May 12, 2007 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 Quote Link to comment Share on other sites More sharing options...
Broniukas Posted May 12, 2007 Share Posted May 12, 2007 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 } ?> Quote Link to comment Share on other sites More sharing options...
runnerjp Posted May 12, 2007 Author Share Posted May 12, 2007 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? Quote Link to comment Share on other sites More sharing options...
runnerjp Posted May 12, 2007 Author Share Posted May 12, 2007 also it didnt set the image id to the users id :( Quote Link to comment Share on other sites More sharing options...
Broniukas Posted May 12, 2007 Share Posted May 12, 2007 <?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; } ?> Quote Link to comment Share on other sites More sharing options...
runnerjp Posted May 12, 2007 Author Share Posted May 12, 2007 but the uploader is not setting the image with an id....how comes? Quote Link to comment Share on other sites More sharing options...
Broniukas Posted May 12, 2007 Share Posted May 12, 2007 <img src="get_image.php"> get_image.php must contain script i wrote before Quote Link to comment Share on other sites More sharing options...
runnerjp Posted May 12, 2007 Author Share Posted May 12, 2007 ahh but you know the script u wrote.... does it not insert the image into the database Quote Link to comment Share on other sites More sharing options...
Broniukas Posted May 12, 2007 Share Posted May 12, 2007 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; } ?> Quote Link to comment Share on other sites More sharing options...
runnerjp Posted May 12, 2007 Author Share Posted May 12, 2007 yes but i mean that the 1st code you wrote inserting the image into the database...does it insert the url into the database or the actual image because i want the url inserting Quote Link to comment Share on other sites More sharing options...
MadTechie Posted May 12, 2007 Share Posted May 12, 2007 who said planning is over rated!! Quote Link to comment Share on other sites More sharing options...
runnerjp Posted May 12, 2007 Author Share Posted May 12, 2007 wat u mean? Quote Link to comment Share on other sites More sharing options...
MadTechie Posted May 12, 2007 Share Posted May 12, 2007 atleast 30% of my time on any given project is used for planning on how its going to work.. if you jump right inn you spent over 50% of the time trying to workout how your going to do it.. fail to plan and your plan will fail! Quote Link to comment Share on other sites More sharing options...
runnerjp Posted May 12, 2007 Author Share Posted May 12, 2007 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.... Quote Link to comment Share on other sites More sharing options...
chronister Posted May 13, 2007 Share Posted May 13, 2007 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. Quote Link to comment Share on other sites More sharing options...
runnerjp Posted May 13, 2007 Author Share Posted May 13, 2007 that sounds like a good idea... how would i insert that into my code tho?? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.