2013-04-11 86 views
0

我想嵌入文件上传,但它显示PDF和其他应用程序文件无效的错误,我使用的代码后,任何人都可以帮助我追踪出错,为什么它显示无效的消息。提前致谢。php上传无效

<?php 
$allowedExts = array("gif", "jpeg", "jpg", "png"); 
$extension = end(explode(".", $_FILES["file"]["name"])); 
if ((($_FILES["file"]["type"] == "image/gif") 
|| ($_FILES["file"]["type"] == "image/jpeg") 
|| ($_FILES["file"]["type"] == "image/jpg") 
|| ($_FILES["file"]["type"] == "image/png") 
|| ($_FILES["file"]["type"] == "application/pdf")) 
&& ($_FILES["file"]["size"] < 50000) 
&& in_array($extension, $allowedExts)) 
    { 
    if ($_FILES["file"]["error"] > 0) 
    { 
echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; 
} 
    else 
{ 
    echo "Upload: " . $_FILES["file"]["name"] . "<br>"; 
    echo "Type: " . $_FILES["file"]["type"] . "<br>"; 
    echo "Size: " . ($_FILES["file"]["size"]/1024) . " kB<br>"; 
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>"; 

if (file_exists("upload/" . $_FILES["file"]["name"])) 
    { 
    echo $_FILES["file"]["name"] . " already exists. "; 
    } 
else 
    { 
    move_uploaded_file($_FILES["file"]["tmp_name"], 
    "upload/" . $_FILES["file"]["name"]); 
    echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; 
    } 
    } 
      } 
    else 
     { 
     echo "Invalid file"; 
    } 
?> 
+1

你的缩进和嵌套似乎很难解析。我建议你把它排序出来,这样也许会更容易理解你自己的代码在做什么。我建议重构来分离函数/块并自动生成代码。 – eis 2013-04-11 06:33:23

+0

一个简单的改变,比如缓存'$ _FILES ['file']'会使代码变得更干净。 – elclanrs 2013-04-11 06:36:15

回答

2

您只允许.gif.jpeg.jpg.png 1号线:

$allowedExts = array("gif", "jpeg", "jpg", "png"); 

如果您上传PDF,当然它会提示Invalid file

0

在你

$allowedExts = array("gif", "jpeg", "jpg", "png") 

不包含在 “PDF”,请添加允许的扩展名 “$ allowedExts” 像

 $allowedExts = array("gif", "jpeg", "jpg", "png","pdf","php"); 
0

,请尝试以下方法:

使用:$allowedExts = array("gif", "jpeg", "jpg", "png","pdf");

代替:$allowedExts = array("gif", "jpeg", "jpg", "png");

和今后请继续记住添加你想要的扩展名。

我希望这会有所帮助。

0

已经指出了明显的错误。

除此之外,如果你重构和重新格式化您的代码,类似

$allowedExts = Array("gif", "jpeg", "jpg", "png"); 
$allowedContentTypes = Array("application/pdf", "image/png", "image/jpeg", "image/jpg", "image/gif"); 
$fileSizeLimit = 50000; 

function getExtension($file) 
{ 
    return end(explode(".", $file["name"])); 
} 
function hasAllowedContentType($file) 
{ 
    return in_array($file['type'], $allowedContentTypes); 
} 
function isWithinSizeLimits($file) 
{ 
    return $file['size'] < $fileSizeLimit; 
} 
function hasAllowedExtension($file) 
{ 
    $extension = getExtension($file['name'); 
    return in_array($extension, $allowedExts); 
} 
function alreadyExists($path) 
{ 
    return file_exists($path); 
} 

$file = $_FILES["file"]; 
$targetPath = "upload/" . $file["name"]; 
$tempPath = $file["tmp_name"]; 

if (!hasAllowedContentType($file) 
    || !isWithinSizeLimits($file) 
    || !hasAllowedExtension($file)) 
{ 
    echo "Invalid file"; 
} 
else if ($file["error"] > 0) 
{ 
    printf("Return Code: %s<br>", $file["error"]); 
} 
else 
{ 
    printf("Upload: %s<br>", $file["name"]); 
    printf("Type: %s<br>", $file["type"]); 
    printf("Size: %d kB<br>", ($file["size"]/1024)); 
    printf("Temp file: %s<br>", $tempPath); 

    if (alreadyExists($targetPath)) 
    { 
    printf("%s already exists.", $file["name"]); 
    } 
    else 
    { 
    move_uploaded_file($tempPath, $targetPath); 
    printf("Stored in: %s", $targetPath); 
    } 
} 

它已经是一个更容易理解和维护。