Jump to content

[SOLVED] Upload image to mysql with php


twistisking

Recommended Posts

I thought this code was perfect but unfortunately, every time I attempt to upload an image, it will only upload 1 kb of data. Can anyone tell me what Im doing wrong?

 

<?php

$con = mysql_connect("host","login","password");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("Database", $con);

 

function getimage($file){

  $takeFile = fopen($file, "r");

  $file = fread($takeFile, filesize($file));

  fclose($takeFile);

  return $file;

}

 

 

 

function getfileType( $name ){

  $name = explode(".", $name);

  $name = array_reverse($name);

  $name = strtolower($name[0]);

  return $name;

}

 

$fp      = fopen($tmpName, 'r');

$content = fread($fp, filesize($tmpName));

$content = addslashes($content);

fclose($fp);

 

 

$allowedImageTypes = array("jpg");

if(empty($_FILES['image']['tmp_name'])){

  die("File not uploaded");

} else {

  $fileType = $_FILES['image']['name'];

 

 

  if(in_array(getfileType($fileType), $allowedImageTypes)){

    $fileContent = getimage($_FILES['imgfile']['tmp_name']);

 

    $sql="INSERT INTO post (poster, poster2, title, category, tagline, thearticle, image)

    VALUES

    ('$_POST[poster]','$_POST[poster2]','$_POST[title]','$_POST[category]','$_POST[tagline]','$_POST[thearticle]','$content ')";

 

    if (!mysql_query($sql,$con))

    {

      die('Error: ' . mysql_error());

    }

    if(mysql_affected_rows() > 0){

      die("Image inserted successfully");

    } else {

      die("Image can not be inserted check your submission");

    }

    mysql_close($con);

  } else {

    die("This is not a valid image type");

  }

}

 

 

 

?>

 

 

 

Link to comment
Share on other sites

And correct me if I am wrong but can images even be uploaded to a MYSQL db? Maybe a link to it but not the actual code of the image itself :S

You can upload the Image's Binary data into Database. But its not fair and also it will increase your work load . what you need to do is just to insert the location of that Image in MySQL Database and then Use that.
Link to comment
Share on other sites

And correct me if I am wrong but can images even be uploaded to a MYSQL db? Maybe a link to it but not the actual code of the image itself :S

You can upload the Image's Binary data into Database. But its not fair and also it will increase your work load . what you need to do is just to insert the location of that Image in MySQL Database and then Use that.

Thanks for the advice but all i want to do is upload the image's binary data. I already created a script the reverts the binary data back to a normal image. I can upload the binary data using phpmyadmin but for security reason of course I don't wana everyone being able to access that. Also I dont want people just inserting images on others servers, just incase that server deletes their image. Just too many problems i could run into. Anyone else have any advice to get all the image data into mysql?

Link to comment
Share on other sites

Look if you fear about security of your Images there is still Ways .

You can still keep secure just Keeping the Locations into Database.

You have to Keep a .htaccess file into that folder where you keep your image file

Deny from All

In the .htaccess file

Link to comment
Share on other sites

If you store the actual binary file in the database, you will take a large performance hit. It can be done, but it would be better to store the path and information about the image, and actually store the image in a directory on the server.

 

For security, to keep people from being able to access the folder directly just do what neel_basu suggested.

 

It is a lot quicker to grab a several hundred bytes containing image information and use the information to link to the directory and to the file itself, rather than trying to grab that info AND a 300-500Kb file. Unless you put constraints on how big the file you could be trying to grab database info in the MB+ range and that would put a huge load on the server if your site is fairly busy, not to mention the size of the database could be HUGE.

 

My hosting provider gives me something like 50 databases at 100MB each. That could get filled up very quickly if I were to store images and such. but storing text and numbers I can store TONS of data

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.