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="scholarships@#.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>
4

2 回答 2

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

docx、pptx 等的正确 mime 类型是什么?

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

http://dotwhat.net/wps/32

于 2013-10-10T20:06:06.380 回答
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)){
于 2013-10-10T19:54:20.337 回答