2013-10-10 48 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

相關問題