problem to store and retrive image in php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Eminosoft
    New Member
    • Feb 2008
    • 6

    problem to store and retrive image in php

    The below code can't take the images.plz tell that what is the problem in that code
    [php]
    <?php

    // Connect to database

    $errmsg = "";
    if (! @mysql_connect( "localhost","ro ot","sreeni") ) {
    $errmsg = "Cannot connect to database";
    }
    @mysql_select_d b("my_db1");

    // First run ONLY - need to create table by uncommenting this
    // Or with silent @ we can let it fail every sunsequent time ;-)

    $q = <<<CREATE
    create table pix (
    pid int primary key not null auto_increment,
    name varchar(20),ema il varchar(30),cit y varchar(20),sta te varchar(20),zip int(7),country varchar(20),mon th varchar(20),dat e int(3),imgdata longblob)
    CREATE;
    @mysql_query($q );

    // Insert any new image into database
    if ($_REQUEST[completed] == 1) {
    // Need to add - check for large upload. Otherwise the code
    // will just duplicate old file ;-)
    // ALSO - note that latest.img must be public write and in a
    // live appliaction should be in another (safe!) directory.
    move_uploaded_f ile($_FILES['upload']['tmp_name'],"latest.img ");
    $instr = fopen("latest.i mg","rb");
    $image = addslashes(frea d($instr,filesi ze("latest.img" )));
    if (strlen($instr) < 149000) {
    mysql_query ("insert into pix13(name,emai l,city,state,zi p,country,month ,date,upload) values ('\"". $_POST[name] . "\",\"".$_P OST[email]. "\",\"".$_P OST[city]. "\",\"".$_P OST[state]. "\",\"".$_P OST[zip]. "\",\"".$_P OST[country]. "\",\"".$_P OST[month]. "\",\"".$_P OST[date]. "\",\"". $_POST[whatsit] . "\", \"". $image . "\")");
    } else {
    $errmsg = "Too large!";
    }
    }
    $gotten = @mysql_query("s elect * from pix order by pid desc limit 1");
    if ($row = @mysql_fetch_as soc($gotten)) {
    $name = htmlspecialchar s($row[name]);
    $email = htmlspecialchar s($row[email]);
    $city = htmlspecialchar s($row[city]);
    $state = htmlspecialchar s($row[state]);
    $zip = htmlspecialchar s($row[zip]);
    $country = htmlspecialchar s($row[country]);
    $month = htmlspecialchar s($row[month]);
    $date = htmlspecialchar s($row[date]);
    $bytes = $row[upload];
    } else {
    $errmsg = "There is no image in the database yet";
    // Put up a picture of our training centre
    $instr = fopen("latest.i mg","rb");
    $bytes = fread($instr,fi lesize("latest. img"));
    }
    // If this is the image request, send out the image

    if ($_POST[gim] == 1) {
    header("Content-type: image/jpeg/jpg/zip");
    print $bytes;
    exit ();
    }

    ?>[/php]

    Before you continue: Please enclose any code within the proper code tags. See the Posting Guidelines on how to do that. - moderator
    Last edited by ronverdonk; Mar 3 '08, 10:46 AM. Reason: code withgin tags
  • bonski
    New Member
    • Jun 2007
    • 53

    #2
    Originally posted by Eminosoft
    The below code can't take the images.plz tell that what is the problem in that code
    what exactly you want to do? upload image file and display it on a webpage?

    Comment

    • bonski
      New Member
      • Jun 2007
      • 53

      #3
      ok... i checked your script... for me.. i think you sql statement is having some errors or cant successfully insert the data.. because you lack some single quotes in your $_POST[]...

      i think this one
      [PHP]mysql_query ("insert into pix13(name,emai l,city,state,zi p,country,month ,date ,upload) values ('\"". $_POST[name] . "\",\"".$_P OST[email]. "\",\"".$_P OST[city]. "\",\"".$_P OST[state]. "\",\"".$_P OST[zip]. "\",\"".$_P OST[country]. "\",\"".$_P OST[month]. "\",\"".$_P OST[date]. "\",\"". $_POST[whatsit] . "\", \"". $image . "\")");[/PHP]


      should look like this

      [PHP]mysql_query ("INSERT INTO pix13(name, email, city, state, zip, country, month, date, upload) VALUES('".$_POS T['name']."', '".$_POST['email']."', '".$_POST['city']."', '".$_POST['state']."', '".$_POST['zip']."', '".$_POST['country']."', '".$_POST['month']."', '".$_POST['date']."', '".$_POST['whatsit']."', '".$image."')") ;[/PHP]

      and for uploading files, you can have a look at this
      file uploads

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Originally posted by eminosoft
        The below code can't take the images.plz tell that what is the problem in that code
        That is not a clear description of your problem. Please describe it in more detail and let's not make a puzzle of it.

        Ronald

        Comment

        Working...