简体   繁体   English

如何允许用户将文件上传到网站以便我可以接收它们?

[英]How to allow users to upload files to the website so that I can receive them?

I am getting a Server error when I am trying the following code on my localhost.当我在本地主机上尝试以下代码时出现服务器错误。 Please let me know if there's something wrong with my code如果我的代码有问题,请告诉我

here's my HTML code:这是我的 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>

Here's my PHP code这是我的 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!";
}


} 

this would work:这会起作用:

<?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!";}

} 

?>

it seems the error was the question mark as below似乎错误是下面的问号

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何允许我的网站用户上传媒体文件? - How can I allow users of my website to upload media files? 如何显示已上传到网站服务器上文件夹中的文件,以便用户下载? - How can I display the files I have uploaded to a folder in my server on my website so that users can download them? 如何轻松允许用户上传多个文件? - How can I easily allow users to upload multiple files? 如何使人们可以将文件上传到我的网站并显示它们? - How to make so people can upload files to my website and display them? 我该如何阻止网站中的某些网页供用户使用,以使他们无法直接从浏览器中打开它们? - How can i block some webpages in my website for users, so that they can not open them directly from browser? 如何允许用户上传多个文本文件,然后将它们保存为字符串并显示(在PHP中)? - How can I allow a user to upload multiple text files, then save them to a string and display them (in PHP)? 如何允许 iOS 用户通过我网站上的 html 输入字段上传文档? - How do I allow iOS users to upload a document via an html input field on my website? 如何安全地允许Web用户创建文件? - How can I securely allow web users to create files? PHP:允许为用户上传.php文件,如何防止它们运行? - PHP: allowing upload of .php files for users, how do I prevent them from running? 如何让php创建与创建它们的文件具有相同所有权的文件? - How can I allow php to create files with the same ownership as the files that created them?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM