dongzhan1383 2015-03-04 01:27
浏览 46

如何使用文件名中的重音链接到PHP平面文件中的图像

The images don't load when I use accents in the title. There is nothing wrong with the charset but with the special characters breaking the path to the file.

I just need Spanish and French accents, but I don't like the solution of replacing all the characters one by one. I don't manage to do it any other way, any suggestion?

<?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);
?>
  • 写回答

1条回答 默认 最新

  • donglinxi1467 2015-03-04 01:33
    关注

    First of all, checking web server's access/error log probably will show you what problematic URLs had been sent from the browser and how they had failed.

    The problem might be related to the encoding of the URL. Characters allowed in URLs are limited, and need to be encoded(escaped) otherwise.

    I would like to try changing this:

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

    to

    $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">';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集