Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 下载带有“0”的二进制文件时;fwrite";-&引用;0D";字节消失_Php_Fwrite - Fatal编程技术网

Php 下载带有“0”的二进制文件时;fwrite";-&引用;0D";字节消失

Php 下载带有“0”的二进制文件时;fwrite";-&引用;0D";字节消失,php,fwrite,Php,Fwrite,我正在测试以10 MB的块下载大文件: /** * Copy remote file over HTTP one small chunk at a time. * * @param $infile The full URL to the remote file * @param $outfile The path where to save the file */ function copyfile_chunked($infile, $outfile) { $chunksize


我正在测试以10 MB的块下载大文件:

/**
 * Copy remote file over HTTP one small chunk at a time.
 *
 * @param $infile The full URL to the remote file
 * @param $outfile The path where to save the file
 */
function copyfile_chunked($infile, $outfile) {
    $chunksize = 10 * (1024 * 1024); // 10 Megs

    /**
     * parse_url breaks a part a URL into it's parts, i.e. host, path,
     * query string, etc.
     */
    $parts = parse_url($infile);
    $i_handle = fsockopen($parts['host'], 80, $errstr, $errcode, 5);
    $o_handle = fopen($outfile, 'wb');

    if ($i_handle == false || $o_handle == false) {
        return false;
    }

    if (!empty($parts['query'])) {
        $parts['path'] .= '?' . $parts['query'];
    }

    /**
     * Send the request to the server for the file
     */
    $request = "GET {$parts['path']} HTTP/1.1\r\n";
    $request .= "Host: {$parts['host']}\r\n";
    $request .= "User-Agent: Mozilla/5.0\r\n";
    $request .= "Keep-Alive: 115\r\n";
    $request .= "Connection: keep-alive\r\n\r\n";
    fwrite($i_handle, $request);

    /**
     * Now read the headers from the remote server. We'll need
     * to get the content length.
     */
    $headers = array();
    while(!feof($i_handle)) {
        $line = fgets($i_handle);
        if ($line == "\r\n") break;
        $headers[] = $line;
    }

    /**
     * Look for the Content-Length header, and get the size
     * of the remote file.
     */
    $length = 0;
    foreach($headers as $header) {
        if (stripos($header, 'Content-Length:') === 0) {
            $length = (int)str_replace('Content-Length: ', '', $header);
            break;
        }
    }

    /**
     * Start reading in the remote file, and writing it to the
     * local file one chunk at a time.
     */
    $cnt = 0;
    while(!feof($i_handle)) {
        $buf = '';
        $buf = fread($i_handle, $chunksize);
        $bytes = fwrite($o_handle, $buf);
        if ($bytes == false) {
            return false;
        }
        $cnt += $bytes;

        /**
         * We're done reading when we've reached the conent length
         */
        if ($cnt >= $length) break;
    }

    fclose($i_handle);
    fclose($o_handle);
    return $cnt;
}
我首先在一个小图像上测试这段代码。该图像被下载到我的帐户,但以一种currupted格式:所有字节似乎都是正确的,从下载的图像中删除了“0D”字节,这使其无法使用。
为什么会发生这种情况?我如何克服它?

谢谢

大家好,谢谢你们的帮助。
问题现已解决,罪犯已被查明。
我读了一些书,发现:
ftp_get()将远程服务器上的文件复制到您的计算机上。FTP_ASCII参数将文件传输为ASCII文本。在此选项下,将显示换行符端点 在从一个操作系统移动到另一个操作系统时自动转换。另一种选择 是FTP_二进制文件,用于非文本文件,因此不会进行换行转换。
我的问题中提供的代码运行良好,它可以正确下载图像。
当我检查图像时,我用一个php编写的文件管理器将其下载到我的计算机上,该文件管理器由托管提供商提供。显然,他们并不擅长PHP,因为他们使用上面提到的FTP_ASCII参数来传输二进制文件。因此,图像被破坏了。
当我直接从FTP帐户下载图像时,图像与原始图像完全相同。

因此,最终的问题在于PHP代码,而不是我编译的代码。

大家好,谢谢大家的帮助。
问题现已解决,罪犯已被查明。
我读了一些书,发现:
ftp_get()将远程服务器上的文件复制到您的计算机上。FTP_ASCII参数将文件传输为ASCII文本。在此选项下,将显示换行符端点 在从一个操作系统移动到另一个操作系统时自动转换。另一种选择 是FTP_二进制文件,用于非文本文件,因此不会进行换行转换。
我的问题中提供的代码运行良好,它可以正确下载图像。
当我检查图像时,我用一个php编写的文件管理器将其下载到我的计算机上,该文件管理器由托管提供商提供。显然,他们并不擅长PHP,因为他们使用上面提到的FTP_ASCII参数来传输二进制文件。因此,图像被破坏了。
当我直接从FTP帐户下载图像时,图像与原始图像完全相同。

所以,归根结底,问题出在PHP代码上,而不是我编译的代码。

您最终读取的字节数是否与
内容长度
所指示的字节数相同?嗨,Jon。真奇怪。原始映像是15444字节,函数返回下载的15444字节,但当我检索下载的映像时,它证明只有15397字节,可能是因为缺少“0D”字节。您不使用工作的http客户端库有什么特殊原因吗?你古怪的头解码不符合HTTP。并不是所有的响应都是分块的。服务器配置错误?大概服务器是否为图像发送了正确的
内容类型
。图像大小变为15399字节,因此我猜下载时会再删除两个字节。您最终读取的字节是否与
内容长度
显示的字节数相同?嗨,Jon。真奇怪。原始映像是15444字节,函数返回下载的15444字节,但当我检索下载的映像时,它证明只有15397字节,可能是因为缺少“0D”字节。您不使用工作的http客户端库有什么特殊原因吗?你古怪的头解码不符合HTTP。并不是所有的响应都是分块的。服务器配置错误?大概服务器是否为图像发送了正确的
内容类型
。图像的大小变成了15399字节,所以我猜下载时会删除另外两个字节。