2017-07-30 1 views
-1

Ich habe dieses Programm, wo a in seinen/ihren Namen, den Namen des Bildes setzt, und wählt das Bild aus einer Datei. Alles funktioniert perfekt, aber das Bild. Es wird nicht hochgeladen und auch nicht in den richtigen Ordner verschoben. Hier ist der Code für den Benutzer sein/ihr Bild hochladen:PHP-Bild nicht angezeigt

<!DOCTYPE html> 
<html> 
<head> 
<title>Upload pic to our site</title> 
</head> 
<body> 
<form name="form1" method="post" action="check_image.php" 
enctype="multipart/form-data"> 
    <table border="0" cellpadding="5"> 
     <tr> 
      <td>Image Title or Caption<br> 
       <em>Example: You talkin' to me?</em></td> 
      <td><input type="text" name="image_caption" 
id="item_caption" size="55" maxlength="255"></td> 
     </tr> 
     <tr> 
      <td>Your Username</td> 
      <td><input type="text" name="image_username" 
id="image_username" size="15" max="255"></td> 
     </tr> 
     <tr> 
      <td>Upload Image:</td> 
      <td><input type="file" name="image_filename" 
id="image_filename"></td> 
     </tr> 
    </table> 
    <br> 
    <em>Acceptable image formats include: GIF, JPG, JPEG, PNG.</em> 
    <p align="center"><input type="submit" name="Submit" 
value="Submit"> 
    &nbsp; 
    <input type="submit" name="Submit2" value="Clear Form"> 
    </p> 
</form> 
</body> 
</html> 

Hier ist der Code, um das Bild zu zeigen:

<?php 
//connect to database 
$link = mysqli_connect("localhost", "root", "", "moviesite"); 
if (!$link) { 
    "Connection lost: " . mysqli_connect_error(); 
} 

//make variables available 
$image_caption = $_POST['image_caption']; 
$image_username = $_POST['image_username']; 
$image_tempname = $_FILES['image_filename']['name']; 
$today = date("Y-m-d"); 

//upload image and check for image type 
$ImageDir = "/Applications/XAMPP/xamppfiles/htdocs/chapter7/images"; 
$ImageName = $ImageDir . $image_tempname; 

if (move_uploaded_file($_FILES['image_filename']['tmp_name'], 
$ImageName)) { 
    //get info about the image being uploaded 
    list($width, $height, $type, $attr) = getimagesize($ImageName); 

    switch ($type) { 
     case '1': 
      $ext = ".gif"; 
      break; 
     case '2': 
      $ext = ".jpg"; 
      break; 
     case '3': 
      $ext = ".png"; 
      break; 

     default: 
      echo "Sorry, but the file you uploaded was not a GIF, JPG, 
or PNG."; 
      echo "Please hit your browser's 'back' button and try 
again."; 
      break; 
    } 

    //insert info into images 
    $insert = "INSERT INTO image 
       (`image_caption`, `image_username`, `image_date`) 
       VALUES 
       ('$image_caption', '$image_username', '$today')"; 
    $insertresults = mysqli_query($link, $insert); 

    $lastpicid = mysqli_insert_id($link); 

    $newfilename = $ImageDir . $lastpicid . $ext; 

    rename($ImageName, $newfilename); 
} 
?> 
<!DOCTYPE html> 
<html> 
<head> 
<title>Here is your pic</title> 
</head> 
<body> 
<h1>So how does it feel to be famous?</h1><br><br> 
<p>Here is your picture you just uploaded to our servers:</p> 
<img src="<?php echo $ImageDir . $lastpicid . $ext; ?>" align="left"> 
<strong><?php echo $image_username; ?></strong> 
This image is a <?php echo $ext; ?>image.<br> 
It is <?php echo $width; ?> pixels wide 
and <?php echo $height; ?> pixels high.<br> 
It was uploaded <?php echo $today; ?> 
</body> 
</html> 

Das Bild in die „Kapitel 7“ Ordner gespeichert wird. Ich möchte, dass es im Ordner "images" gespeichert wird. Das Bild wird aus dem Ordner "image" ausgewählt. So sieht die Seite aus: Image not showing on php page

Wenn jemand eine Lösung finden kann, würde mir das sehr helfen! Vielen Dank!

+0

'$ ImageDir' ändern muss eine hintere – Scuzzy

+1

slash Ändere '$ ImageDir' zu '$ ImageDir ="/Programme/XAMPP/xamppfiles/htdocs/chapter7/images/";' –

Antwort

0

In HTML Img-Tag der src Attributwert können nur Dateien über den Webbrowser verfügbar sein.

In Ihrem Code setzen Sie $ImageDir = /Applications/XAMPP/xamppfiles/htdocs/chapter7/images so Browser kann nicht zugreifen Verzeichnis und seine Dateien von dieser Adresse, weil /Application/XAMPP... ist nicht zugänglich für den Browser.

Wenn Sie das Bild anzeigen möchten Sie in src-Attribut des img-Tag diesen Weg echo müssen: echo "chapter7/images/" . $lastpicid . $ext;

und auch

$ImageDir to $ImageDir = "/Applications/XAMPP/xamppfiles/htdocs/chapter7/images/";

+0

Ich habe nicht verstanden was du meinst. Es wird von Ihnen gemacht, dass das Bild nach dem Hochladen in die ID des aus der Datenbankabfrage zurückgegebenen Bildes umbenannt werden sollte. –

+0

Vergiss was ich sagte, vielen Dank, es hat funktioniert! –