2012-07-19 23 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