-1

Can anybody help me? I am trying to upload image to database as LONGBLOB but when i check on the database the uploaded image have only 12 B instead of 148KiB, hence it can't display!!

I wanted to upload the image on a file and store the image path on the database but i don't know how to do it, because i want to display image on a table respective to component information/details.

<?php

include "conn.php";

if(isset($_POST['submit'])) {

  $comp_id=$_POST['comp_id'];
  $compname=$_POST['compname'];
  $image=$_POST['image'];
  $description=$_POST['description'];
  $category=$_POST['category'];
  $subcat=$_POST['subcat'];
  $tech_id=$_POST['tech_id'];


  $query = mysql_query("INSERT INTO `resourcesys`.`ecomp_t`
    (`comp_id`,`compname`,`image`,`description`,`category`,`subcat`,`tech_id`)
    VALUES ('$comp_id','$compname','$image','$description','$category','$subcat','$tech_id')")
    or die(mysql_error());

  echo "<script>alert('Your component has been added successfully !!');</script>";

}
Linus Unnebäck
  • 23,234
  • 15
  • 74
  • 89
  • Welcome to StackOverflow. Do you have any code that you can share with us? – David Wyly May 18 '15 at 23:02
  • Not an answer to the question, but I'd recommend not storing images themselves inside a database, why not keep the files on the filesystem and just reference their locations in the db. – Jonnix May 18 '15 at 23:02
  • 2
    @JonStirling Any particular reason? – Strawberry May 18 '15 at 23:11
  • To note: One of the links out of the page that David Wyly links to in his comment below suggests that objects larger than 1M are best stored in the filesystem. – Strawberry May 18 '15 at 23:26
  • It would be good you tell us the OS (and age) in use as it would limit the amount of discussions needed. Different OS'ses deal with files in different ways (e.g. on UNIX based OS'ses everything is a file) and different files systems (as in NTFS, ext3, ext4) can handle files in different ways. – Jobst May 19 '15 at 00:11
  • Hi @Eng.SHALI, [you can refer to this link](http://stackoverflow.com/questions/18737462/how-to-upload-image-php-and-insert-path-in-mysql/18737955#18737955) as your reference. – Mark May 19 '15 at 00:16

1 Answers1

1

Without any code posted, it's hard to debug your specific use-case.

As an alternative (and, in my opinion, better) answer to your original question, I opine that, depending on what you're trying to do with your application, storing images may not be the best idea. There's far lower overhead that is invoked when you manipulate files using the local filesystem.

There are some use-cases for what you're trying to do (such as if you needed transactional integrity, or if you're consistently using <250kB image files). There are serious pros and cons either way.

Here are some good write-ups on the debate:

Storing Images in DB - Yea or Nay?

Is it a bad practice to store large files (10 MB) in a database?

Instead, I would use a more traditional method, such as using a form to collect user input, validating for security purposes, and posting the file to the server.

Link to the PHP documentation

Link to a step-by-step walkthrough

Community
  • 1
  • 1
David Wyly
  • 1,671
  • 1
  • 11
  • 19
  • I removed my the 'factor of 100' claim as I couldn't really substantiate it. I believed that I read it somewhere, at some time, but I must have been mistaken. – David Wyly May 18 '15 at 23:30
  • Still, the link you provided did at least broadly support your claim – Strawberry May 18 '15 at 23:32
  • Cheers. Here's a pretty interesting research article that looks at storing files in the database versus the file server, using Microsoft SQL server: http://research.microsoft.com/apps/pubs/?id=64525 – David Wyly May 18 '15 at 23:35
  • Although if the op's images are consistently under 250k, then the DB may in fact be a better option (performance wise at least). – Strawberry May 18 '15 at 23:39
  • I'm not sure how true this is between various filesystems (in the research articles case, NTFS) and the various versions of SQL. Additionally, that article is primary concerned about fragmentation. I would like to know more about memory usage as well. I would be interested in getting more hard data on this issue. – David Wyly May 18 '15 at 23:42
  • well i have pass through your advice but i am a project and my aim is to store electronics component information such as transistor, pic, timers etc, so i have created a form for adding components information to the database and i want them to be displayed in a table with the image on right side column. – Eng. SHALI May 19 '15 at 05:55