Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 上传一个文件,确保它';这是一个pdf文件,然后使用Swiftmailer_Php_Html_Forms_File Io_Swiftmailer - Fatal编程技术网

Php 上传一个文件,确保它';这是一个pdf文件,然后使用Swiftmailer

Php 上传一个文件,确保它';这是一个pdf文件,然后使用Swiftmailer,php,html,forms,file-io,swiftmailer,Php,Html,Forms,File Io,Swiftmailer,我已经为一些代码挣扎了一段时间了。我有一个html表单,看起来像这样(我知道它非常坚固,只是试着让它工作): 您不想检查文件扩展名,因为它可能被操纵 相反,方法是检查MIME类型。参见(示例2)。 PDF文件的MIME类型为application/PDF 另请注意以下评论: 这个答案意味着依赖内容类型是安全的。这个 检查文件是否包含有效内容的最佳方法是对其进行解析 使用特定的解析器(例如图像:)。如果没有 然后不要接受可能有害的文件(例如.*.php[使用 保存文件的白名单-不是黑名单]) 您不

我已经为一些代码挣扎了一段时间了。我有一个html表单,看起来像这样(我知道它非常坚固,只是试着让它工作):


您不想检查文件扩展名,因为它可能被操纵

相反,方法是检查MIME类型。参见(示例2)。 PDF文件的MIME类型为
application/PDF

另请注意以下评论:

这个答案意味着依赖内容类型是安全的。这个 检查文件是否包含有效内容的最佳方法是对其进行解析 使用特定的解析器(例如图像:)。如果没有 然后不要接受可能有害的文件(例如.*.php[使用 保存文件的白名单-不是黑名单])


您不想检查文件扩展名,因为它可能被操纵

相反,方法是检查MIME类型。参见(示例2)。 PDF文件的MIME类型为
application/PDF

另请注意以下评论:

这个答案意味着依赖内容类型是安全的。这个 检查文件是否包含有效内容的最佳方法是对其进行解析 使用特定的解析器(例如图像:)。如果没有 然后不要接受可能有害的文件(例如.*.php[使用 保存文件的白名单-不是黑名单])


您的
上缺少
enctype=“multipart/form data”
,这也将在上载开始之前终止上载。

您的
上缺少
enctype=“multipart/form data”
,这也会在上传开始之前终止上传。

如何检查pdf的mime类型?这可以由swiftmailer处理吗?我在我的帖子中添加了PDF MIME类型。另外,请检查我发布的链接。
$\u FILES[file][type]
中提供的MIME类型与文件扩展名一样容易被篡改。(事实上,大多数用户代理只是将文件扩展名映射到媒体类型。)@mario-在进行检查之前,我是否需要将文件上载到服务器?看起来我需要在检查MIME类型之前执行
finfou_open
,这需要服务器上的绝对路径。不,您需要检查上传文件的MIME类型。请参阅有关如何完成此操作的发布链接。如何检查pdf的mime类型?这可以由swiftmailer处理吗?我在我的帖子中添加了PDF MIME类型。另外,请检查我发布的链接。
$\u FILES[file][type]
中提供的MIME类型与文件扩展名一样容易被篡改。(事实上,大多数用户代理只是将文件扩展名映射到媒体类型。)@mario-在进行检查之前,我是否需要将文件上载到服务器?看起来我需要在检查MIME类型之前执行
finfou_open
,这需要服务器上的绝对路径。不,您需要检查上传文件的MIME类型。请参阅有关如何完成此操作的已发布链接。
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<table>

<tr>
    <td> Name </td>
    <td> <input type="text" name="name" size="30"></td>
</tr>
<tr>
    <td> Email </td> 
    <td> <input type="text" name="email" size="30"></td>
</tr>

<tr> <td>&nbsp;</td><td>&nbsp;</td> </tr>

<tr>
    <td> Title of Article &nbsp;&nbsp;</td>
    <td> <input type="text" name="title" size="40"></td>
</tr>
<tr>
    <td> Course </td>
    <td>
    <select name="course">
    <option>CEG - Computer Architecture I</option>
    <option>BIO - General Biology I</option>
    <option>BIO - General Biology II</option>
    <option>BIO - Introduction to Human Genetics</option>
    </select>
    </td>
</tr>
<tr>
    <td> File </td>
    <td> <input type="file" name="file" id="file"></td>
</tr>

<tr> <td>&nbsp;</td><td>&nbsp;</td> </tr>

<tr>
    <td valign="top"> Additional Info </td>
    <td><textarea rows="3" cols="40" name="info"></textarea></td>
</tr>

</table>

<input type="checkbox" name="agree"> I agree to the points outlined above and am willing to submit my article <br>
<    input type="submit" name="submit" value="Submit">

</form>
if (isset($_POST['submit'])) {
    if ( !isset($_POST['agree'])   || 
         !isset($_POST['name'])    || 
         !isset($_POST['email'])   ||  
         !isset($_POST['title'])   ||  
         !isset($_POST['course'])  ||  
         !isset($_POST['file'])) {
        echo 'Please complete all required fields<br>';
    } else {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $title = $_POST['title'];
        $course = $_POST['course'];
        $file = $_POST['file'];

        $message  = "Name: ".$name."\n";
        $message .= "Email: ".$email."\n\n";
        $message .= "Title of Article: ".$title."\n";
        $message .= "Program: ".$course."\n\n";
        $message .= "Additional Info: ".$info;

        if ( !preg_match("/.pdf$/", $file) ) {
            echo 'Article must be in pdf format<br>';
            exit;
        }

        require_once 'include/swift_required.php';

        $transport = Swift_MailTransport::newInstance();
        $mailer = Swift_Mailer::newInstance($transport);
        $swift = Swift_Message::newInstance()
            ->setSubject('New Institutum Submission')
            ->setFrom(array($email => $name))
            ->setTo(array('sub@f33r.com'))
            ->setBody($message)
            ->attach(Swift_Attachment::fromPath($file));

        $result = $mailer->send($swift);
        if ($result) {  echo 'Article sent. Please allow required amount of time to review submission.\n';
                        echo 'You will be contacted by email when we go over your submission.'; }
        else {          echo 'Message failed'; }
    }
}
        # Create the message
        # ----------------------------------------------------------------
        $name = $_POST['name'];
        $email = $_POST['email'];
        $title = $_POST['title'];
        $course = $_POST['course'];
        $file = $_POST['file'];

        $message  = "Name: ".$name."\n";
        $message .= "Email: ".$email."\n\n";
        $message .= "Title of Article: ".$title."\n";
        $message .= "Program: ".$course."\n\n";
        $message .= "Additional Info: ".$info;

        # Upload temporary files
        # ----------------------------------------------------------------
        $uploaddir = '/home/public/uploads/';
        $uploadfile = $uploaddir . basename($_FILES['file']['name']);
        if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile) == false) {
            echo 'Could not move file';
            exit;
        }

        if ($_FILES['file']['type'] != "application/pdf") {
            echo 'Not a pdf file';
            unlink($uploadfile);
            exit;
        }