簡體   English   中英

使用php filemtime()和angularjs獲取文件的最新修改時間時出錯

[英]Error when using php filemtime() and angularjs to grab file's latest modification time

我正在使用AngularJS,並嘗試通過使用php的filemtime()函數來打印文件上載到服務器的日期,但出現錯誤。

這是我嘗試在其中一個表格單元格中使用的內容:

<td><?php $file = 'images/icons/{{icon.fileName}}.png'; echo date('m/d/Y', filemtime($file)); ?></td>

不幸的是,當您查看頁面時,出現以下PHP錯誤:

Warning: filemtime(): stat failed for images/icons/air_balloon.png in /home/ogmda/public_html/symbols/symbols.php on line 105
12/31/1969

我的現場演示在這里

有人可以幫助我走上正確的道路來解決此問題嗎? 謝謝。

您在這里遇到的是經典的前端與后端問題。 您必須先從服務器獲得所需的一切,然后才能在Angular的前端中開始使用它(除非您隨后再執行另一個ajax請求以獲取更多數據)。

因此,我建議的解決方案是您將更多信息與icon對象一起發送。

您的頁面中有此腳本,該腳本將轉到MOCK_DATA2.json以獲取信息。 該文件不僅需要包含icon.fileName,還需要包含icon.filemtime。 您可以手動執行此操作,也可以使用服務器端腳本來添加或生成它。

var iconApp = angular.module('iconApp', ['ngAnimate']);

iconApp.controller('ListController', ['$scope', '$http', function($scope, $http){
    $http.get('js/MOCK_DATA2.json').success(function(data){
        $scope.iconData = data;
        $scope.orderProp = "name";
    });
}]);

將其添加到iconData后,您可以更改頁面的html:

<tr ng-animate="'animate'" ng-repeat="icon in filtered = (iconData | filter: query | orderBy: orderProp)">
                        <td><img src="images/icons/{{icon.fileName}}.png"></td>
                        <td>{{icon.name}}</td>
                        <td>{{icon.group}}</td>
                        <td>{{icon.description | limitTo:50:begin}}</td>
                        <td>{{icon.filemtime}}</td>

                    </tr>

PHP選項

如果您沒有選擇向文件中添加更多信息的選項,則可以使用php, json_decode()其加載,循環圖標並獲取每個文件的filemtime。 將每個都添加到您的數據中,然后添加json_encode()並以javascript可以看到的方式將其打印到頁面的html中:

var iconData = <?php print json_encode($iconData); ?>;

要做更多的工作,然后將圖標數據粘貼到頁面的html中,但這可能是更易於管理的工作流。

最終選擇

還有另一種選擇,但是每個圖像需要一個額外的ajax請求,以嘗試獲取文件修改時間。 看這里:

是否可以使用javascript查找文件的上次修改時間?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM