上传文件,确保它是 pdf,然后使用 Swiftmailer

发布于 2024-11-28 03:34:18 字数 4770 浏览 1 评论 0原文

我已经在一些代码上苦苦挣扎了一段时间了。我有一个看起来像这样的html表单(我知道它真的很坚固。只是想让它工作):

<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('[email protected]'))
            ->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'; }
    }
}

我正在使用正则表达式来检查它是否是pdf 文件,但我怀疑这是正确的处理方式(因为有人可以用 pdf 扩展名重命名文件)。另外,我还没有实现暂时上传文件的方法。这是假设我需要在使用 swiftmailer 将文件添加为附件之前在本地上传文件(对吗?)。

我至少走在正确的轨道上吗?我从来没有以这种方式真正接触过 PHP。

需要一些调试:

        # 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;
        }

I've been struggling with some code for a while now. I have an html form that looks like this (It's really rugged, I know. Just trying to get it to work):

<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> </td><td> </td> </tr>

<tr>
    <td> Title of Article   </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> </td><td> </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>

This form calls itself, this is what I have up to now:

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('[email protected]'))
            ->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'; }
    }
}

I'm using regex to check if it s a pdf file, but I doubt that's the correct way of doing things (as someone could just rename a file with a pdf extension). Also, I haven't implemented a way to upload the file temporarily yet. This is assuming I need to upload the file locally before using swiftmailer to add it as an attachment (right?).

Am I at least on the right track? I've never really dealt with PHP in this way.

Needs some debugging:

        # 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;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘初心 2024-12-05 03:34:18

您的

上缺少 enctype="multipart/form-data",这也会在上传开始之前就终止上传。

You're missing enctype="multipart/form-data" on your <form>, which will also kill the upload before it even has a chance to get started.

木格 2024-12-05 03:34:18

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

相反,正确的方法是检查 MIME 类型。请参阅此处(示例 2)。
PDF 文件的 MIME 类型是 application/pdf

另请注意 Saxoier 的评论:

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

You don't want to check the file-extension as it could be manipulated.

Instead, the way to go is checking for the MIME-Type. See here (example 2).
The MIME-type of a PDF-file is application/pdf

Also note Saxoier's comment:

This answer implies that it is safe to rely on the Content-Type. The
best way to check a file if it contains valid content is to parse it
with the specific parser (e.g. images: GD). If none is available
then do not accept files who can be harmful (e.g. *.php [use a
whitelist of save files - not a blacklist]).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文