【问题标题】:Upload pic from php form从php表单上传图片
【发布时间】:2018-12-06 02:53:08
【问题描述】:

我将如何从 php 表单上传照片,然后通过 POST 方法在下一页打印该照片。我也想将该照片保存在 mysql 数据库中。

这是我的 html 代码

<form enctype="multipart/form-data" action="form.php" method="POST">
  <h3><input name="fileupload" type="file" id="fileToUpload" />
  </h3>
<input type="submit" name ='submit' value="Submit" />

【问题讨论】:

标签: php


【解决方案1】:

HTML 表单

<form enctype="multipart/form-data" action="./action.php" 
       method="POST">
     <h3><input name="fileupload" type="file" id="fileToUpload" /></h3>
     <input type="submit" name ='submit' value="Submit" />
</form>

上传图片的PHP脚本

action.php

if(isset($_FILES)) {
 $file   =   UploadImage();

    // If success, show image
    if($file != false) { ?>

        <img src="<?php echo $file['localpath']; ?>" />
    <?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'] : "fileupload";
            // 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;
        }

输出

// This is what the array  on the return of successful upload:
Array
(
    [filename] => bear.png
    [fullpath] => /opt/lampp/htdocs/uploads/bear.png
    [localpath] => /uploads/bear.png
    [filesize] => 69631
)

上传成功后你会得到$file['filename']的文件名和$file['fullpath']的完整路径。您可以将文件名或文件路径保存在 db 表中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-03
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多