2012-07-19 26 views
1

與爆炸麻煩()我從uploadify.php這個片段:具有uploadify.php

if (!empty($_FILES)) { 
$name = $_FILES['Filedata']['name']; 

$tempFile = $_FILES['Filedata']['tmp_name']; 
$targetPath = $targetFolder; 
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name']; 

$path = pathinfo($targetFile); 

// this portion here will be true if and only if the file name of the uploaded file does not contain '.', except of course the dot(.) before the file extension 
$count = 1; 
list($filename, $ext) = explode('.', $name,); 
$newTargetFile = $targetFolder . $filename . '.' . $ext; 
while(file_exists($newTargetFile)) { 
    $newTargetFile = $targetFolder . $filename . '(' . ++$count . ')' . '.' . $ext; 
} 

// Validate the file type 
$fileTypes = array('pdf'); // File extensions 
$fileParts = pathinfo($_FILES['Filedata']['name']); 

if (in_array($fileParts['extension'],$fileTypes)) { 
    move_uploaded_file($tempFile,$newTargetFile); 
    echo $newTargetFile; 
} else { 
    echo 'Invalid file type.'; 
} 
return $newTargetFile; 
} 

這基本上是相當的工作。上傳文件並獲取文件的路徑,然後將其插入數據庫等。但是,我試着上傳一個文件,該文件的文件名看起來像這樣,

filename.1.5.3.pdf 

,當成功地上傳,文件名則成了filename獨自一人,而無需文件擴展名,而不是提的文件名是不完整的。從我的理解,問題在於我的爆炸()。它分解了具有分隔符'.'的字符串,然後將其分配給變量。我會做什麼來使explode()將字符串分割爲兩部分,其中前半部分是文件名,第二部分是文件擴展名?請幫忙。

回答

3

不要使用爆炸,使用專門作業的功能:pathinfo()

$ext = pathinfo($_FILES['Filedata']['name'], PATHINFO_EXTENSION); 
+1

以及物品是否完整起見,$文件名= PATHINFO($ _ FILES [ 'Filedata上'] [ '名'],PATHINFO_FILENAME) ; – ernie 2012-07-19 23:53:01

+0

我的不良bro.TSK。我沒有發現使用pathinfo()。它對我來說真的很好。非常感謝。 :) – 2012-07-20 00:00:30