php - 将附件文件上传到另一台服务器 [Mybb]

标签 php html mysql upload mybb

我使用的是最新版本的mybb(mybb1.8),它有附件功能,但它上传文件到当前主机,我想将文件上传到不同的服务器或主机。

我怎样才能做到这一点?

如果需要,请下载 Mybb18: http://resources.mybb.com/downloads/mybb_1800.zip

attachment.php文件的PHP代码

<?php
/**
 * MyBB 1.8
 * Copyright 2014 MyBB Group, All Rights Reserved
 *
 * Website: http://www.mybb.com
 * License: http://www.mybb.com/about/license
 *
 */

define("IN_MYBB", 1);
define('THIS_SCRIPT', 'attachment.php');

require_once "./global.php";

if($mybb->settings['enableattachments'] != 1)
{
    error($lang->attachments_disabled);
}

// Find the AID we're looking for
if(isset($mybb->input['thumbnail']))
{
    $aid = $mybb->get_input('thumbnail', 1);
}
else
{
    $aid = $mybb->get_input('aid', 1);
}

$pid = $mybb->get_input('pid', 1);

// Select attachment data from database
if($aid)
{
    $query = $db->simple_select("attachments", "*", "aid='{$aid}'");
}
else
{
    $query = $db->simple_select("attachments", "*", "pid='{$pid}'");
}
$attachment = $db->fetch_array($query);

$plugins->run_hooks("attachment_start");

if(!$attachment)
{
    error($lang->error_invalidattachment);
}
$pid = $attachment['pid'];

// Don't check the permissions on preview
if($pid || $attachment['uid'] != $mybb->user['uid'])
{
    $post = get_post($pid);
    $thread = get_thread($post['tid']);

    if(!$thread && !isset($mybb->input['thumbnail']))
    {
        error($lang->error_invalidthread);
    }
    $fid = $thread['fid'];

    // Get forum info
    $forum = get_forum($fid);

    // Permissions
    $forumpermissions = forum_permissions($fid);

    if($forumpermissions['canview'] == 0 || $forumpermissions['canviewthreads'] == 0 || (isset($forumpermissions['canonlyviewownthreads']) && $forumpermissions['canonlyviewownthreads'] != 0 && $thread['uid'] != $mybb->user['uid']) || ($forumpermissions['candlattachments'] == 0 && !$mybb->input['thumbnail']))
    {
        error_no_permission();
    }

    // Error if attachment is invalid or not visible
    if(!$attachment['attachname'] || (!is_moderator($fid, "canviewunapprove") && ($attachment['visible'] != 1 || $thread['visible'] != 1 || $post['visible'] != 1)))
    {
        error($lang->error_invalidattachment);
    }
}

if(!isset($mybb->input['thumbnail'])) // Only increment the download count if this is not a thumbnail
{
    $attachupdate = array(
        "downloads" => $attachment['downloads']+1,
    );
    $db->update_query("attachments", $attachupdate, "aid='{$attachment['aid']}'");
}

// basename isn't UTF-8 safe. This is a workaround.
$attachment['filename'] = ltrim(basename(' '.$attachment['filename']));

$plugins->run_hooks("attachment_end");

if(isset($mybb->input['thumbnail']))
{
    $ext = get_extension($attachment['thumbnail']);
    switch($ext)
    {
        case "gif":
            $type = "image/gif";
            break;
        case "bmp":
            $type = "image/bmp";
            break;
        case "png":
            $type = "image/png";
            break;
        case "jpg":
        case "jpeg":
        case "jpe":
            $type = "image/jpeg";
            break;
        default:
            $type = "image/unknown";
            break;
    }

    header("Content-disposition: filename=\"{$attachment['filename']}\"");
    header("Content-type: ".$type);
    $thumb = $mybb->settings['uploadspath']."/".$attachment['thumbnail'];
    header("Content-length: ".@filesize($thumb));
    $handle = fopen($thumb, 'rb');
    while(!feof($handle))
    {
        echo fread($handle, 8192);
    }
    fclose($handle);
}
else
{
    $ext = get_extension($attachment['filename']);

    switch($attachment['filetype'])
    {
        case "application/pdf":
        case "image/bmp":
        case "image/gif":
        case "image/jpeg":
        case "image/pjpeg":
        case "image/png":
        case "text/plain":
            header("Content-type: {$attachment['filetype']}");
            $disposition = "inline";
            break;

        default:
            $filetype = $attachment['filetype'];

            if(!$filetype)
            {
                $filetype = 'application/force-download';
            }

            header("Content-type: {$filetype}");
            $disposition = "attachment";
    }

    if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), "msie") !== false)
    {
        header("Content-disposition: attachment; filename=\"{$attachment['filename']}\"");
    }
    else
    {
        header("Content-disposition: {$disposition}; filename=\"{$attachment['filename']}\"");
    }

    if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), "msie 6.0") !== false)
    {
        header("Expires: -1");
    }

    header("Content-length: {$attachment['filesize']}");
    header("Content-range: bytes=0-".($attachment['filesize']-1)."/".$attachment['filesize']);
    $handle = fopen($mybb->settings['uploadspath']."/".$attachment['attachname'], 'rb');
    while(!feof($handle))
    {
        echo fread($handle, 8192);
    }
    fclose($handle);
}

最佳答案

Firstof:发布(数据,文件)到服务器的表单,它尚未从 ist 调用,被认为存在安全风险。实现起来并不那么容易让浏览器有信心。所以您的接收脚本应该位于同一服务器上。

这就是为什么我会在文件发布后移动它们。

如果您有 Linux/unix 服务器,我会使用命令行。

您必须将文件从主机复制到目标上,并在成功后删除原始文件。

在文件系统中(在同一服务器上)通过

system("cp filename targetdir");

或者通过

到不同的服务器
system("scp filename server:targetdir")

还可以看看“rsync”,它在服务器之间同步文件时效率更高。

关于php - 将附件文件上传到另一台服务器 [Mybb],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25698477/

相关文章:

php - 处理表单处理的最佳实践

php - Beanstalkd:如何自动埋葬命中 TTR 的作业?

mysql - 使用大量与 wp_postmeta(键/值表)的内部联接来改进查询

php - 替换模式内的所有事件

php - Yii 自动加载命名空间

html - 制作背景幻灯片不是全屏?

html - 将元素居中并让另一个元素向右浮动的最简单方法

javascript - 具有相同脚本的多个模式

php - 像 Swoopo 或 Quibids 这样的网站如何让所有游戏玩家保持计时器

java - 使用一个提交按钮将记录从一个表单提交到多个 SQL 表