当前位置: 首页>>代码示例>>PHP>>正文


PHP UploadedFile::get_sha1方法代码示例

本文整理汇总了PHP中UploadedFile::get_sha1方法的典型用法代码示例。如果您正苦于以下问题:PHP UploadedFile::get_sha1方法的具体用法?PHP UploadedFile::get_sha1怎么用?PHP UploadedFile::get_sha1使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在UploadedFile的用法示例。


在下文中一共展示了UploadedFile::get_sha1方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: upload_file

/**
 * Handles the uploading and db entry for a file
 *
 * @param  UploadedFile $file
 * @return array
 */
function upload_file($file)
{
    global $db;
    // Handle file errors
    if ($file->error) {
        throw new UploadException($file->error);
    }
    // Check if a file with the same hash and size (a file which is the same) does already exist in
    // the database; if it does, delete the file just uploaded and return the proper link and data.
    $q = $db->prepare('SELECT filename, COUNT(*) AS count FROM files WHERE hash = (:hash) ' . 'AND size = (:size)');
    $q->bindValue(':hash', $file->get_sha1(), PDO::PARAM_STR);
    $q->bindValue(':size', $file->size, PDO::PARAM_INT);
    $q->execute();
    $result = $q->fetch();
    if ($result['count'] > 0) {
        unlink($file->tempfile);
        return array('hash' => $file->get_sha1(), 'name' => $file->name, 'url' => POMF_URL . $result['filename'], 'size' => $file->size);
    }
    // Generate a name for the file
    $newname = generate_name($file);
    // Attempt to move it to the static directory
    if (move_uploaded_file($file->tempfile, POMF_FILES_ROOT . $newname)) {
        // Need to change permissions for the new file to make it world readable
        if (chmod(POMF_FILES_ROOT . $newname, 0644)) {
            // Add it to the database
            if (empty($_SESSION['id'])) {
                // Query if user is NOT logged in
                $q = $db->prepare('INSERT INTO files (hash, originalname, filename, size, date, ' . 'expire, delid) VALUES (:hash, :orig, :name, :size, :date, ' . ':exp, :del)');
            } else {
                // Query if user is logged in (insert user id together with other data)
                $q = $db->prepare('INSERT INTO files (hash, originalname, filename, size, date, ' . 'expire, delid, user) VALUES (:hash, :orig, :name, :size, ' . ':date, :expires, :delid, :user)');
                $q->bindValue(':user', $_SESSION['id'], PDO::PARAM_INT);
            }
            // Common parameters binding
            $q->bindValue(':hash', $file->get_sha1(), PDO::PARAM_STR);
            $q->bindValue(':orig', strip_tags($file->name), PDO::PARAM_STR);
            $q->bindValue(':name', $newname, PDO::PARAM_STR);
            $q->bindValue(':size', $file->size, PDO::PARAM_INT);
            $q->bindValue(':date', date('Y-m-d'), PDO::PARAM_STR);
            $q->bindValue(':exp', null, PDO::PARAM_STR);
            $q->bindValue(':del', sha1($file->tempfile), PDO::PARAM_STR);
            $q->execute();
            return array('hash' => $file->get_sha1(), 'name' => $file->name, 'url' => POMF_URL . $newname, 'size' => $file->size);
        } else {
            throw new Exception('Failed to change file permissions', 500);
        }
    } else {
        throw new Exception('Failed to move file to destination', 500);
    }
}
开发者ID:LolcatsV2,项目名称:Pomf,代码行数:56,代码来源:upload.php

示例2: upload_file

/**
 * Handles the uploading and db entry for a file.
 *
 * @param UploadedFile $file
 *
 * @return array
 */
function upload_file($file)
{
    global $db;
    // Handle file errors
    if ($file->error) {
        throw new UploadException($file->error);
    }
    // Check if a file with the same hash and size (a file which is the same) does already exist in
    // the database; if it does, delete the file just uploaded and return the proper link and data.
    $q = $db->prepare('SELECT filename, COUNT(*) AS count FROM files WHERE hash = (:hash) ' . 'AND size = (:size)');
    $q->bindValue(':hash', $file->get_sha1(), PDO::PARAM_STR);
    $q->bindValue(':size', $file->size, PDO::PARAM_INT);
    $q->execute();
    $result = $q->fetch();
    if ($result['count'] > 0) {
        unlink($file->tempfile);
        return array('hash' => $file->get_sha1(), 'name' => $file->name, 'url' => POMF_URL . $result['filename'], 'size' => $file->size);
    }
    // Generate a name for the file
    $newname = generate_name($file);
    // Attempt to move it to the static directory
    if (move_uploaded_file($file->tempfile, POMF_FILES_ROOT . $newname)) {
        // Need to change permissions for the new file to make it world readable
        if (chmod(POMF_FILES_ROOT . $newname, 0644)) {
            // Add it to the database
            $q = $db->prepare('INSERT INTO files (hash, originalname, filename, size, date, ' . 'expire, delid) VALUES (:hash, :orig, :name, :size, :date, ' . ':exp, :del)');
            //Adds expire date to database for removal via python script and cron
            $expTime = date("Y-m-d H:i:s", time() + 9001 * 60 * 60);
            if ($_POST['Time'] == '1') {
                $expTime = date("Y-m-d H:i:s", time() + 9001 * 60 * 60);
            }
            if ($_POST['Time'] == '2') {
                $expTime = date("Y-m-d H:i:s", time() + 6 * 60 * 60);
            }
            if ($_POST['Time'] == '3') {
                $expTime = date("Y-m-d H:i:s", time() + 24 * 60 * 60);
            }
            if ($_POST['Time'] == '4') {
                $expTime = date("Y-m-d H:i:s", time() + 48 * 60 * 60);
            }
            if ($_POST['Time'] == '5') {
                $expTime = date("Y-m-d H:i:s", time() + 168 * 60 * 60);
            }
            if ($_POST['Time'] == '6') {
                $expTime = date("Y-m-d H:i:s", time() + 720 * 60 * 60);
            }
            // Common parameters binding
            $q->bindValue(':hash', $file->get_sha1(), PDO::PARAM_STR);
            $q->bindValue(':orig', strip_tags($file->name), PDO::PARAM_STR);
            $q->bindValue(':name', $newname, PDO::PARAM_STR);
            $q->bindValue(':size', $file->size, PDO::PARAM_INT);
            $q->bindValue(':date', date('Y-m-d'), PDO::PARAM_STR);
            $q->bindValue(':exp', $expTime, PDO::PARAM_STR);
            $q->bindValue(':del', sha1($file->tempfile), PDO::PARAM_STR);
            $q->execute();
            return array('hash' => $file->get_sha1(), 'name' => $file->name, 'url' => POMF_URL . $newname, 'size' => $file->size);
        } else {
            throw new Exception('Failed to change file permissions', 500);
        }
    } else {
        throw new Exception('Failed to move file to destination', 500);
    }
}
开发者ID:happy-box,项目名称:Pomf_Expirey_Support,代码行数:70,代码来源:upload.php


注:本文中的UploadedFile::get_sha1方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。