Tell me how you can realize that if the user attached a picture to the message, then it would be shown, if not, then no. What should be the structure of the database and the query output, if it is and if it does not exist, or throw the tutorial really want to understand. Tell me plz!

  • Why give permission to insert pictures? What if a user downloads a picture that steals sessions, passwords, etc. from hacker sites - Riolu
  • This means a picture that is downloaded from the user's computer and not from a third-party site! - OverLoader
  • @LovelyCat, save the path to the loaded image in the database along with the message. - Deonis
  • Well, you can try using php + mysql, the user will create an album where he will upload the pictures and when uploading the picture the name of the picture will be added to the database and to whom the picture belongs and give permission to insert the bb code - Riolu

3 answers 3

A simple option , when there can be only one picture in a message - add the "picture" field in which to store the local address of the picture, the string VARCHAR(255) eg. "/uploads/2012/trollface.jpg" . The line is empty - no pictures, nothing to show.

Slightly more complicated if a picture to one message can be several. It will take extra. table:

 id id_сообщения путь_к_картинке 1 1258 /uploads/2012/08/12/3f5ha2ff.jpg 2 1258 /uploads/2012/08/12/facepalm.jpg 

You have to do two requests:

  1. receive the message itself;
  2. find images by the "id_сообщения" : 0, 1 or several lines will be returned.

Further to understand PHP - if the empty list returned, there are no pictures, otherwise it is necessary to show them somehow.

  • I can't help but grumble, sorry :) The structure of the table 1) is potentially buggy (it is not always possible to store files with Chinese names; when processing files with non-ascii characters, spaces, etc., additional efforts are needed, etc.) 2) is redundant. Problem 1 and part 2 is solved as follows: we have a primary key, so let it be the file name. For over 9000 cases, the name does not matter, for the rest of the exotic, you can create a "comment" or "original name." Redundancy and that way is stored. We don’t need to keep the name, ergo, instead of [var] char, a fairly simple date representation :) - user6550

Like that

 <?php $asel = mysql_query("select * from `images` where `user`='" . $_SESSION['user'] . "' ORDER BY `id` DESC") or die("Ошибка!"); while ($arm = mysql_fetch_array($asel)) { $text_msg = str_replace("[" . $arm[name] . "]", "<img src=\"/upload/" . $arm[name] . ".png\">", $text_msg); } ?> 

    .

      <? $uploaddir = 'uploads/'; $uploadfile = $uploaddir . basename($_FILES['userfile']['name']); if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { echo "File is valid, and was successfully uploaded.\n"; } else { echo "File uploading failed.\n"; } ?> <form name="upload" method="POST" ENCTYPE="multipart/form-data"> <input type="file" name="userfile"> <input type="submit" name="upload" value="Загрузить..."> </form> 
    • Thank you, I'm not a little interested in adding the picture to the user's message - OverLoader