-2

I have a site where users can post messages (around 400 characters) which can also include images and I need to store these massages somewhere. I was wondering what would be the best way to do this. Should I store them in the MySQL database as a TEXT file which in the case of a user adding an image would leave a pointer to where the image has been uploaded, or to fwrite and update an html file with all the messages of one person (which in the case of an image would include a <img src="path/to/image.jpg" class="message_img"/>)?

David Makogon
  • 69,407
  • 21
  • 141
  • 189
Casey Neistat
  • 81
  • 1
  • 2
  • 6
  • You should probably go through the site intro docs to get an idea what kind of questions are on topic and how to ask them so you get decent answers. https://stackoverflow.com/help/how-to-ask – pvg Apr 09 '17 at 20:13
  • 1
    This is very broad and opinion-soliciting, with no right answer. While using a database, in general, is good advice vs a text file (or html file), the choice of database engine is not as easy as "should I do it." And this question is completely open-ended. Unfortunately off-topic for StackOverflow. – David Makogon Apr 09 '17 at 20:13
  • @DavidMakogon Sorry for the question being so open ended. I don't really know how else to ask it though, you say that in general it is better to use a database, why is that? – Casey Neistat Apr 09 '17 at 20:28
  • there's something that wasn't mentioned here and that's about possible sql injections. Never trust user input; use a prepared statement. Plus, there are a few ways to go about this for files and that's by storing them in a folder rather than a BLOB. A db will quickly rise in size when using a BLOB; yet that choice is yours. – Funk Forty Niner Apr 09 '17 at 20:44
  • @Fred-ii- Oh yes, I always use prepared statements! What exactly do you mean storing them in a folder? – Casey Neistat Apr 09 '17 at 20:48
  • Your question talks about files; I assume you are storing them in a folder on the server rather than a BLOB? – Funk Forty Niner Apr 09 '17 at 20:50
  • @Fred-ii- Right. Yes. Do you recommend doing so rather than storing them in the database? – Casey Neistat Apr 09 '17 at 21:12
  • It's by preference really; both have their own pros & cons. I myself have always uploaded to a folder and used as a reference, rather than as a BLOB. Some may not share that same preference while others prefer BLOBs. It just leaves room for discussion. – Funk Forty Niner Apr 09 '17 at 21:15
  • @Fred-ii- Ok thank you! – Casey Neistat Apr 09 '17 at 21:16
  • welcome. Here's a Q&A on the subject http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay and read the comments also. – Funk Forty Niner Apr 09 '17 at 21:19

1 Answers1

-1

You can simply use VARCHAR with the maximum size allowed for a message + the number of characters for the image path, you can create a code to separate the two and store them in MySQL. Example: "message....text.... <!*&my_SpecialCode*!>/images/some-image.jpg" In your PHP code you can split the string, just make sure your special code is not allowed in the message.