Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

display image inside zip archive - can't read the file if it has special chars

$
0
0

This script will find a zip archive in a specific folder, then read an image file inside the zip and output it as jpg. It works perfectly when using a simple image filename, but as soon as the image file has special characters it returns an error "could not load image".

Here's the code:

<?php
function showimage($zip_file, $file_name) {
    $z = new ZipArchive();
    if ($z->open($zip_file) !== true) {
        echo "File not found.";
        return false;
    }

    $stat = $z->statName($file_name);
    $fp   = $z->getStream($file_name);
    if(!$fp) {
        echo "Could not load image.";
        return false;
    }

    header('Content-Type: image/jpeg');
    header('Content-Length: ' . $stat['size']);
    fpassthru($fp);
    return true;
}

$imgsrcencoded = $_GET['i'];
$imagesrc = base64_decode($imgsrcencoded);
$explodez = explode("#",$imagesrc);
$imgg = utf8_encode($explodez[1]);
$dirnfile = $explodez[0];
$zipp = end((explode('/', $dirnfile)));
$dirr = str_replace($zipp,"",$dirnfile);
$dirr = rtrim($dirr,"/");
$imgg = urlencode(rtrim($imgg));
// $zipp = urlencode($zipp);
chdir($dirr);
    if (empty($_GET['debug'])) {
    echo showimage($zipp,$imgg);
    // echo showimage("jpg.zip","test.jpg");
    }




// debug
if (!empty($_GET['debug'])) {
echo "
base64 query = $imgsrcencoded<br>
decoded query = $imgsrc<br>
zipp = $zipp<br>
imgg = $imgg<br>
dirr = $dirr<br>
current directory = ";
echo getcwd();
}
?>


And here's what my debug returns:

base64 query = ZGwvcHAvNjI1MC84NiBDcmV3IC0gMjAwMCAtIEJhZCBCYWQgUmVnZ2FlLnppcCM4IDYgQ3JldyAtIEJhZCBCYWQgUmVnZ2FlLWZyb250ICBbd3d3LlBpcmF0ZS1QdW5rLm5ldF0uanBnCg==

decoded query = dl/pp/6250/86 Crew - 2000 - Bad Bad Reggae.zip#8 6 Crew - Bad Bad Reggae-front [www.Pirate-Punk.net].jpg

zipp = 86 Crew - 2000 - Bad Bad Reggae.zip

imgg = 8+6+Crew+-+Bad+Bad+Reggae-front++%5Bwww.Pirate-Punk.net%5D.jpg

dirr = dl/pp/6250

current directory = /var/www/clients/client2/web1/web/dl/pp/6250


So as you can see the script correctly switch to the corresponding directory and it is able to open the zip archive but it fails when come the time to read the JPG file if the filename has special characters

I tried with or without utf8_encode() on the image's filename, i tried with or without urlencode() or rawurlencode() but i always get the same result...

any suggestion ? i'm desperate.

Viewing all articles
Browse latest Browse all 13200

Trending Articles