【问题标题】:PHP Upload image then showPHP上传图片然后显示
【发布时间】:2015-06-09 02:18:41
【问题描述】:

我正在尝试弄清楚如何允许上传图片并在上传后显示图片。我找到了关于上传图片的教程,但我不知道之后如何显示它们。我是否必须将其保存在数据库中,然后以某种方式将其拉出来?

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

<?php
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
    // Check if image file is a actual image or fake image
    if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
        if($check !== false) {
            echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
        }
    }
?>

【问题讨论】:

    标签: php


    【解决方案1】:

    我认为您会从返回上传图像信息的上传类或函数中受益。这将帮助您存储结果或按您希望的方式显示。这是一个基于您提供的符号的大致内容:

    表格:

    <form action="" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload Image" name="submit">
    </form>
    

    脚本:

    <?php
        function UploadImage($settings = false)
            {
                // Input allows you to change where your file is coming from so you can port this code easily
                $inputname      =   (isset($settings['input']) && !empty($settings['input']))? $settings['input'] : "fileToUpload";
                // Sets your document root for easy uploading reference
                $root_dir       =   (isset($settings['root']) && !empty($settings['root']))? $settings['root'] : $_SERVER['DOCUMENT_ROOT'];
                // Allows you to set a folder where your file will be dropped, good for porting elsewhere
                $target_dir     =   (isset($settings['dir']) && !empty($settings['dir']))? $settings['dir'] : "/uploads/";
                // Check the file is not empty (if you want to change the name of the file are uploading)
                if(isset($settings['filename']) && !empty($settings['filename']))
                    $filename   =   $settings['filename'];
                // Use the default upload name
                else
                    $filename   =   preg_replace('/[^a-zA-Z0-9\.\_\-]/',"",$_FILES[$inputname]["name"]);
                // If empty name, just return false and end the process
                if(empty($filename))
                    return false;
                // Check if the upload spot is a real folder
                if(!is_dir($root_dir.$target_dir))
                    // If not, create the folder recursively
                    mkdir($root_dir.$target_dir,0755,true);
                // Create a root-based upload path
                $target_file    =   $root_dir.$target_dir.$filename;
                // If the file is uploaded successfully...
                if(move_uploaded_file($_FILES[$inputname]["tmp_name"],$target_file)) {
                        // Save out all the stats of the upload
                        $stats['filename']  =   $filename;
                        $stats['fullpath']  =   $target_file;
                        $stats['localpath'] =   $target_dir.$filename;
                        $stats['filesize']  =   filesize($target_file);
                        // Return the stats
                        return $stats;
                    }
                // Return false
                return false;
            }
    ?>
    

    使用方法:

    <?php
        // Make sure the above function is included...
        // Check file is uploaded
        if(isset($_FILES["fileToUpload"]["name"]) && !empty($_FILES["fileToUpload"]["name"])) {
            // Process and return results
            $file   =   UploadImage();
            // If success, show image
            if($file != false) { ?>
                <img src="<?php echo $file['localpath']; ?>" />
            <?php
                }
        }
    ?>
    

    原始反馈:

    // This is what the array would look like on return of successful upload:
    Array
    (
        [filename] => animal.png
        [fullpath] => /data/19/2/133/150/2948313/user/2524254/htdocs/mydomain/uploads/animal.png
        [localpath] => /uploads/animal.png
        [filesize] => 35702
    )
    

    【讨论】:

      【解决方案2】:

      是的,您必须将文件的路径保存在数据库中并获取它,但对于您的用例,您可以将路径保存到 $_SESSION 变量,然后在脚本完成后立即回显路径。

      但您首先必须使用 move_uploaded_file 函数完成文件传输,否则,您将无法检索文件路径,因为它们被存储为临时文件并在脚本被解释后被删除

      http://php.net/manual/en/function.move-uploaded-file.php

      完成后,获取文件路径,使用普通的imgHTML标签

      【讨论】:

        【解决方案3】:

        永远创建&lt;img src="" widht="" height="" /&gt; 你必须将图像移动到目录路径,现在我在提交表单后从表中获取图像名称.. 并将 URL 提供给 img..example.. 你的目录名称 uploads/img 。现在您的文件名在数据库表中保存为 image01.jpg 。样本

         $img= 'select imagename from table name ';
        
        if(count($img))
        {
         <img src="<?php echo 'uploads/img/'.$img" widht="10px" height="20px" /></div>
        }
        

        【讨论】:

          【解决方案4】:

          如果您将图片上传到数据库,由于图片尺寸太大,数据加载会很慢。更好的方法是在文件夹中上传图像并将图像文件路径保存在数据库中。当您在图像标签上检索图像调用图像 Web 根目录时

          例子

          Saving Filepath Of Uploaded Image To MySQL Database

          获取图片路径

          name 是指客户端的文件名。要在服务器端获取文件名(包括完整路径),需要使用 tmp_name:

          $check = fopen($_FILES["UploadFileName"]["tmp_name"], 'r');
          

          【讨论】:

            猜你喜欢
            • 2011-10-17
            • 1970-01-01
            • 1970-01-01
            • 2018-11-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-03-13
            相关资源
            最近更新 更多