我现在有一个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