Sponsor-Board.de
bilder in MySQL Speichern

+- Sponsor-Board.de (https://www.sponsor-board.de)
+-- Forum: Community (/forumdisplay.php?fid=56)
+--- Forum: Hilfe (/forumdisplay.php?fid=102)
+---- Forum: Scripting (/forumdisplay.php?fid=108)
+---- Thema: bilder in MySQL Speichern (/showthread.php?tid=59337)


bilder in MySQL Speichern - Garfield200508 - 15.03.2016 17:29

Hallo, ich habe hier mal eine frage an die Fachkundigen unter uns...

und zwar, möchte ich gerne meine Bilder in die MySQL speichern, soweit ist das ja kein Problem...

NUR werden die bilder anscheind nicht vollständig hoch geladen. von 4 Bildern wurde nur 1 bild vollständig gespeichert.

[attachment=3464]

kann sich das vlt jemand erklären und mir ggf, bei einer lösung helfen?

Bitte kommt mir nun nicht mit der Frage wieso in die MySQL Speichern... Ich denke mir dabei schon etwas, Danke

connect.php

PHP-Code:
<?php 
    
// MySQL-Verbindungsdaten
    
$hostname 'localhost';
    
$database '<base>-44d';
    
$username '<user>-44d';
    
$password '<passwd>';

    
// MYSQL-Verbindung herstellen
    
mysql_connect($hostname$username$password) or die(mysql_error());
    
mysql_select_db($database) or die(mysql_error());
    
?>


image.php

PHP-Code:
<?php
    
// MySQL-Verbindung herstellen
    
require 'connect.php';
    
    
// Bild ausgeben
    
$id $_GET['id'];
    
$result mysql_query("SELECT image, mimetype FROM images WHERE id='$id'");
    
$row mysql_fetch_object($result);
    
header("Content-type: $row->mimetype");
    echo 
$row->image;
?>


index.php

PHP-Code:
<?php
    
// MySQL-Verbindung herstellen
    
require 'connect.php';

    if(isset(
$_FILES['image'])) {
        if(
is_uploaded_file($_FILES['image']['tmp_name'])) {
            
$image $_FILES['image']['tmp_name'];
            
$data addslashes(file_get_contents($image));
            
$meta getimagesize($image);
            
$mime $meta['mime'];

            
mysql_query("INSERT INTO images VALUES('', '$data', '$mime')");
        }
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <title>MySQL-Bilder-Datenbank</title>
    </head>
    <body>
        <h1>Bild hochladen</h1>
        <form action="" method="post" enctype="multipart/form-data">
            <input name="image" type="file" />

            <input type="submit" value="hochladen" />
        </form>
        <h1>Bilderliste</h1>
        <?php
            $result 
mysql_query("SELECT id FROM images");
            while(
$row mysql_fetch_object($result)) {
                echo 
'<img alt="" src="image.php?id='.$row->id.'" width="100px" height="100px" />';
            }
        
?>
    </body>
</html> 




RE: bilder in MySQL Speichern - Aaron - 15.03.2016 18:11

Was ist "image" fürn Feldtyp in der DB?


RE: bilder in MySQL Speichern - Patrick - 15.03.2016 19:30

Hi

Kleines bisschen offtopic, aber...

Wieso möchtest Du überhaupt bilder IN die Datenbank speichern. Das ist absolut unvorteilhaft.
Die DB wird unnötig gross, langsam und gibt sicher noch mehr gute Gründe das nicht zu machen.
Speichere die Bilder in einen Ordner und hole den Link aus der DB, das macht viel mehr Sinn.

Hier ein kleines Zitat, und das war der 2te Google treffer: [Link: Registrierung erforderlich]
"Do not store images in the database. Store images in directories and store references to the images in the database. Like store the path to the image in the database or the image name in the database.

Images can get quite large 1MB >. Even if it's a small image, it's still bad practice. You're putting extra hits on your database transactions that you can completely avoid. No big websites stores images in their database. Craigslist doesn't do it. Facebook doesn't do it. It's not a good idea."