2017-06-28 172 views
1

所以我有這個代碼。我正在上傳圖片,並希望在用戶上傳圖片後顯示圖片。請幫我用我的代碼,這裏只是一個PHP新手。 :(顯示圖片上傳

connect.php 
<?php 
$servername = "localhost"; 
$user = "root"; 
$password = "12345"; 
$db = "demo"; 
$con = mysqli_connect($servername, $user, $password, $db); 
if (!$con) { 
    die('Could not connect: ' .mysql_error()); 
} 
?> 
try.php here 
<?php include 'connect.php'; ?> 

<?php 
if (isset($image)) { 
    $errors = array(); 
    $imageName = $_FILES['photoUpload']['name']; 
    $imageSize = $_FILES['photoUpload']['size']; 
    $imageType = $_FILES['photoUpload']['type']; 
    $imageTmp = $_FILES['photoUpload']['tmp_name']; 
    $imageExt = strtolower(end(explode('.', $imageName))); 
    $expensions = array("jpeg", "jpg", "png"); 

    if(in_array($imageExt, $expensions)===false) { 
     $errors[] = "Extensions not allowed. Only JPEG, JPG, PNG"; 
    } 
    if($imageSize > 2097152) { 
     $errors[] = "File size must not exceed 2MB"; 
    } else { print_r($errors); } 
} 
?> 

<div class="col-md-4"> 
<form action="upload.php" method="post" enctype="multipart/form-data"> 
    <input type="file" name="photoUpload"><br><br> 
    <input type="submit" name="submit" value="SUBMIT"/> 
    <ul> 
     <li>Sent File: <?php echo $imageName; ?></li> 
     <li>File Size: <?php echo $imageSize; ?></li> 
    </ul> 
</form> 
</div> 

我知道我在這裏缺少像使用echo和這樣的顯示它我也希望它顯示在高度&寬度= 200像素;和邊界半徑:50%;

+0

我相信你可以像這個例子一樣打印圖像:https://stackoverflow.com/questions/16284713/how-to-display-or-preview-a-uploaded-image-after-submitted-in-php- without-save-t –

+0

[如何顯示或預覽上傳的圖像後提交的PHP沒有保存到數據庫?](https://stackoverflow.com/questions/16284713/how-to-display-or-preview -a上傳的圖像,之後提交的,在-PHP-沒有保存-T) –

回答

0

是在保存它的名字在你的數據庫?

如果是的話,試試這個下面

$id = $_SESSION['id'];  
$sql = "SELECT image FROM user WHERE `id` = $id"; 
$result = mysqli_query($conn, $sql);            

    while ($row = mysqli_fetch_array($result)) { 

     echo "<div id='img_div'>"; 
     //img tag fetching from folder 'images' - change to match the file it was uploaded to 
     echo "<img src='images/".$row['image']."' style='width:30%; height:30%;'>"; 
     echo "</div>"; 
    } 

您需要更改查詢,以適應你的代碼,但這個想法應該讓你朝着正確的方向前進。