我現在有一個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>
print_r($ _ FILES [「file」])將是有用的而不是無效的文件。 –
你的代碼正在工作只是從20000增加上傳限制或上傳小圖片 –
你正在用'$ target_path覆蓋'$ target_path'。 basename($ _FILES ['file'] ['name']);'。沒有文件上傳時,$ _FILES將爲空,因此會觸發錯誤。另外,爲了檢測擴展,我更喜歡'pathinfo()'。 – Ruben