2013-10-10 42 views
0

所以我正在编辑一些已经存在的代码,我不知道如何添加额外的扩展名,以便PDF以外的文件可以上传任何线索?我想添加jpeg,docx,doc,xls和wps。添加更多的文件扩展名到现有的代码

<?php 

$lastname=$_POST['mylastname']; 

$firstname=$_POST['myfirstname']; 

$asuid=$_POST['#']; 

$ftype=$_POST['ftype']; 

$dkServerConn = mysql_connect("#", "#", "#") or die("no way"); 
mysql_select_db("#", $dkServerConn) or die("Cannot connect to the DB!"); 



$sql1 = "SELECT * FROM scholnumber"; 
$dkResultSet1 = mysql_query($sql1,$dkServerConn) or die(mysql_error()); 
$checkIt=0; 

while ($dkROWrecord1 = mysql_fetch_array($dkResultSet1,MYSQL_BOTH)) 


{ 
$checkIt=$dkROWrecord1['filenumber']; 
} 

$checkIt2=$checkIt+1; 


$sql2="Update scholnumber set filenumber='".$checkIt2."'"; 
$dkResultSet2 = mysql_query($sql2,$dkServerConn) or die(mysql_error()); 


echo "<html> 
     <head> 
      <title>Uploading Information</title> 
       <style> body { font-family:arial; font-size:14px } </style> 
     </head> 
      <body><table cellpadding='5' align='center' width='55%'><tr><td><p>&nbsp;</p> 
       <img src='#'><br /> 
       <img src='#'><br /> 
     <span style='font-family:arial;font-size:14pt'>&nbsp; &nbsp; &nbsp; 2013-2014 Privately Funded Scholarship Application</span> 
      <h3><br/>"; 


// File validation --> 

$allowedExts = array("pdf","docx","doc","wps"); 

$extension = end(explode(".", $_FILES["file"]["name"])); 


if (($_FILES["file"]["type"] == "application/pdf") 
&& in_array($extension, $allowedExts)){ 
    if ($_FILES["file"]["error"] > 0){ 
     echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; 
    } 
    else{ 
     echo "<p />Submission successful.<p /> 

    Your submission has been received. If you have loaded all of your documents, you can close this browser. Your scholarship application is complete.<p />"; 

     echo "Upload: " . $_FILES["file"]["name"] . "<br>"; 
     echo "Type: " . $_FILES["file"]["type"] . "<br>"; 


     if (file_exists("upload/" . $_FILES["file"]["name"])){ 
      echo $_FILES["file"]["name"] . " already exists. "; 
     } 
     else{ 
      $ext = substr($_FILES['file']['name'], strpos($_FILES['file']['name'],'.'), strlen($_FILES['file']['name'])-1);  
      $docName = $checkIt.$ext; 
      move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $docName); 
      // echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; 
     } 
    } 
} 
elseif($_FILES["file"]["type"] == "application/pdf"){ 

} 
else{ 
echo "<hr> <p /> >>>>>> INVALID FILE <<<<<<<< <p />&nbsp;<p />"; 
} 

// End of the validation 


$sql3="insert into scholfile (filename, lastname, firstname, asuid, ftype) values ('$checkIt', '$lastname', '$firstname', '$asuid', '$ftype')"; 
$dkResultSet3 = mysql_query($sql3,$dkServerConn) or die(mysql_error()); 


$sql6="select * from scholarships where asuid='".$asuid."'"; 
$dkResultSet6 = mysql_query($sql6,$dkServerConn) or die(mysql_error()); 


while ($dkROWrecord6 = mysql_fetch_array($dkResultSet6,MYSQL_BOTH)) 
      { 
      $email=$dkROWrecord6['email']; 
      } 

$from="Scholarships"; 
$fromem="[email protected]#.edu"; 


$subjectStudent = "Thank you for your supporting documentation."; 
$messageStudent="Hello, ".$firstname. " -<p /> 

We have received your ".$ftype.". <p /> 

<hr /> 

If you have any questions, please contact the Financial Aid and Scholarships Office at 870-972-2310 or reply to this email. 
<p /> 

Thank you for your submission.<br />Financial Aid and Scholarships Office"; 


//Email Information 

$headers = "MIME-Version: 1.0\r\n"; 
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
$headers .= "To:" .$firstname. "<" . $email. ">\r\n"; 
$headers .= "From:" . $from . "<". $fromem . ">\r\n"; 



mail($email, $subjectStudent, $messageStudent, $headers); 

?> 

<p />&nbsp; <p /> 
If you have another document to submit, please <a href='fileupload.php'>click here.</a><p /> 

Thank you!<p /></body></html> 

回答

1

我不确定我的回答是否正确,因为我自己还没有这样做,但是这里有些东西供您尝试。 (未经测试)

您的代码限制了这里上传文件的文件类型:

if (($_FILES["file"]["type"] == "application/pdf") && in_array($extension, $allowedExts)){ 

更改代码的文件验证部分:

// File validation --> 

$allowedExts = array("pdf","docx","doc","wps","jpg"); //Added jpg 

//Get Filename Extension into var (your code - unchanged) 
$extension = end(explode(".", $_FILES["file"]["name"])); 

//Create array of acceptable file types: 
// Sources: (1) http://filext.com/file-extension/DOC and (2) http://php.net/manual/en/function.mime-content-type.php 
$doctype = array("application/msword","application/doc","appl/text","application/vnd.msword","application/vnd.ms-word","application/winword","application/word","application/x-msw6","application/x-msword"); 
$xltype = array("application/vnd.ms-excel","application/msexcel","application/x-msexcel","application/x-ms-excel","application/vnd.ms-excel","application/x-excel","application/x-dos_ms_excel","application/xls"); 
$jpgtype = array("image/jpeg","image/jpg","image/jp_","application/jpg","application/x-jpg","image/pjpeg","image/pipeg","image/vnd.swiftview-jpeg","image/x-xbitmap"); 
$wpstype = array("application/vnd.ms-works","application/x-msworks-wp","zz-application/zz-winassoc-wps","text/plain"); 
//Combine them into one array: 
$allowedFT = array_merge($doctype, $xltype, $jpgtype, $wpstype); 

//Get this file's file type into var 
$ft = $_FILES["file"]["type"]; 

//NOW DO THE BIG TEST 
if (in_array($ft, $allowedFT) && in_array($extension, $allowedExts)){ 
+0

让我试试,我会及时向大家发布,感谢您的时间:) –

+0

+1:似乎是正确的。你击败了我。但是我已经开始了,所以我刚刚完成了它。你的看起来更像是一个解决方案。我只是一个向导。 – Touch

+0

顺便问一下,你们是否知道lynda.com教授PHP代码的地方?我想要一个像在线课程,大学或大学更正式的东西,你们有什么线索吗? –

1

我不知道关于他人,但从我的角度来看,你的代码似乎只允许pdf上传。

首先,我会添加到允许扩展阵列:

$allowedExts = array("pdf","docx","doc","wps", "jpeg", "xls"); 

我将随后改变这一行:

if (($_FILES["file"]["type"] == "application/pdf") 

到:

$allowedMIMETypes = array(

    "application/pdf", //for pdf 

    "application/vnd.openxmlformats-officedocument.wordprocessingml.document", //for docx 

    "application/msword", //for doc 

    "application/vnd.ms-works", //for wps, I think you should also paste the other in the link to 

    "image/jpeg", //for jpeg, again, there are other mime-types to add to 

    "application/excel", //for xls, again there are other mime-types to add from the sources 

); 

//then check if the type is in the array 
if (in array($_FILES["file"]["type"],$allowedMIMETypes)) { 

的源的文件类型:

http://hul.harvard.edu/ois/systems/wax/wax-public-help/mimetypes.htm

What is a correct mime type for docx, pptx etc?

http://lwp.interglacial.com/appc_01.htm

http://dotwhat.net/wps/32

+0

干得好,触摸+1 – gibberish

相关问题