【问题标题】:php file upload guidephp文件上传指南
【发布时间】:2011-04-11 10:35:03
【问题描述】:

如何在php中上传文件并将文件名发送到数据库?

【问题讨论】:

标签: php


【解决方案1】:
<?php
 if (isset($_FILES['file'])) 
 {
    $file = $_FILES['file'];

    // File Properties
    $file_name = $file['name'];
    $file_tmp = $file['tmp_name'];
    $file_size = $file['size'];
    $file_error = $file['error'];

    // Work out the file extension
    $file_ext = explode('.', $file_name);
    $file_ext = strtolower(end($file_ext));

    $allowed = array('png', 'jpg');

    //filename
    $id = 'uploads/Test';

    if (!file_exists($id)) 
    {
        mkdir($id, 0777, true);
    }

    if (in_array($file_ext, $allowed)) {
        if ($file_error === 0) {
            if ($file_size <= 2097152) {

                $file_name_new = uniqid('', true) . '.' . $file_ext;
                $file_destination = $id .'/'. $file_name_new;

                if (move_uploaded_file($file_tmp, $file_destination)) {
                    echo $file_destination;
                }
            }
        }
    }
}

    ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 2011-09-05
    • 1970-01-01
    • 2014-07-14
    • 1970-01-01
    • 2012-03-17
    • 2021-08-14
    相关资源
    最近更新 更多