-1

わかりました私は自分の答えを編集しました。ディレクトリ内のファイルを見つける方法を見つけましたが、私の問題は画像が印刷されないことです。これが私のコードです。

<?php
$file = 'testimage.png'; 

$dir = array(
    $_SERVER['DOCUMENT_ROOT'] . "/folderA/", 
    $_SERVER['DOCUMENT_ROOT'] . "/folderB/"
);

foreach($dir as $d)
    {

    if(file_exists( $d . $file )) 
    {
        $file = $file;
    } 

}

$imgPng = imageCreateFromPng($file);

header("Content-type: image/png");
imagePng($imgPng); 

?>

なぜ画像を印刷しないのですか?

4

3 に答える 3

0

問題は$imgPng = imageCreateFromPng($file);、ファイルが見つからないことが原因である可能性があります。画像へのパスを指定する必要があります$d. $file。以下のコードのコメントを確認してください。

<?php
$file = 'testimage.png';

$dir = array($_SERVER['DOCUMENT_ROOT'] . "/folderA/", $_SERVER['DOCUMENT_ROOT'] . "/folderB/");

foreach ($dir as $d) {

    if (file_exists($d . $file)) {
        $file = $file;
        //i don't know the purpose of this but i think you want to do $file = $d . $file
    }

}

//$imgPng = imageCreateFromPng($file); //can't find the image, should be

$imgPng = imageCreateFromPng($d . $file);

header("Content-type: image/png");
imagePng($imgPng);
?>
于 2013-10-25T04:00:36.067 に答える
0
<?php
$file = 'testimage.png'; 

$dir = [
    $_SERVER['DOCUMENT_ROOT'] . "/pathA/", 
    $_SERVER['DOCUMENT_ROOT'] . "/pathB/"];

foreach( $dir as $d )
    {

    if( file_exists( $d . $file )) 
    {
        //set image if found in the directories
        $image = $d . $file;        
    }
    else
    {
        //is not found in directories
        $image = null;
    }   
}

$img = imagecreatefrompng($image);

header("Content-type: image/png");
imagepng($img);
imagedestroy();
?>
于 2013-10-25T04:46:13.583 に答える