Jump to content

Display last uploaded image


Judit1983-2

Recommended Posts

I want to display the last uploaded image from a database. My code displays the name of the file but not the image itself. Can someone help me what is wrong with my code. Here's the code:

 

<?php

ini_set("memory_limit","128M");

ini_set("post_max_size","64M");

ini_set("upload_max-filesize","64M");

if(isset($_POST['ok']))

{

  if($_FILES['fajl']['name']!="")

  {

      if($_FILES['fajl']['type']!=="image/jpeg")

      {

        if($_FILES['fajl']['error']==0)

        {

            if(!is_dir("uploads"))

            {

              mkdir("uploads");

            }

            $filename=basename($_FILES['fajl']['name']);

            move_uploaded_file($_FILES['fajl']['tmp_name'],"uploads/".$filename);

                $location="uploads/".$filename;

            if(file_exists("uploads/".$filename))

            {

              $error="Sikeres f?jlfelt?lt?s!";

                    $con = mysql_connect("localhost","root","");

                        if (!$con)

                            {

                              die ("Kapcsol?d?s nem siker?lt a kapcsol?d?s ehhez: " . mysql_error());

                            }

                    mysql_select_db("image_uploader",$con);

                    $sql = mysql_query("INSERT INTO data (id,location) VALUES ('','$location')");

                    //$last=printf("Utolj?ra felt?lt?tt k?p ID-ja: %d\n", mysql_insert_id());

 

 

            }

            else

            {

              $error="A F?jl felt?lt?se sikertelen!";

            }

        }

        else

        {

            $error="Hiba a felt?lt?s sor?n: ".$_FILES['fajl']['error'];

        }

      }

      else

        {

            $error="Hib?s f?jlform?tum - csak JPEG megengedett";

        }

  }

  else

  {

      $error="Nem adott meg f?jlt!";

  }

}

if(isset($error))

{

  print($error);

}

echo "<br />";

echo ("Legutolj?ra felt?lt?tt f?jl:");

echo "<br />";

 

$con = mysql_connect("localhost","root","");

if (!$con)

    {

        die ("Nem siker?lt kapcsol?dni a k?vetkező adatb?zishoz:" . mysql_error());

    }

else

    {

        mysql_select_db("image_uploader",$con);

 

        $record=("SELECT location FROM data WHERE id=(SELECT max(id) FROM data)") or die ("Kiv?laszt?s nem siker?lt");

        $myData = mysql_query ($record);

    }

    while($record = mysql_fetch_assoc($myData))

    {

    $lastimage=$record['location'];

    echo $lastimage;

    echo "<br />";

 

 

}

?>

<html>

<head>

<body>

<img src=$lastimage alt="utolso kep"  height="200px" width="200px" />

<form method="post" enctype="multipart/form-data">

 

<br />

K?rem t?lts?n fel egy JPEG f?jlt.(Max 2MB)<input type="file" name="fajl" /> <br />

<input type="hidden" name="MAX_FILE_SIZE" value="2MB" />

<br />

<input type="submit" name="ok" value="Felt?lt?s" />

</form>

</body>

</head>

</html>

Link to comment
https://forums.phpfreaks.com/topic/265577-display-last-uploaded-image/
Share on other sites

$lastimage is the path to the file.

header("Content-Type: image/jpeg"); // tell the browser this is a JPEG image
header("Content-Length: " . filesize($lastimage)); // be nice and say how large the image is
readfile($lastimage); // output the file
exit; // stop the script so it doesn't accidentally do anything else

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.