我现在有一个php文件,可以将文件上传到我的网络服务器。目前它将上传诸如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 :(得分:0)
您的代码没问题。您收到“文件无效”消息,因为您的文件大小&gt; = 20000或您的文件类型与您的过滤器不匹配。只需增加php代码中的文件大小,并确保只上传“gif”,“jpeg”,“jpg”或“png”文件。
答案 1 :(得分:0)
您的ms-word .doc文件将与您的代码完美地加载。但是,如果您尝试上传.docx文件(即ms-word 2007以后的默认文件),那么您的代码将无效。 为了使它工作使用.. “应用程序/ vnd.openxmlformats-officedocument.wordprocessingml.document” 此外 “应用程序/ msword”
同样在您的代码更改中
if(file_exists(“upload /”。$ _FILES [“uploadedfile”] [“tmp_name”]))
to
if(file_exists(“upload /”。$ _FILES [“file”] [“name”]))
Happy computing