如何链接到PHP平面文件中的图像,并在文件名中添加重音符号


How to link to an image in PHP flatfile with accents in the filename

当我在标题中使用重音符号时,图像不会加载。字符集没有错,但特殊字符破坏了文件的路径。

我只需要西班牙语和法语口音,但我不喜欢逐个替换所有字符的解决方案。我没有其他办法,有什么建议吗?

<?php
    $dir   = 'img/'; // Directory for images
    $filetype = '*.*'; // Files
    $allow = array('jpg', 'gif'); // Files allowed in the array
    $files = glob($dir.$filetype);
    $newest_images_first = true;
    $files = array_reverse($files); // Sort files in reverse
    $i=0;
    $open = opendir($dir);
    // Get filenames from directory, get extension and if the extension is valid, store in array using numerical indexing
    while (($file=readdir($open))!==false) {
    $ext=str_replace('.', '', strrchr($file, '.'));
    if (in_array($ext, $allow)) 
    $list[$i++]=$file; }
    $perPage= 20; // Number of images per page
    $total=count($list); // Number of images to show in total
    $pages=ceil($total/$perPage); // Number of pages: the number of images divided by how many per page
    $thisPage=isset($_GET['pg'])?$_GET['pg']-1:0; // did user select a specific page? Note, pages on web are counted from 1 (not zero) so must subtract 1 for correct indexing
    $start=$thisPage*$perPage; // calculate starting index into list of filenames
    $pageNumber= $thisPage+1;
    $perRow= 1; // Number images per row
    $imgCnt=0; // used to count number of images displayed and hence whether to wrap page. note, could use "for" index $i but this is computationally quicker
    for ($i=$start;$i<$start+$perPage;$i++) {
    // may be too few images to fill page, so check if we have a valid array index. if we don't output empty table cell so fussy browsers
    // don't mis-display table due to missing cells
    echo "<div class='item' 'hyphenate'>";
    if (isset($list[$i])) {
        echo "<figure>";
        echo '<div class="photo">';
                // Functions before alt attribute
                $exif = exif_read_data($files[$i], 0, true); // Fetch Exif metadata
                error_reporting(E_ERROR | E_PARSE); // Avoid warnings when trying to get exif data from PNG files
                $title = substr($files[$i],strlen($dir),strrpos($files[$i], '.')-strlen($dir)); // Get filename, ignore path and text since the last '.'
                $title = str_replace( array( '%', '-'), " ", $title); // Replace symbols with a space
                $title = substr($title, 5); // Substract first 5 characters
                $title = str_replace( array('', '_'), "", $title); // Replace items with nothing
        // Image
        echo '<img src="'.$files[$i].'" alt="'.$title.'" onload="this.width*=0.6">';
        echo "</div>"; // Photo
            echo "<figcaption>";                    
                // Title    
                $title = preg_replace('/''([^'']+)''/', '<em>$1</em>', $title); // Make italics from anything within single quotes
                echo '<h2>'."". $title .'</h2>'; // Show title
                echo '<p>'; 
                echo "".$exif['IFD0']['ImageDescription'].""; // Filter to IFD0 and ImageDescription
                echo '</p>';    
            echo "</figcaption>";       
        echo "</figure>";           
    echo "</div>"; // item
    }else {
    echo "<td></td>"; // create an empty td
    }
    $imgCnt+=1; // increment images shown
    if ($imgCnt%$perRow==0) // if image count divided by number to show per row has no remainder then it's time to wrap
    echo "</tr><tr>";
    }
    echo "</tr>";
    closedir($open);
?>

首先,检查web服务器的访问/错误日志可能会显示浏览器发送了哪些有问题的URL以及它们是如何失败的。

该问题可能与URL的编码有关。URL中允许的字符是有限的,否则需要进行编码(转义)。

我想尝试改变这个:

echo '<img src="'.$files[$i].'" alt="'.$title.'" onload="this.width*=0.6">';

$filename = basename($files[$i]); // get the name part
$filename = urlencode($filename); // escape for URL
echo '<img src="' . $filename. '.' . $ext . '" alt="'.$title.'" onload="this.width*=0.6">';