dongqin6926 2017-09-30 12:28
浏览 41

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

I'm trying to upload a file to a server then display it to the user. I'm having difficulties to display the image to the user.

If you could provide code that helps me out to display the image to the user. The code should fit in the php file right under //Display image here <---------

html file

<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 file

<?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条回答 默认 最新

  • doucu7330 2017-09-30 12:38
    关注

    first you have to change .html file to .php and note that i have renamed file as 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;
                    }
                }
    
            }
    
        ?>
    

    I have done using php but better option is using ajax call .just google it you will get man examples

    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序