获取上传的文件名?

时间:2017-05-06 16:04:52

标签: php

我正在尝试用户可以上传文件的PHP脚本。 我正在使用来自php.net的脚本。 它在我的localhost上成功运行。但问题是如何将上传的文件名保存到我的数据库中以保存图像路径?

代码 -

<?php

header('Content-Type: text/plain; charset=utf-8');

try {

    // Undefined | Multiple Files | $_FILES Corruption Attack
    // If this request falls under any of them, treat it invalid.
    if (
        !isset($_FILES['upfile']['error']) ||
        is_array($_FILES['upfile']['error'])
    ) {
        throw new RuntimeException('Invalid parameters.');
    }

    // Check $_FILES['upfile']['error'] value.
    switch ($_FILES['upfile']['error']) {
        case UPLOAD_ERR_OK:
            break;
        case UPLOAD_ERR_NO_FILE:
            throw new RuntimeException('No file sent.');
        case UPLOAD_ERR_INI_SIZE:
        case UPLOAD_ERR_FORM_SIZE:
            throw new RuntimeException('Exceeded filesize limit.');
        default:
            throw new RuntimeException('Unknown errors.');
    }

    // You should also check filesize here. 
    if ($_FILES['upfile']['size'] > 1000000) {
        throw new RuntimeException('Exceeded filesize limit.');
    }

    // DO NOT TRUST $_FILES['upfile']['mime'] VALUE !!
    // Check MIME Type by yourself.
    $finfo = new finfo(FILEINFO_MIME_TYPE);
    if (false === $ext = array_search(
        $finfo->file($_FILES['upfile']['tmp_name']),
        array(
            'jpg' => 'image/jpeg',
            'png' => 'image/png',
            'gif' => 'image/gif',
        ),
        true
    )) {
        throw new RuntimeException('Invalid file format.');
    }

    // You should name it uniquely.
    // DO NOT USE $_FILES['upfile']['name'] WITHOUT ANY VALIDATION !!
    // On this example, obtain safe unique name from its binary data.
    if (!move_uploaded_file(
        $_FILES['upfile']['tmp_name'],
        sprintf('./uploads/%s.%s',
            sha1_file($_FILES['upfile']['tmp_name']),
            $ext
        )
    )) {
        throw new RuntimeException('Failed to move uploaded file.');
    }

    echo 'File is uploaded successfully.';

} catch (RuntimeException $e) {

    echo $e->getMessage();

}

?>

我很困惑如何将上传的图像名称传递给变量然后将其存储到我的数据库中? 我试过了

 $path = $_FILES['upfile']['tmp_name'];
echo $path; 

但没有运气。这里的任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

要获取上传文件的信息,请使用$_FILES['upfile']['tmp_name']作为临时名称和真实姓名$_FILES['upfile']['name']

$tmp_file = $_FILES['upfile']['tmp_name']; // store temporary file name
$real_file_name = $_FILES['upfile']['name']; // store the name of the file like upload.png

例如,上传的文件为ccd37b2ce541f407cabfc58be4e4af952fce7bde.jpg

$tmp_file = $_FILES['upfile']['tmp_name']; // this is a random generated temp image name like /var/www/html/php‌​yCWSRd.jpg
$real_file_name = $_FILES['upfile']['name']; // which is ccd37b2ce541f407cabfc58be4e4af952fce7bde.jpg

将此文件移至uploads目录

$path = 'uploads/' . $real_file_name; // this will be uploads/ccd37b2ce541f407cabfc58be4e4af952fce7bde.jpg

if (!move_uploaded_file($_FILES['upfile']['tmp_name'], $path)) {
    throw new RuntimeException('Failed to move uploaded file.');
}