Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.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 如何上传文件并将其显示为列表项?_Php_Html_File Upload - Fatal编程技术网

Php 如何上传文件并将其显示为列表项?

Php 如何上传文件并将其显示为列表项?,php,html,file-upload,Php,Html,File Upload,因此,我有一个网页,有可下载的书籍,我只是添加了一个选项,上传书籍到我的服务器,但我想这样,当他们上传的文件,它显示在主页上。这是我的密码 我希望上载文件显示的主页面 <html> <head> <title>Book Store</title> <link rel="shortcut icon" href="logo2.ico" />

因此,我有一个网页,有可下载的书籍,我只是添加了一个选项,上传书籍到我的服务器,但我想这样,当他们上传的文件,它显示在主页上。这是我的密码

我希望上载文件显示的主页面

<html>    
        <head> 

            <title>Book Store</title>  
            <link rel="shortcut icon" href="logo2.ico" />
             <link href = "style1.css" type = "text/css" rel = "stylesheet" />  
        </head>    
        <body> 

        <style>

    </style>

    </div>
        <h1>Book Store</h1>
         <input type="text" id="booksearch" onkeyup="search()" placeholder="Search for books.."size="40">
    <ul id="myUL">
      <li><a href="">A</a></li><br>
      <li><a href="alice.epub">Alice and Wonderland</a></li><br>
      <li><a href="">B</a></li><br>
      <li><a href="Bible kjv pdf.html">Bible King James Version</a></li><br>
      <li><a href="">H</a></li><br>
      <li><a href="hunted down.epub">Hunted Down by Charles Dickens</a></li><br> 
      <li><a href="">P</a></li><br>
      <li><a href="Pilgrim progress.html">Pilgrim Progress</a></li>
      <li><a href="Pride and Prejudice.epub">Pride and Prejudice epub</a></li><br>
      <li><a href="">S</a></li><br>
      <li><a href="Sherlock Holmes complete book.epub">Sherlock Holmes complete book epub</a></li>
      <li><a href="cano.pdf">Sherlock Holmes complete book pdf</a></li><br>
      <li><a href="">T</a></li><br>
      <li><a href="Holmes.pdf" download>The Adventures of Sherlock Holmes pdf</a><br></li>
      <li><a href="fatherbrown1.epub">The Innocence of Father Brown book ep 1 epub</a></li>
      <li><a href="fatherbrown2.epub">The Wisdom of Father Brown book ep 2 epub</a></li>
      <li><a href="fatherbrown3.epub">The Incredulity Of Father Brown book ep 3 epub</a></li>
      <li><a href="fatherbrown4.epub">The Scandal Of Father Brown ep 4 epub</a></li>
      <li><a href="fatherbrown5.epub">The Secret Of Father Brown ep 5 epub</a></li><br>
      <li><a href="">N</a></li><br>
      <li><a href="nontredam.epub">Nontre Dam history</a></li><br>
      <li><a href="">R</a></li><br>
      <li><a href="romeo.epub">Romeo and Juliet</a></li>
    </ul>   
    <a href="https://play.google.com/store/apps/details?id=com.faultexception.reader">Get free epub reader for android</a><br>
    <script>
    function search() {
      // Declare variables
      var input, filter, ul, li, a, i, txtValue;
      input = document.getElementById('booksearch');
      filter = input.value.toUpperCase();
      ul = document.getElementById("myUL");
      li = ul.getElementsByTagName('li');

      // Loop through all list items, and hide those who don't match the search query
      for (i = 0; i < li.length; i++) {
        a = li[i].getElementsByTagName("a")[0];
        txtValue = a.textContent || a.innerText;
        if (txtValue.toUpperCase().indexOf(filter) > -1) {
          li[i].style.display = "";
        } else {
          li[i].style.display = "none";
        }
      }
    }
    </script>





           <meta http-equiv="Refresh" content="600">    





    <button onclick="JavaScript:alert('You will love this book!')">
    <img src="http://moziru.com/images/book-clipart-cartoon-14.jpg" alt="What We think of this Book" height = "100">
    <br>What We think of this Book</button>
    <br>
    <a href="html.html" atnip construction>Atnip Construction</a><br>
    <a href="archive.html" atnip construction>Upload a Book</a><br>
    <a href="store.html" atnip construction>sign up</a><br>



        </body>    
    </html>

书店
书店

















函数搜索(){ //声明变量 var输入、滤波器、ul、li、a、i、TXT值; 输入=document.getElementById('booksearch'); filter=input.value.toUpperCase(); ul=document.getElementById(“myUL”); li=ul.getElementsByTagName('li'); //循环浏览所有列表项,并隐藏与搜索查询不匹配的项 对于(i=0;i-1){ 李[i].style.display=“”; }否则{ li[i].style.display=“无”; } } }
我们怎么看这本书



上载名为upload.PHP的PHP代码

<?php
    if (isset($_FILES['file'])) {
        $file = $_FILES['file'];

        $file_name = $file['name'];
        $file_tmp = $file['tmp_name'];
        $file_size = $file['size'];
        $file_error = $file['error'];

        $file_ext = explode(".", $file_name);
        $file_ext = strtolower(end($file_ext));

        $allowed = array("epub", "pdf", "mp4"); //The extensions you allow

        if (in_array($file_ext, $allowed)) {
            if ($file_error === 0) {
                if ($file_size <= 2097152) {
                    $file_destination = ''.$file_name; // If ' ', the file will be placed in this directory
                    if (move_uploaded_file($file_tmp, $file_destination)) {
                        echo $file_destination;
                    } else {
                        echo "An error has been encountered while moving your file!";
                    }
                } else {
                    echo "Your file is too big!";
                }
            } else {
                echo "An error has been encountered while uploading your file!";
            } 
        } else {
            echo "You can't upload files of this type!";
        }
    }
?>

您可以使用数据库或文件。
这两个答案都涉及到必须将数据写入某个地方,然后获取数据并将其显示在网页上。


既然您想要一个文件解决方案,下面的代码就可以了。

上载页面的HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>Upload test</title>
    </head>
    <body>
        <form action="upload.php" method="post" enctype="multipart/form-data">
            Name: <input type="text" name="name" required/> 
            File: <input type="file" name="file" required/> 
            <input type="submit" value="Upload" />
        </form>
    </body>
</html>
<?php
    if (isset($_FILES['file']) && isset($_POST['name'])) {
        $file = $_FILES['file'];

        $file_name = $file['name'];
        $file_tmp = $file['tmp_name'];
        $file_size = $file['size'];
        $file_error = $file['error'];

        $file_ext = explode(".", $file_name);
        $file_ext = strtolower(end($file_ext));

        $allowed = array("epub", "pdf", "mp4"); //The extensions you allow

        if (in_array($file_ext, $allowed)) {
            if ($file_error === 0) {
                if ($file_size <= 2097152) {    //the maximum filesize
                    $file_destination = ''.$file_name;  // If ' ', the file will be placed in this directory
                    if (move_uploaded_file($file_tmp, $file_destination)) {
                        echo $file_destination;

                        $fp = fopen('book_list.txt', "a");
                        fwrite($fp, $_POST['name']. "|||" .$file_destination."\n");
                        fclose($fp);

                    } else {
                        echo "An error has been encountered while moving your file!";
                    }
                } else {
                    echo "Your file is too big!";
                }
            } else {
                echo "An error has been encountered while uploading your file!";
            } 
        } else {
            echo "You can't upload files of this type!";
        }
    }
?>

上传测试
姓名:
文件:
上载页面的PHP:

<!DOCTYPE html>
<html>
    <head>
        <title>Upload test</title>
    </head>
    <body>
        <form action="upload.php" method="post" enctype="multipart/form-data">
            Name: <input type="text" name="name" required/> 
            File: <input type="file" name="file" required/> 
            <input type="submit" value="Upload" />
        </form>
    </body>
</html>
<?php
    if (isset($_FILES['file']) && isset($_POST['name'])) {
        $file = $_FILES['file'];

        $file_name = $file['name'];
        $file_tmp = $file['tmp_name'];
        $file_size = $file['size'];
        $file_error = $file['error'];

        $file_ext = explode(".", $file_name);
        $file_ext = strtolower(end($file_ext));

        $allowed = array("epub", "pdf", "mp4"); //The extensions you allow

        if (in_array($file_ext, $allowed)) {
            if ($file_error === 0) {
                if ($file_size <= 2097152) {    //the maximum filesize
                    $file_destination = ''.$file_name;  // If ' ', the file will be placed in this directory
                    if (move_uploaded_file($file_tmp, $file_destination)) {
                        echo $file_destination;

                        $fp = fopen('book_list.txt', "a");
                        fwrite($fp, $_POST['name']. "|||" .$file_destination."\n");
                        fclose($fp);

                    } else {
                        echo "An error has been encountered while moving your file!";
                    }
                } else {
                    echo "Your file is too big!";
                }
            } else {
                echo "An error has been encountered while uploading your file!";
            } 
        } else {
            echo "You can't upload files of this type!";
        }
    }
?>

我想使用一个文件,但你会怎么做?如果你不想在文件中有很多项目,这没有问题,但是如果你想有很多项目,我建议你使用一个数据库。我可以使用一个数据库,但我没有很好的运气。上传完成后,获取文件名并将其附加到一个文件中,然后使用
file()
将文件作为行数组获取,然后将内容回显到网页上。是否有方法使其自动执行,并有一个从网站下载的链接?