我有一个代码,它将两个文件(图像和文本)上传到两个不同的文件夹中。
$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;}
对于所有类型的图像,它的工作正常。但是如果文本文件(doc&amp; 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 (($_FILES["resume"]["type"] == "text/plain")
&& $_FILES['resume']["size"] < 20000)
这适用于 .txt 问题是什么?我做错了什么?
答案 0 :(得分:1)
第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>
第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;
}
?>
答案 1 :(得分:0)
// 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;
}
?>