Something that always worked for me is forcing users to upload their images directly to my mysql db. PHP will be encoded in base64 and stored in blob. Then you do something similar to what Michael said to get and display the image. I included some code from a project that I worked on in 2008. I would not copy it if this is the method you want to use with its old code.
This is PHP for loading and storing in the database. Obviously, replace your information and connect to your own database.
<?php include("auth.php"); // uploadimg.php // By Tyler Biscoe // 09 Mar 2008 // Test file for image uploads include("connect.php"); include("include/header.php"); $max_file_size = 786432; $max_kb = $max_file_size/1024; if($_POST["imgsubmit"]) { if($_FILES["file"]["size"] > $max_file_size) { $error = "Error: File size must be under ". $max_kb . " kb."; } if (!($_FILES["file"]["type"] == "image/gif") && !($_FILES["file"]["type"] == "image/jpeg") && !($_FILES["file"]["type"] == "image/pjpeg")) { $error .= "Error: Invalid file type. Use gif or jpg files only."; } if(!$error) { echo "<div id='alertBox'> Image has been successfully uploaded! </div>"; $handle = fopen($_FILES["file"]["tmp_name"],'r'); $file_content = fread($handle,$_FILES["file"]["size"]); fclose($handle); $encoded = chunk_split(base64_encode($file_content)); $id = $_POST["userid"]; echo $_FILES["file"]["tmp_name"]; $default_exist_sql = "SELECT * FROM members WHERE id='".$id."'"; $default_result = mysql_query($default_exist_sql); $results = mysql_fetch_array($default_result); if(!$results["default_image"]) { $insert_sql = "UPDATE members SET default_image = '$encoded' WHERE id='". $id ."'"; mysql_query($insert_sql); } $sql = "INSERT INTO images (userid, sixfourdata) VALUES ('$id','$encoded')"; mysql_query($sql); } else { echo "<div id='alertBox'>". $error . "</div>"; } } ?> <br /> <font class="heading"> Upload images </font> <br /><br /> <form enctype = "multipart/form-data" action = "<?php $_SERVER['PHP_SELF']; ?>" method = "post" name = "uploadImage"> <input type = "hidden" name="userid" value = "<?php echo $_GET["userid"]; ?>" > <input id="stextBox" type="file" name="file" size="35"><br /> <input type="submit" name="imgsubmit" value="Upload"> </form> <?php include("include/footer.php"); ?>
The following file displays the file:
<?php // image.php // By Tyler Biscoe // 09 Mar 2008 // File used to display pictures include("connect.php"); $imgid = $_GET["id"]; $result = mysql_query("SELECT * FROM images WHERE imgid=" . $imgid . ""); $image = mysql_fetch_array($result); echo base64_decode($image["sixfourdata"]); echo $image["sixfourdata"]; ?>
Then:
<img src="image.php?id=your_img_id">
source share