开发者

PHP Image Serving, extraneous bytes before marker

开发者 https://www.devze.com 2023-01-23 17:45 出处:网络
I\'m desperately trying to solve this one. I have a bunch of files stored outside of the webroot and I need to serve them to a user after a few auth checks. These files have been uploaded using a Flex

I'm desperately trying to solve this one. I have a bunch of files stored outside of the webroot and I need to serve them to a user after a few auth checks. These files have been uploaded using a Flex application or have just been manually uploaded through FTP. I have a serving script that looks something like:

<?php

$filePath = '/for/demonstration/only.jpg';
...
$type = exif_imagetype($filePath);
$size = filesize($filePath);
if ($type && $size > 0) {
    switch($type)
    {
        case IMAGETYPE_PNG:
            header("Content-Type: image/png");
            break;
        case IMAGETYPE_JPEG:
            header("Content-Type: image/jpeg");
            break;
        default:
            header("Content-Type: text/plain");
            break;
    }
    header("Content-Length: {$size}");
    readfile($filePath);
    exit;
} else {
    echo 'error';
}

Pretty simple. The image however, somewhere in the upload process, Because of the encoding process, the file has gained an extra 100-130B, and now seems to be corrupted. I get the extraneous bytes error. The upload script is pretty simple as well, Flex uses FileRefrence for the user to select the file, then encodes the data and sends it to the server script:

<?php
function fileupload($data)
{
    $daily_folder = 'today/';

    $fileName_clipped = substr( $fileName, 0, $max_file_len );
    $fileName_clippe开发者_开发技巧d = preg_replace('/\./','_',$fileName_clipped);
    $filePath = '/path/to/storage' . $daily_folder;

    if(!is_dir($filePath))
        mkdir($filePath);

    if( strlen($data->filedata) > 0 ) {
        if( !file_put_contents($filePath . $fileName_clipped, base64_decode($data->filedata)) )
            return false;
    } else {
        return false;
    }
}

Running the process

file A: 31,740B in, 31,848B out, 108B extra

file B: 35,273B in, 31,403B out, 130B extra

I imagine this could be on the Flash side, but honestly it's dirt simple. I just don't see where the extra data is coming in, and why its corrupting the file. Anyone know why this is happening? or better yet, how I can clean these files up now?


Here's the dealio. When I backed up all the images from one server and moved them to another I FTPed all the files onto my Windows laptop. That process, whether because of Windows or perhaps FileZilla, corrupted all of the files. adding a bunch of junk onto them. I'd live input as to what you think caused the problem, but regardless I located the problem and fixed it.

The solution was to zip the parent directory on the server and download the zip. I didn't have to modify any code. Just a simple procedure revision. How lame.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号