PHP 无法从 HTML 表单上传文件

发布于 2024-11-28 12:25:51 字数 1227 浏览 1 评论 0原文

这就是我处理表单的方式:

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

最终产品希望发送一封包含文件作为附件的电子邮件。现在我失败了,并收到我内置的“无法移动文件”消息。有明显的原因吗? $file 是我从 HTML 文件对话框中获得的内容 (input type="file")

This is how I handle my form:

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

The end product is hopefully sending an email with the file as an attachment. Right now I'm failing and getting the "Could not move file" message I built in. Is there an obvious reason why? $file is what I get from a file dialog in HTML (input type="file")

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

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

发布评论

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

评论(2

儭儭莪哋寶赑 2024-12-05 12:25:52

两件事:
1. 表单是否设置为:

<form method="POST" enctype="multipart/form-data" action="INSERT ACTION">

2. 您将文件发布到的文件夹是否设置为 777?

Two things:
1. Is the form set to:

<form method="POST" enctype="multipart/form-data" action="INSERT ACTION">

2. Is the folder your posting the file to, is it set to 777?

撩心不撩汉 2024-12-05 12:25:52

您的表单需要设置适当的 enctype 属性,即

<form enctype="multipart/form-data" method="post" action=... >

更新

一些建议...

  1. 在移动上传的文件之前检查文件类型并执行任何其他常规验证。那应该是你的最后一步。
  2. 现在记不清了,但我认为您不会在 $_POST['file'] 值中得到任何有用的(或根本没有)任何有用的东西。使用 $_FILES 数组来存储所有上传的文件数据。

Your form needs to have the appropriate enctype attribute set, ie

<form enctype="multipart/form-data" method="post" action=... >

Update

A couple of suggestions...

  1. Check the file type and do any other general validation before you move the uploaded file. That should be your last step.
  2. Can't remember exactly right now but I don't think you'll get anything useful (or at all) in the $_POST['file'] value. Use the $_FILES array for all uploaded file data.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文