PHP上传图片然后显示

时间:2015-06-09 02:18:42

标签: php

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

<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;
        }
    }
?>

4 个答案:

答案 0 :(得分:2)

我认为您可以从上传类或函数中获益,该类或函数会返回您上传图片的信息。这将帮助您存储结果或显示您想要的。这是一个松散的基于你提供的符号:

<强>形式:

<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
            }
    }
?>

RAW反馈:

// 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
)

答案 1 :(得分:0)

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

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

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

完成此操作后,您将获取该文件的路径并使用正常的img HTML标记

答案 2 :(得分:0)

永远创建<img src="" widht="" height="" />你必须将图像移动到目录路径,现在我在提交表单后从表中获取图像名称..并给出了img..example的url ..你的目录名称上传/ img 。现在你的文件名保存在数据库表中,如image01.jpg。样品

 $img= 'select imagename from table name ';

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

答案 3 :(得分:0)

如果您在数据库上传图像,数据加载会很慢,因为图像siz太大。更好的方法是在文件夹中上传图片&amp;在图像标记

上检索图像调用图像Web根时,在数据库中保存图像文件路径

例如

Saving Filepath Of Uploaded Image To MySQL Database

获取图像路径

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

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