iT邦幫忙

0

用php判斷檔案類型

在對php不熟的情況下,我想用php來判斷檔案類型。因為資料夾不一樣,所以我想知道該怎麼判斷。
圖片檔的資料夾是picture,音樂的是music。
以下的程式碼是一個高手幫我弄的。只能顯示圖片!!

for ($j = 0; $j < $COLS; $j++) {
if (!$arrProducts[$index]) {
echo '<td width="145"> </td>';
$index++;
continue;
}
echo '<td bgcolor="#99CCFF" align="center" valign="top" width="145">';
echo '<p><a href="product.php?id='.$arrProducts[$index]['Composition_id'].'">
<img src = "picture/'.$arrProducts[$index]['Composition_sename'].'" width=100 height=120 ;></a></p>';
echo '<p>點閱率:'.$arrProducts[$index]['Click_count'].'</p>';
echo '<p>種類:'.$arrProducts[$index]['Class_name'].'</p>';
echo '<p>分類:'.$arrProducts[$index]['Type_name'].'</p>';
echo '<p>產品名稱:'.$arrProducts[$index]['Composition_name'].'</p>';
echo '<p>作者:'.$arrProducts[$index]['Member_nickname'].'</p>';
if ($arrProducts[$index]['Composition_way'] == 1) {
echo '<p>'.(($arrProducts[$index]['Class_id'] != 3)?
'<input id="collect" name="collect" type="button" value="引用" 
onClick="top.location=\'collect.php?id='.$arrProducts[$index]['Composition_id'].'\'"> ' : '').
'<input id="exchange" name="exchange" type="button" value="交換"
onClick="top.location=\'exchange.php?id='.$arrProducts[$index]['Composition_id'].'\'"></p>';
} elseif ($arrProducts[$index]['Composition_way'] == 2) {
echo '<p>'.(($arrProducts[$index]['Class_id'] != 3)?
'<input id="collect" name="collect" type="button" value="引用" 
onClick="top.location=\'collect.php?id='.$arrProducts[$index]['Composition_id'].'\'"> ' : '').'</p>';
} elseif ($arrProducts[$index]['Composition_way'] == 3) {
echo '<p><input id="exchange" name="exchange" type="button" value="交換" 
onClick="top.location=\'exchange.php?id='.$arrProducts[$index]['Composition_id'].'\'"></p>';
}
echo '</td>';
$index++;
}
看更多先前的討論...收起先前的討論...
player iT邦大師 1 級 ‧ 2010-07-26 20:37:28 檢舉
如果你把php程式碼
掐頭去尾的
那恐怕別人想幫
也很難幫吧?

不過
大概是

# echo '<p><a href="product.php?id='.$arrProducts[$index]['Composition_id'].'">
# <img src = "picture/'.$arrProducts[$index]['Composition_sename'].'" width=100 height=120 ;></a></p>';

這段改掉, 音樂的話, 看你要用怎麼方式播放, 把你撥放的那段補進去
先把附檔名從 $arrProducts[$index]['Composition_sename'] 切出來判斷
再用 if 去決定要顯示哪段的內容
fillano iT邦超人 1 級 ‧ 2010-07-27 09:28:22 檢舉
先釐清一些問題:
1. 你的需求(播放音樂?播放影片?下載檔案?顯示圖片?其他?)
2. 檔案類型包括哪些(音樂、影片、pdf、office、zip...)?總不會是任意檔案吧?
f211333 iT邦新手 5 級 ‧ 2010-07-27 10:08:40 檢舉
1.我的需求是可以播放音樂&顯示圖片
2.檔案類型包括mp3、jpg、gif這三種
player iT邦大師 1 級 ‧ 2010-07-27 20:57:12 檢舉
當附檔名為 jpg、gif 時, 延用原本的code
當附檔名為 mp3 時, 改輸出撥放 mp3 的code , 通常 call Flash 或 WMP 來用
fillano iT邦超人 1 級 ‧ 2010-07-27 22:06:50 檢舉
mp3可以考慮用這個flash player內嵌在網頁中來播放:
http://flash-mp3-player.net/

如果還有影片的話,用flowplayer會比較完整:
http://flowplayer.org/

FlowPlayer功能蠻強的,但是使用起來複雜很多。

這兩個都是OpenSource的,需要的話你還可以自己把source拿來改。

圖片的話應該沒問題吧?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
godstamp
iT邦新手 3 級 ‧ 2010-07-28 02:13:46
最佳解答

你的問題說明,看不太懂..看那段 code 還是不太清楚...
所以針對你的標題回答,希望能有幫助..

如果你是將圖片檔和音樂檔放在不同資料夾下,建議可在 db 的資料表中加個欄位(folder_type)代表分類,例如:圖片是 1 ,音樂是 2

&lt;pre class="c" name="code">
//先使用一個變數來定義...
$folder[1] = 'picture';
$folder[2] = 'music';

//假設你從資料庫中取出的資料放在 $row,那麼該檔案路徑應該就是這樣...
$path = $folder[$row['folder_type']].'/'.$fileName;

不用判斷^^

當然,如果要用判斷的方式也行

&lt;pre class="c" name="code">
if($row['folder_type'] == 1){
    $path = 'picture/'.$fileName;
}else if($row['folder_type'] == 2){
    $path = 'music/'.$fileName;
}

另外,如果你是把全部的檔案不分類放到同一個資料夾,依照檔案類型使用不同方式處理的話,也可以使用副檔名來做判斷,以下例子是使用迴圈將資料夾中每個檔名都拿出來判斷...

&lt;pre class="c" name="code">
//開資料夾
$openPath = './folder/';
$dh = opendir($openPath);

//讀檔
while($file = readdir($dh)){

    //完整路徑檔案訊息
    $filePath = $openPath.$file;
    $pathInfo = pathinfo($filePath);
    
    //依副檔名處理
    switch($pathInfo['extension']){
        
        //mp3 檔案的處理方式
        case 'mp3':
            ....
        break;
        
        //jpg 檔案的處理方式
        case 'jpg':
            ....
        break;
        
        //gif 檔案的處理方式
        case 'gif':
            ....
        break;
    }
}

//關資料夾
closedir($dh);

我要發表回答

立即登入回答