PHP - 上传照片时更改文件名目的地

时间:2015-10-07 03:45:41

标签: php file-upload

美好的一天,我正在开发一个用户可以上传照片的网络应用程序。

我已经有了一个PHP代码,我可以将照片上传到服务器目录中的文件夹。但是,我对我的代码的主要问题是它在上传之前和上传之后保留了照片的原始文件名。我的问题是当有两张不同的照片有相同的文件名时,我上传时会有冲突。

这是我的代码:

<?php

error_reporting(-1);
ini_set('display_errors', 'On');


require_once '_common.php';
$config = require_once '_config.php';
$fileName = '';
$mimeType = '';
$fileSize = 0;

if (empty($_FILES)) {
    _error('No file received');
}

$pathArray = array();

foreach ($_FILES as $fileName => $fileData) {

    if (!isset($fileData['error']) ||
        is_array($fileData['error'])) {
        die(json_encode(array(
            'success' => false,
            'status' => "Invalid Parameters.",
            'files' => $_FILES 
        )));
    }

    switch ($fileData['error']) {
        case UPLOAD_ERR_OK:
            break;
        case UPLOAD_ERR_NO_FILE:

            die(json_encode(array(
                'success' => false,
                'status' => "No file sent.",
                'files' => $_FILES)));

        case UPLOAD_ERR_INI_SIZE:
        case UPLOAD_ERR_FORM_SIZE:
            die(json_encode(array(
                'success' => false,
                'status' => "Exceeded filesize limit.",
                'files' => $_FILES)));

        default:
            die(json_encode(array(
                'success' => false,
                'status' => "Unknown errors.",
                'files' => $_FILES)));
    }

    // You should also check filesize here. 
    if ($fileData['size'] > 1000000) {
            die(json_encode(array(
                'success' => false,
                'status' => "Exceeded File Size Limit.",
                'files' => $_FILES)));
    }

    $finfo = new finfo(FILEINFO_MIME_TYPE); 
    if (false === $ext = array_search(
        $finfo->file($fileData['tmp_name']),
        array(
            'jpg' => 'image/jpeg',
            'png' => 'image/png',
            'gif' => 'image/gif',), 
        true)) {
        die(json_encode(array(
            'success' => false,
            'status' => "Invalid file format.",
            'files' => $_FILES)));
    }

    if (!move_uploaded_file(
        $fileData['tmp_name'],
        sprintf('./gallery/%s',
        basename($fileData['name'])) )) {

        die(json_encode(array(
            'success' => false,
            'status' => "Failed to move uploaded file.",
            'files' => $_FILES)));
    }

    $uploaddir = '/gallery/';
    $name = $fileData["tmp_name"];

    $fullPath = sprintf('gallery/%s.%s',
        basename($fileData['tmp_name']),
        $ext

    array_push($pathArray, $fullPath);

}

print_r(json_encode(array(
    'success' => true,
    'status' => "File is uploaded successfully.",
    'filePath' => "$fullPath"
)));
?>

我在PHP中失去了联系,但是,这是我尝试过的:

if (!move_uploaded_file(
    $fileData['tmp_name'],
    sprintf('gallery/%s.%s',
    sha1_file($_FILES['fileToUpload']['tmp_name'],
    $ext) 

    )) {

    die(json_encode(array(
        'success' => false,
        'status' => "Failed to move uploaded file.",
        'files' => $_FILES)));
}

如果这样,我改变$fullPath以适应我想要的新目的地:

$fullPath = sprintf('gallery/%s.%s',
    sha1_file($_FILES['fileToUpload']['tmp_name'],
    $ext);

但是,这些似乎都不起作用。

我的目标是为上传的文件提供或多或少的唯一文件名,但是,我似乎无法让sha1_file工作。

如果有人可以指导我如何使sha1_file工作或以其他方式更改目标文件名,我将以一个独特的文件名(例如使用日期和时间)结束,这将是非常受欢迎的。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可以使用microtime()或uniqid()函数与您的文件名连接:

if (!move_uploaded_file(
    $fileData['tmp_name'],
    sprintf('gallery/%s.%s',
    uniqid() . '_' . $_FILES['fileToUpload']['tmp_name'],
    $ext) 

    )) {

    die(json_encode(array(
        'success' => false,
        'status' => "Failed to move uploaded file.",
        'files' => $_FILES)));
}

有关uniqid的详细信息,请参阅uniqid文档。