2014-02-11 86 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