上载文件及;php中的docx文件不工作
我有一个代码,这是上传两个文件(图像和文本)在两个不同的文件夹上载文件及;php中的docx文件不工作,php,file-upload,docx,doc,Php,File Upload,Docx,Doc,我有一个代码,这是上传两个文件(图像和文本)在两个不同的文件夹 $file_path = "users/".$uname."/dp/"; $file_path2 = "users/".$uname."/resume/"; $q=mkdir("users/".$uname."/dp/", 0777, $recursive=true); $r=mkdir("users/".$uname."/resume/", 0777, $recur
$file_path = "users/".$uname."/dp/";
$file_path2 = "users/".$uname."/resume/";
$q=mkdir("users/".$uname."/dp/", 0777, $recursive=true);
$r=mkdir("users/".$uname."/resume/", 0777, $recursive=true);
if($q && $r)
{
$targate = $file_path.basename($_FILES['dp']['name']);
//echo $targate ;die;
if ((($_FILES['dp']["type"] == "image/gif") || ($_FILES['dp']["type"] == "image/jpeg") || ($_FILES['dp']["type"] == "image/png") ||
($_FILES['dp']["type"] == "image/jpg")) && ($_FILES['dp']["size"] < 20000))
{
if ($_FILES['dp']["error"] > 0)
{
echo "Return Code: " . $_FILES['dp']["error"] . " ";
}
else
{
move_uploaded_file($_FILES['dp']["tmp_name"], $targate);
}
}
else
{
//echo "Invalid file";
}
$targate2 = $file_path2.basename($_FILES['resume']['name']);
//echo $targate2 ;die;
if ((($_FILES["resume"]["type"] == "text/plain") || ($_FILES["resume"]["type"] == "application/msword")
|| ($_FILES["resume"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")) && $_FILES['resume']["size"] < 20000)
{
if ($_FILES['resume']["error"] > 0)
{
echo "Return Code: " . $_FILES['resume']["error"] . " ";
}
else
{
move_uploaded_file($_FILES['resume']["tmp_name"], $targate2);
}
}
else
{
//echo "Invalid file";
}
echo "success";die;
}
else{ echo "fail";die;}
$file_path=“users/”$uname./dp/”;
$file_path2=“users/”$uname./resume/”;
$q=mkdir(“用户/”$uname./dp/”,0777,$recursive=true);
$r=mkdir(“用户/”$uname./resume/”,0777,$recursive=true);
如果($q&&r)
{
$target=$file\u path.basename($\u FILES['dp']['name']);
//回声$targate;死亡;
如果($_文件['dp'][“type”]==“image/gif”)| |($_文件['dp'][“type”]==“image/jpeg”)| |($_文件['dp'][“type”]==“image/png”)| |
($_文件['dp'][“type”]==“image/jpg”)&&($_文件['dp'][“size”]<20000))
{
如果($\u文件['dp'][“错误”]>0)
{
回显“返回代码:”.$\u文件['dp'][“错误”]”;
}
其他的
{
移动上传的文件($文件['dp'][“tmp\u名称”],$target);
}
}
其他的
{
//回显“无效文件”;
}
$target2=$file_path2.basename($_FILES['resume']['name']);
//echo$target2;死亡;
如果(($_文件[“恢复”][“类型”]=“文本/普通”)| |($_文件[“恢复”][“类型”]=“应用程序/msword”)
||($\u文件[“resume”][“type”]=“application/vnd.openxmlformats officedocument.wordprocessingml.document”)&&&$\u文件[“resume”][“size”]<20000)
{
如果($_文件['resume'][“error”]>0)
{
回显“返回代码:.”文件['resume'][“error”]。“”;
}
其他的
{
移动上传的文件($文件['resume'][“tmp\U名称”],$Target2);
}
}
其他的
{
//回显“无效文件”;
}
呼应“成功”;死亡;
}
否则{echo“fail”;die;}
对于所有类型的图像,它都可以正常工作。但是如果是文本文件(doc和docx文件)它会打印成功,但只上传图像文件
当我替换这个的时候
if ((($_FILES["resume"]["type"] == "text/plain")
|| ($_FILES["resume"]["type"] == "application/msword")
|| ($_FILES["resume"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")) && $_FILES['resume']["size"] < 20000)
if(($_文件[“resume”][“type”]=“text/plain”)
||($_文件[“恢复”][“类型”]=“应用程序/msword”)
||($\u文件[“resume”][“type”]=“application/vnd.openxmlformats officedocument.wordprocessingml.document”)&&&$\u文件[“resume”][“size”]<20000)
条件
if (($_FILES["resume"]["type"] == "text/plain")
&& $_FILES['resume']["size"] < 20000)
if($_文件[“resume”][“type”]=“text/plain”)
&&$\u文件['resume'][“大小”]<20000)
这适用于.txt
有什么问题?我哪里做错了?步骤1
创建一个名为index.html的html文件,并将以下代码粘贴到其中
<html>
<body>
<form enctype="multipart/form-data" method="POST" action="upload.php">This is the code for html:
<table border="0">
<tbody>
<tr>
<td align="left">File:</td>
<td><input accept="doc/docx" name="filename" size="40" type="file" /></td>
</tr>
<tr>
<td><input name="Upload" type="submit" value="Upload" /></td>
</tr>
</tbody></table>
</form>
</body>
</html>
这是html的代码:
文件:
步骤2
创建一个名为upload.PHP的PHP文件并粘贴以下代码
<?php
//if we clicked on Upload button
if($_POST['Upload'] == 'Upload')
{
//make the allowed extensions
$goodExtensions = array(
'.doc',
'.docx',
);
$error='';
//set the current directory where you wanna upload the doc/docx files
$uploaddir = './ ';
$name = $_FILES['filename']['name'];//get the name of the file that will be uploaded
$min_filesize=10;//set up a minimum file size(a doc/docx can't be lower then 10 bytes)
$stem=substr($name,0,strpos($name,'.'));
//take the file extension
$extension = substr($name, strpos($name,'.'), strlen($name)-1);
//verify if the file extension is doc or docx
if(!in_array($extension,$goodExtensions))
$error.='Extension not allowed<br>';
echo "<span> </span>"; //verify if the file size of the file being uploaded is greater then 1
if(filesize($_FILES['filename']['tmp_name']) < $min_filesize)
$error.='File size too small<br>'."\n";
$uploadfile = $uploaddir . $stem.$extension;
$filename=$stem.$extension;
if ($error=='')
{
//upload the file to
if (move_uploaded_file($_FILES['filename']['tmp_name'], $uploadfile)) {
echo 'File Uploaded. Thank You.';
}
}
else echo $error;
}
?>
//以上答案是正确的,但为了完美,需要一些细微的改变。。。。。。。
//thanx。。。
//它会将特定文档存储在一个文件夹中
尝试为Word文件回显$\u FILES[“resume”][“type”]
的值。它可能与您正在检查的MIME不同。否则{//echo“Invalid file”}取消对此的注释,tryIt将始终打印成功,因为它不处于任何状态。实际上它显示为无效文件。但是当我回显$\u FILES[“resume”][“type”]
的值时,它会显示application/msword for doc和application/vnd.openxmlformats-officedocument.wordprocessingml.document for docx不走运。。问题仍然存在。。
// Above answer is right but for perfection need some slight changes.......
// thanx...
//It will store particular document in a folder
<?php
//if we clicked on Upload button
if($_POST['Upload'] == 'Upload')
{
//make the allowed extensions
$goodExtensions = array( '.doc', '.docx',);
$error='';
//set the current directory where you wanna upload the doc/docx files
$uploaddir = 'upload./ ';
$name = $_FILES['filename']['name'];//get the name of the file that will be uploaded
$min_filesize=10;//set up a minimum file size(a doc/docx can't be lower then 10 bytes)
$stem=substr($name,0,strpos($name,'.'));
//take the file extension
$extension = substr($name, strpos($name,'.'), strlen($name)-1);
//verify if the file extension is doc or docx
if(!in_array($extension,$goodExtensions))
$error.='Extension not allowed<br>';
echo "<span> </span>"; //verify if the file size of the file being uploaded is greater then 10
if(filesize($_FILES['filename']['tmp_name']) < $min_filesize)
$error.='File size too small<br>'."\n";
else
$uploadfile = $uploaddir . $stem.$extension;
$filename=$stem.$extension;
if ($error=='')
{
//upload the file to
if (move_uploaded_file($_FILES['filename']['tmp_name'], $uploadfile)) {
echo 'File Uploaded. Thank You.';
}
}
else echo $error;
}
?>