2017-09-30 26 views
-1

我試圖將文件上傳到服務器,然後將其顯示給用戶。我很難將圖像顯示給用戶。上傳圖像並將其顯示給用戶

如果您可以提供幫助我將圖像顯示給用戶的代碼。該代碼應適合在PHP文件有權根據這裏//顯示圖像< ---------

HTML文件

<html> 
     <body> 
      <form method="post" enctype="multipart/form-data" action="server.php"> 
      <input type="file" name="fileToUpload" id="fileToUpload" size="35"> 
      <br> 
      <br> 
      <input type="submit" value="Upload" name="upload"> 
     </body> 
    </html> 

php文件

<?php 

     if(isset($_FILES["fileToUpload"])){ 
      $file = $_FILES['fileToUpload']; 

      $fileName = $_FILES["fileToUpload"]["name"]; 
      $fileTmpName = $_FILES["fileToUpload"]["tmp_name"]; 
      $fileSize = $_FILES["fileToUpload"]["size"]; 
      $fileError = $_FILES["fileToUpload"]["error"]; 
      $fileType = $_FILES["fileToUpload"]["type"]; 

      $fileExt = explode('.', $fileName); 
      $fileActualExt = strtolower(end($fileExt)); 

      $allowed = array('jpg', 'jpeg', 'png'); 

      if(in_array($fileActualExt, $allowed)){ 
       //Image code 
       if($fileError === 0){ 
        if($fileSize < 500000){ 

         $fileDestination = 'uploads/'.$fileName; 
         move_uploaded_file($fileTmpName, $fileDestination); 
         header("Location: server.php?uploadsuccess"); 
         //Display image here <---------- 

        }else{ 
         echo "Your file is too big!"; 
        } 
       }else{ 
        echo "There was an error while uploading your file!"; 
       } 
      }else{ 

       if(isset($_FILES["fileToUpload"])){ 
        $file = $_FILES["fileToUpload"]["name"]; 
        echo "File: ".$file; 
       } 
      } 

     } 

    ?> 

回答

1

首先你得將.html文件更改爲.php並注意我已將文件重命名爲index.php

<html> 
     <body> 
    <?php if(isset($_GET['filename'])){ ?> 
     <img src="<?php echo $_GET['filename']; ?>" /> 
<?php } ?> 
      <form method="post" enctype="multipart/form-data" action="server.php"> 
      <input type="file" name="fileToUpload" id="fileToUpload" size="35"> 
      <br> 

      <br> 
      <input type="submit" value="Upload" name="upload"> 
     </body> 
    </html> 

server.php

<?php 

     if(isset($_FILES["fileToUpload"])){ 
      $file = $_FILES['fileToUpload']; 

      $fileName = $_FILES["fileToUpload"]["name"]; 
      $fileTmpName = $_FILES["fileToUpload"]["tmp_name"]; 
      $fileSize = $_FILES["fileToUpload"]["size"]; 
      $fileError = $_FILES["fileToUpload"]["error"]; 
      $fileType = $_FILES["fileToUpload"]["type"]; 

      $fileExt = explode('.', $fileName); 
      $fileActualExt = strtolower(end($fileExt)); 

      $allowed = array('jpg', 'jpeg', 'png'); 

      if(in_array($fileActualExt, $allowed)){ 
       //Image code 
       if($fileError === 0){ 
        if($fileSize < 500000){ 

         $fileDestination = 'uploads/'.$fileName; 


         move_uploaded_file($fileTmpName, $fileDestination); 
        // header("Location: server.php?uploadsuccess"); 
         //Display image here <---------- 
header("Location:index.php?filename=$fileDestination"); 

        }else{ 
         echo "Your file is too big!"; 
        } 
       }else{ 
        echo "There was an error while uploading your file!"; 
       } 
      }else{ 

       if(isset($_FILES["fileToUpload"])){ 
        $file = $_FILES["fileToUpload"]["name"]; 
        echo "File: ".$file; 
       } 
      } 

     } 

    ?> 

使用PHP,但更好的方法是使用Ajax調用。只是谷歌它,你會得到男人的例子

0

爲了能夠顯示圖像,爲此,我做了:

//Display image here <---------- 
echo "<img src='" . $fileLocation . "'>"; 

用包含文件位置的變量替換$ file_location

相關問題