在 PHP 4 中复制通过 PHP 的 HTTP Get 上传的文件

发布于 2024-09-26 09:45:51 字数 1237 浏览 5 评论 0原文

我一直致力于向最初用 PHP 4.4.9 编写的网站添加功能。将网站移植到 PHP5 不在他们的预算之内,所以甚至不建议这样做。 (尽管它非常需要)。我面临的问题是如何将二进制数据从 GET 请求复制到服务器上的文件位置。目前编写的支持此方法的代码如下:

function save($path) { 
    $input = fopen("php://input", "r");
    $temp = tmpfile();
    $realSize = stream_copy_to_stream($input, $temp);
    fclose($input);

    if ($realSize != $this->getSize()){            
        return false;
    }

    $target = fopen($path, "w");        
    fseek($temp, 0, SEEK_SET);
    stream_copy_to_stream($temp, $target);
    fclose($target);
}

我遇到的问题是函数 stream_copy_to_stream 仅在 PHP 5 中受支持。这是我到目前为止所拥有的,但它似乎只创建 8K 大小的文件,我不知道为什么。理论上,它应该允许高达 20M。

function save($path) {    
    $input = fopen("php://input", "rb");
    $temp = tmpfile();
    fwrite($temp, fread($input, 20971520));
    fclose($input);

    $target = fopen($path, "w");        
    fseek($temp, 0, SEEK_SET);
    #stream_copy_to_stream($temp, $target);
    fwrite($target, fread($temp, 20971520));
    fclose($target);
    echo $path;

    return true;
}

我删除了大小检查,因为我无法找到在阅读时获取文件大小的方法。对此,我们非常感谢任何帮助。我已经绞尽脑汁花了几个小时,我知道有人(很可能在 stackoverflow 上)可以很容易地回答我的问题。

感谢您提前提供的所有帮助!

另外,我通过 GET 提交数据,以允许带有进度条等的多个文件上传。

I have been working on adding functionality to a site originally written in PHP 4.4.9. It's not in their budget to port the site to PHP5, so don't even suggest it. (Although it needs it badly). The problem I am facing is how to copy binary data from a GET request to a file location on the server. The code that is currently written to support this method is as follows:

function save($path) { 
    $input = fopen("php://input", "r");
    $temp = tmpfile();
    $realSize = stream_copy_to_stream($input, $temp);
    fclose($input);

    if ($realSize != $this->getSize()){            
        return false;
    }

    $target = fopen($path, "w");        
    fseek($temp, 0, SEEK_SET);
    stream_copy_to_stream($temp, $target);
    fclose($target);
}

The problem that I am having with this is the funciton stream_copy_to_stream is only supported in PHP 5. Here is what I have so far, but it seems to only create files that are 8K in size and I'm not sure why. It should, in theory, allow for up to 20M.

function save($path) {    
    $input = fopen("php://input", "rb");
    $temp = tmpfile();
    fwrite($temp, fread($input, 20971520));
    fclose($input);

    $target = fopen($path, "w");        
    fseek($temp, 0, SEEK_SET);
    #stream_copy_to_stream($temp, $target);
    fwrite($target, fread($temp, 20971520));
    fclose($target);
    echo $path;

    return true;
}

I removed the size check because I couldn't figure a way to get the filesize when reading. Any help is greatly appreciated on this. I have been racking my brain for literally hours and I know there is someone out there, most likely on stackoverflow, that can answer my question probably very easily.

Thanks for all the help in advance!

Also, I am submitting data via GET to to allow multiple file uploads with progress bars, etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

著墨染雨君画夕 2024-10-03 09:45:51

我遇到了这个帖子,正在寻找完全相同问题的答案。
我知道帖子很旧,但将答案放在这里供其他人查看。
你很接近。
fread 一次仅从流中取出 8192 字节块。所以你必须循环直到看到文件末尾。


    function save($path) {    
        $input = fopen("php://input", "rb");
        $temp = tmpfile();
        while(!feof($input))
            fwrite($temp, fread($input, 8192));
        //fwrite($temp, fread($input, 20971520));
        fclose($input);

        $target = fopen($path, "w");        
        fseek($temp, 0, SEEK_SET);
        #stream_copy_to_stream($temp, $target);
        while(!feof($temp))
            fwrite($target, fread($temp, 8192));

        fclose($target);
        echo $path;

        return true;
    }

I came across this thread looking for answer for exact same problem.
I know post is old but putting answer here for anyone else looking.
You were close.
fread only takes 8192 byte chunks out of stream at a time. So you will have to loop through until it sees end of file.


    function save($path) {    
        $input = fopen("php://input", "rb");
        $temp = tmpfile();
        while(!feof($input))
            fwrite($temp, fread($input, 8192));
        //fwrite($temp, fread($input, 20971520));
        fclose($input);

        $target = fopen($path, "w");        
        fseek($temp, 0, SEEK_SET);
        #stream_copy_to_stream($temp, $target);
        while(!feof($temp))
            fwrite($target, fread($temp, 8192));

        fclose($target);
        echo $path;

        return true;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文