[bglug] PHP visualizzare immagini

Asperti Paolo paolo@asperti.com
Mer 7 Nov 2007 11:32:25 CET


Il giorno 07/nov/07, alle ore 11:24, renato ha scritto:

> Saresti cosė gentile da "postare" tutto il codice da te testato ?

---- config.php ----
<?php

$db_host="localhost";
$db_name="immagini";
$db_user="user";
$db_pass="password";

mysql_pconnect($db_host,$db_user,$db_pass) || die("no connect");
mysql_select_db($db_name) || die("no select");

---- image.php ----
<?php

require_once("config.php");

if (isset($_GET['id']))
{
$id = @intval($_GET['id']);
$sql = "SELECT id,type,immagine FROM immagini WHERE id='$id'";
$result = @mysql_query($sql) or die(mysql_error ());
$row = @mysql_fetch_array($result);
$id_img = $row['id'];
$type = $row['type'];
$img = $row['immagine'];
if (!$id_img)
{
echo "Id sconosciuto";
}else{
@header ("Content-type:image/jpeg ".$type);
echo $img ;
}
}else{
echo "Impossibile soddisfare la richiesta.";
}


---- upload.html ----

     <!DOCTYPE HTML PUBLIC
                "-//W3C//DTD HTML 4.0 Transitional//EN"
                "http://www.w3.org/TR/html4/loose.dtd">
     <html>
     <head>
       <title>Upload an Image File</title>
     </head>
     <body bgcolor="white">
     <form method="post" action="upload.php" enctype="multipart/form- 
data">
     <h1>Upload an Image File</h1>
     <h3>Please fill in the details below to upload your file.
     Fields shown in <font color="red">red</font> are mandatory.</h3>
     <table>
     <col span="1" align="right">

     <tr>
        <td><font color="red">File:</font></td>
        <td><input name="file" type="file"></td>
     </tr>

     <tr>
           <td><input type="submit" value="Submit"></td>
     </tr>
     </table>

     </form>
     <h3>Click <a href="index.php">here</a> to browse the images  
instead.</h3>
     </body>
     </html>


---- upload.php ----

<?php

require_once("config.php");

function upload()
{
   $result = false;
   $immagine = '';
   $size = 0;
   $type = '';
   $nome = '';
   $max_size = 300000;
   $result = @is_uploaded_file($_FILES['file']['tmp_name']);
   if (!$result)
   {
     echo "Impossibile eseguire l'upload.";
     return false;
   }else{
     $size = $_FILES['file']['size'];
     if ($size > $max_size)
     {
       echo "Il file č troppo grande.";
       return false;
     }
     $type = $_FILES['file']['type'];
     $nome = $_FILES['file']['name'];
     $immagine = @file_get_contents($_FILES['file']['tmp_name']);
     $immagine = addslashes ($immagine);
     $sql = "INSERT INTO immagini (nome, size, type, immagine) VALUES
('$nome','$size','$type','$immagine')";
     $result = @mysql_query ($sql) or die (mysql_error());
     return true;
   }
}

if (upload()) echo "ok";

---- database ----


CREATE TABLE `immagini` (
    `id` int(10) unsigned NOT NULL auto_increment,
    `nome` varchar(30) NOT NULL,
    `size` bigint(20) NOT NULL,
    `type` varchar(50) NOT NULL,
    `immagine` longblob NOT NULL,
    PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 ;





http://localhost/upload.html
per uppare l'immagine

http://localhost/image.php?id=1
http://localhost/image.php?id=2
http://localhost/image.php?id=3
ecc....
per visualizzarle

spartano, ma č solo per test....






--
Asperti Paolo
paolo@asperti.com





Maggiori informazioni sulla lista bglug