2014-02-11 87 views
0

我现在有一个php文件,它将文件上传到我的Web服务器。目前它会上传jpg文件等文件,但是无论何时我尝试上传excel或word文件(说实话,我需要),它都不会呈现我的代码,只是说'无效文件'。我没有得到任何错误,因为它只回应最后一行。任何想法,将不胜感激。将文件上传到目录

<?php 
include 'dbconnect.php'; 
$target_path = "uploads/"; 
$target_path = $target_path . basename($_FILES['file']['name']); 

//$allowedExts = array("gif", "jpeg", "jpg", "png"); 
//$temp = $target_path . basename($_FILES['file']['name']); 
//$extension = end($temp); 


if (($_FILES["file"]["type"] == "image/gif") 
|| ($_FILES["file"]["type"] == "image/jpeg") 
|| ($_FILES["file"]["type"] == "image/jpg") 
|| ($_FILES["file"]["type"] == "image/pjpeg") 
|| ($_FILES["file"]["type"] == "image/x-png") 
|| ($_FILES["file"]["type"] == "image/png") 
|| ($_FILES["file"]["type"] == "text/plain") 
|| ($_FILES["file"]["type"] == "application/msword") 
|| ($_FILES["file"]["type"] == "application/vnd.ms-excel") 
&& ($_FILES["file"]["size"] < 2000000)) 
    { 
    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["uploadedfile"]["tmp_name"])) 
     { 
     echo $_FILES["file"]["name"] . " already exists. "; 
     } 
    else 
     { 
     move_uploaded_file($_FILES['file']['tmp_name'], $target_path); 
     echo "The file ". basename($_FILES['file']['name']). 
     " has been uploaded"; 
    } 
    } 
    } 
else 
    { 
    echo "Invalid file"; 
    } 
?> 



<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<title>upload</title> 
</head> 

<body> 

<form action="upload.php" method="post" 
enctype="multipart/form-data"> 
<label for="file">Filename:</label> 
<input type="file" name="file" id="file"><br> 
<input type="submit" name="submit" value="Submit"> 
</form> 

</body> 
</html> 
+0

print_r($ _ FILES [“file”])将是有用的而不是无效的文件。 –

+0

你的代码正在工作只是从20000增加上传限制或上传小图片 –

+0

你正在用'$ target_path覆盖'$ target_path'。 basename($ _FILES ['file'] ['name']);'。没有文件上传时,$ _FILES将为空,因此会触发错误。另外,为了检测扩展,我更喜欢'pathinfo()'。 – Ruben

回答

0

您的代码没问题。您收到'无效文件'消息,因为您的文件大小> = 20000或您的文件类型与您的过滤器不匹配。只需增加php代码中的文件大小,并确保只上传“gif”,“jpeg”,“jpg”或“png”文件。

+0

我通过避免数组方法(坏习惯是我知道)得到了它的工作,但是现在我想要的是它的工作。然而,我现在不能上传word文件或任何这样的jpegs等......任何想法?我将在上面编辑我的代码。 – user3225430

0

您的ms-word .doc文件将完全加载您的代码。但是,如果您尝试上载.docx文件(即ms-word 2007以后的默认文件),那么您的代码将无法工作。 要使它工作使用.. “应用/ vnd.openxmlformats-officedocument.wordprocessingml.document” 除了 “应用程序/ msword”

而且在你的代码更改

如果(file_exists(”上传/”。$ _FILES [ “UploadedFile的”] [ “tmp_name的值”))

   to 

如果(file_exists( “上传/”。$ _FILES [ “文件”] [ “名称”]))

+0

谢谢Bobby!一旦我完成关于设计思维和挑战的这份报告并回复给您,我会尽力。有时我喜欢编程:/ – user3225430