上传图片并将其显示给用户

时间:2017-09-30 12:28:34

标签: php html file upload

我正在尝试将文件上传到服务器,然后将其显示给用户。我很难向用户显示图像。

如果您可以提供帮助我向用户显示图像的代码。代码应该放在//显示图像这里< ---------

下面的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;
                }
            }

        }

    ?>

3 个答案:

答案 0 :(得分: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调用。只是google它你会得到男人的例子

答案 1 :(得分:0)

为了能够显示图像,请执行以下操作:

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

将$ file_location替换为包含文件位置的变量

答案 2 :(得分:0)

 <!-- index.php -->  

<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file2upload">
    <input type="submit" value="upload">
    </form>

<!-- sorry i didn't got php code section here , so i m posting php file code here -->
<!-- upload.php file -->
<?php 

// target directory where files goes after uploading 
$target_dir = "uploads/pictures/";

// target files name and extension save to this target_file variable
// $target_file specifies the path of the file to be uploaded
$target_file = $target_dir . basename($_FILES["file2upload"]["name"]);
$uploadOk = 1;

// checking that target_file is been uploading is a true image file extenion or not
// $audioFileType holds the file extension of the file (in lower case)
$picFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
echo $picFileType."<br />";

if(isset($_POST["submit"])) {
 // The filesize() function in PHP is an inbuilt function which is used to return the size of a specified file. 
// The filesize() function accepts the filename as a parameter and returns the size of a file in bytes on success and False on failure.   
    $check = getimagesize($_FILES["file2upload"]["tmp_name"]); 
    
   
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// step:2 
//  Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
    
// step: 3
// Check file size
if ($_FILES["file2upload"]["size"] > 5000000) {  // 5MB manual set
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}   
    
// step: 4
// Allow certain file formats   
  if($picFileType != "jpeg" && $picFileType != "jpg" && $picFileType != "bmp" && $picFileType != "gif" && $picFileType != "png" ) {
   $uploadOk = 0;   
    echo "<b style='color:red;'> File to be uploading is not have image formates like : .jpeg,.jpg,.bmp, .gif, .png etc  </b><br />";  
  }
      
// step: 5
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    
    if (move_uploaded_file($_FILES["file2upload"]["tmp_name"], $target_file)) 
    {
        echo "The file ". basename( $_FILES["file2upload"]["name"]). " has been uploaded. <br />";
        
        <!-- this is the ANSWER -->
         <!-- ----------------------------- -->
         
         
       echo $file_name = basename( $_FILES["file2upload"]["name"]);
       echo $file_size =  $_FILES["file2upload"]["size"];
       echo $fileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
        
        <!-- ----------------------------- -->
        
    } 
    else {
        echo "Sorry, there was an error uploading your file.";
    }
}   
       
    
    
    
 // }else{ echo "<b style='color:red;'> form have diffrent method ( post/get )  </b><br />"; $uploadOk = 0; }

?>