php - Images not displaying from database and file -


i have set site lets user upload images images displayed on home screen. hit wall got past letting user upload image images gets saved folder , database how can image gets displayed on home screen.

 <?php   // connect database   $errmsg = "";  if (! @mysql_connect("localhost","alfred1000351","*******")) {  $errmsg = "cannot connect database";      }  @mysql_select_db("drp_2cgih5o233");     $q = <<<create  create table pix (  pid int primary key not null auto_increment,  title text,  imgdata longblob)  create;  @mysql_query($q);   // insert new image database    if ($_request[completed] == 1) {   // need add - check large upload. otherwise code   // duplicate old file ;-)  // - note latest.img must public write , in  // live appliaction should in (safe!) directory.  move_uploaded_file($_files['imagefile']['tmp_name'],"latest.img");  $instr = fopen("latest.img","rb");  $image = addslashes(fread($instr,filesize("latest.img")));  if (strlen($instr) < 149000) {  mysql_query ("insert pix (title, imgdata) values (\"".  $_request[whatsit].  "\", \"".  $image.  "\")");  } else {  $errmsg = "too large!";  }  }   // find out latest image    $gotten = @mysql_query("select * pix order pid desc limit 1");   if ($row = @mysql_fetch_assoc($gotten)) {   $title = htmlspecialchars($row[title]);   $bytes = $row[imgdata];   } else {   $errmsg = "there no image in database yet";   $title = "no database image available";   // put picture of our training centre   $instr = fopen("../wellimg/ctco.jpg","rb");    $bytes = fread($instr,filesize("../wellimg/ctco.jpg"));   }   // if image request, send out image  if ($_request[gim] == 1) {  header("content-type: image/jpeg"); print $bytes; exit (); } ?>  <html><head> <title>upload image database</title> <body bgcolor=white><h2>here's latest picture</h2> <font color=red><?= $errmsg ?></font> <center><img src=?gim=1 width=144><br> <b><?= $title ?></center> <hr> <h2>please upload new picture , title</h2> <form enctype=multipart/form-data method=post> <input type=hidden name=max_file_size value=150000> <input type=hidden name=completed value=1> please choose image upload: <input type=file name=imagefile><br> please enter title of picture: <input name=whatsit><br> then: <input type=submit></form><br> <hr>  </body> </html> 

here toughts:

  1. do not suppress warnings db. real, , needed. remove @-characters front of db-calls. if notices, warnings, errors not suppress them, correct them.

  2. if making new code, consider using pdo db api, not old, deprecating php mysql api. it's easy mysql api use.

  3. you want try create table once, remove code executed many times.

  4. you should check if $_request parameters exists, , not compare them if not. need put parameter names in quotes, otherwise php thinks using constants, not. line if ($_request[completed] == 1) { must fixed if(isset($_request['completed']) && $_request['completed'] == 1) {. same appies whatisit , gim -params.

  5. the code if (strlen($instr) < 149000) { not work intented, cannot length of resource. looking functionality: if (strlen($_files['imagefile']['size']) < 149000) {.

  6. same (as in step 4) $row s need put literas in quotes, fix lines as: $title = htmlspecialchars($row['title']); $bytes = $row['imgdata'];

  7. othewise it should work. however, contains db-security hole, can lead compromize website, recommend not put real site. own/friends fun.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -