ds342222222 2014-09-06 08:41
浏览 19

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

I'm using the latest version of mybb (mybb1.8), and it has attachment feature, but it upload the files into current host, I want upload the files to different server or host.

How can I do that ?

Download Mybb18 if you need it: http://resources.mybb.com/downloads/mybb_1800.zip

PHP code of attachment.php file

<?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);
}
  • 写回答

1条回答 默认 最新

  • dongxie548548 2014-09-08 15:45
    关注

    Firstof: a Form that posts(Data, a file) do a server it has not been called from ist considered a security risk. It is not so easy to implement so that browsers feel confident. So your recieving script should be on the same server.

    That is why I would move the files after they have been posted.

    I would use the command line for that if you have a linux/unix server.

    You will have to copy the file from your host onto the target and delete the orginal file when successful.

    Either in the filesystem(on the same server) via

    system("cp filename targetdir");
    

    or onto a different server via

    system("scp filename server:targetdir")
    

    also have a look at "rsync" which ist much more efficient when syncing files from server to server.

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算