Jump to content

Image to MySQL database


sayr

Recommended Posts

Hello,

I have been looking for a good and as simple code as possible for image upload form and yet I haven't found a good one..
I was wondering that some of you might have some good and simple codes for this one.
So what I wanted is a php & html code which has form for image uploading, after the image is selected from your pc, there is "send" button, after that it automaticly adds the picture into mysql database.
Im also curious to hear your toughts about the field type in database, someone has recommended to use BIN type, but I have also heared people talking about a picture link (didn't really get that one) If you have ideas for this on too, do tell!

Thanks!
Link to comment
https://forums.phpfreaks.com/topic/33320-image-to-mysql-database/
Share on other sites

This is what I am using:
[code]
<?php if(isset($_POST['submit'])){

//checks file extension
$filename = $_FILES['uploadedfile']['name'];
$ext = explode(".", $filename);

if ($ext[1] != "jpg" && $ext[1] != "gif" && $ext[1] != "png" && $ext[1] !="jpeg")
{echo "<p><b>The file you uploaded does not have an image extension!</b></p>";}

else {
// Where the file is going to be placed
$target_path = "images/photo/";
$datetime = date("Y-m-d H:i:s");

$counter = ("images/photo/photonumber.txt");
$hits = file($counter);
$hits[0] ++;
$fp = fopen($counter , "w");
fputs($fp , "$hits[0]");
fclose($fp);
$number = file_get_contents("images/photo/photonumber.txt");

/* Add the original filename to our target path. 
Result is "uploads/filename.extension" */
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);

$photoname = $ext[0];


$target_path = "images/photo/";

$target_path = $target_path . basename("photo".$number.".jpg");

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{require("../mysqlconnection.php");
$insert = mysql_query("INSERT INTO photo(username, photonumber, photoname, photodate)
VALUES('$username', '$number', '$photoname', '$datetime') ");}
else
{echo"<p><b>There was an error uploading the image, please try again!</b></p>";}

if($insert === false)
{echo"<p><b>There was an error uploading the image, please try again!</b></p>";}
else
{require("../mysqlconnection.php");
$read = mysql_query("SELECT id FROM photo WHERE photonumber='$number' AND username='$username'");
$r = mysql_fetch_array($read);
$id = $r['id'];
header("location: confirm.php?confirm=photoedit_upload&id=$id");}}}?>
<form enctype="multipart/form-data" action="" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" name=submit value="Upload File" />
</form>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.