【问题标题】:How to allow users to upload files to the website so that I can receive them?如何允许用户将文件上传到网站以便我可以接收它们?
【发布时间】:2020-11-01 08:14:11
【问题描述】:

我在本地主机上尝试以下代码时遇到服务器错误。 如果我的代码有问题,请告诉我

这是我的 HTML 代码:

<section class= "box3">
    <h4>Become a contributor</h4>
    <div class="lines"></div>
    <form class="contact-form" action="uploads.php" method= "POST" enctype= "multipart/form-data"> 
        <input type= "file" name="file" class= "contact-form-text" >
        <button type="submit" class="contact-form-btn" name="submit">Upload</button>
    </form>
</section>

这是我的 PHP 代码

<?php

if (isset($_POST['submit'])) {
    $file = $_FILES ['file'];

    $fileName = $_FILES ['file'] ['name'];
    $fileTmpName = $_FILES ['file'] ['tmp_name'];
    $fileSize = $_FILES ['file'] ['size'];
    $fileError = $_FILES ['file'] ['error'];
    $fileType = $_FILES ['file'] ['type'];

    $fileExt = explode ('.',$fileName);
  $fileActualExt = strtolower (end($fileExt));

   $allowed = array ('jpg', 'jpeg', 'png', 'pdf', 'doc');

   if (in_array($fileActualExt, $allowed)) {
       if ($fileError === 0) {
           if ($fileSize < 10000000) {
            $fileNameNew = uniqid ('', true).".".$fileActualExt;
          $fileDestination = 'upload/'.$fileNameNew;
          move_uploaded_file($fileTmpName, $fileDestination);
          header ("Location : Contact.html?uploadedsuccessfully");
        } else {
            echo "Your file is too big!";
        }  

    } else {
        echo "There was an error uploading your file!";
    } 
} else {
    echo "You cannot upload files of this type!";
}


} 

【问题讨论】:

  • I am getting a Server error ... 哪一个?

标签: php html forms file-upload


【解决方案1】:

这会起作用:

<?php

if (isset($_POST['submit'])) {
    $file = $_FILES ['file'];

    $fileName = $_FILES ['file'] ['name'];
    $fileTmpName = $_FILES ['file'] ['tmp_name'];
    $fileSize = $_FILES ['file'] ['size'];
    $fileError = $_FILES ['file'] ['error'];
    $fileType = $_FILES ['file'] ['type'];

    $fileExt = explode ('.',$fileName);
    $fileActualExt = strtolower (end($fileExt));

    $allowed = array ('jpg', 'jpeg', 'png', 'pdf', 'doc');

    if (in_array($fileActualExt, $allowed)) {
        if ($fileError === 0) {
            if ($fileSize < 10000000) {
                $fileNameNew = uniqid ('', true).".".$fileActualExt;
                $fileDestination = 'upload/'.$fileNameNew;
                move_uploaded_file($fileTmpName, $fileDestination);
                header ("Location Contact.html uploadedsuccessfully");
            } else {
                echo "Your file is too big!";
            }  
        } else { echo "There was an error uploading your file!"; } 
    } else { echo "You cannot upload files of this type!";}

} 

?>

似乎错误是下面的问号

header ("Location Contact.html?uploadedsuccessfully");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-05
    • 2013-12-11
    • 2019-05-27
    • 1970-01-01
    • 2010-11-01
    • 2016-01-29
    • 1970-01-01
    • 2013-07-02
    相关资源
    最近更新 更多