簡體   English   中英

上載頁面不占用多個文件

[英]Upload Page Not Taking Multiple Files

我有一個頁面,允許用戶上傳5個文件和有關每個文件的信息。 我一次成功上傳了1個文件,但是每當我嘗試上傳2個或更多文件時,網頁都會出現異常,然后Google Chrome瀏覽器顯示沒有從服務器發送數據(我使用GoDaddy)。 然后大約一分鍾后,我的網站將重新啟動。 很奇怪。

我的代碼中是否有引起這種反應的東西? 我的for()語句是否正確,最多允許5次上傳? 有沒有更簡單的方法可以做到這一點? 仍在嘗試找出所有問題,但這是我編寫的代碼:

<?

if ($_POST['submitupload']) {


    for ($j=1;$j<6;$j++) {
        //START FIRST FILE
        ${"title".$j}=$_POST['title'.$j];
        ${"authors".$j}=$_POST['authors'.$j];
        ${"description".$j}=$_POST['description'.$j];
        ${"keywords".$j}=$_POST['keywords'.$j];
        ${"private".$j}=$_POST['private'.$j];
        if (${"private".$j}=="Public" || ${"private".$j}=="") {
            ${"private".$j}="1";
        }
        else {
            ${"private".$j}="0";
        }
        ${"uploaded".$j}=$_FILES['uploaded'.$j];

        if (${"uploaded".$j}=="") {
            echo "<div style='margin-bottom:10px; font-size:14px; color:red;'>Nothing was submitted.</div>";
        }

        if (($_FILES['uploaded'.$j]['size'] > 0) && (${"title".$j}=="" || ${"authors".$j}=="" || ${"description".$j}=="" || ${"keywords".$j}=="")) {
            echo "<div style='margin-bottom:10px; font-size:14px; color:red;'>Please fill out all information for each file (File $j).</div>";
        }
        else {
            //This function separates the extension from the rest of the file name and returns it 
            if ($j==1) {
                function findexts1 ($filename) { 
                    $filename = strtolower($filename) ; 
                    $exts = @(split("[/\\.]", $filename)); 
                    $n = count($exts)-1; 
                    $exts = $exts[$n]; 
                    return $exts; 
                } 
            }
            if ($j==2) {
                function findexts2 ($filename) { 
                    $filename = strtolower($filename) ; 
                    $exts = @(split("[/\\.]", $filename)); 
                    $n = count($exts)-1; 
                    $exts = $exts[$n]; 
                    return $exts; 
                } 
            }
            if ($j==3) {
                function findexts3 ($filename) { 
                    $filename = strtolower($filename) ; 
                    $exts = @(split("[/\\.]", $filename)); 
                    $n = count($exts)-1; 
                    $exts = $exts[$n]; 
                    return $exts; 
                } 
            }
            if ($j==4) {
                function findexts4 ($filename) { 
                    $filename = strtolower($filename) ; 
                    $exts = @(split("[/\\.]", $filename)); 
                    $n = count($exts)-1; 
                    $exts = $exts[$n]; 
                    return $exts; 
                } 
            }
            if ($j==5) {
                function findexts5 ($filename) { 
                    $filename = strtolower($filename) ; 
                    $exts = @(split("[/\\.]", $filename)); 
                    $n = count($exts)-1; 
                    $exts = $exts[$n]; 
                    return $exts; 
                } 
            }

            ${"date".$j}=md5(date('Y-m-d H:i:s'));
            ${"datetime".$j}=date('Y-m-d H:i:s');

            //This applies the function to our file  
            if ($j==1) {
            $ext = findexts1 ($_FILES['uploaded'.$j]['name']) ;
            }
            if ($j==2) {
            $ext = findexts2 ($_FILES['uploaded'.$j]['name']) ;
            }
            if ($j==3) {
            $ext = findexts3 ($_FILES['uploaded'.$j]['name']) ;
            }
            if ($j==4) {
            $ext = findexts4 ($_FILES['uploaded'.$j]['name']) ;
            }
            if ($j==5) {
            $ext = findexts5 ($_FILES['uploaded'.$j]['name']) ;
            }
            $type = $_FILES['uploaded'.$j]['type'];
            $size = $_FILES['uploaded'.$j]['size'];

            if (($_FILES['uploaded'.$j]['size'] > 0) && ($type!="audio/mp3" && $type!="audio/mp4" && $type!="audio/mpeg" && $type!="image/gif" && $type!="image/jpeg" && $type!="image/png" && $type!="text/plain" && $type!="video/mpeg" && $type!="video/mp4" && $type!="application/pdf")) {
                echo "<div style='margin-bottom:10px; font-size:14px; color:red;'>Not a valid file type.<br>mp3, mp4, mpeg, gif, jpeg, png, txt, pdf (File $j)</div>";
            }
            elseif ($_FILES['uploaded'.$j]['size'] > "1073741824") {
                echo "<div style='margin-bottom:10px; font-size:14px; color:red;'>Files must not exceed 1024MB (File $j)</div>";
            }
            elseif ($_FILES['uploaded'.$j]['size'] > 0) {

                //This line assigns a random number to a variable. You could also use a timestamp here if you prefer. 
                $ran = $username.$j.${"date".$j};

                //This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
                $ran2 = $ran.".";

                $site = "thefiles/".$ran2.$ext;

                //This assigns the subdirectory you want to save into... make sure it exists!
                $target = "thefiles/";
                //This combines the directory, the random file name, and the extension
                $target = $target.$ran2.$ext; 

                //Insert file info into the database.
                $sql= $conn->prepare('INSERT INTO files (name, link, description, user_id, datetime, public) VALUES (:name, :link, :description, :userid, :datetime, :public)');
                $sql->bindParam(':name', ${"title".$j});
                $sql->bindParam(':link', $site);
                $sql->bindParam(':description', ${"description".$j});
                $sql->bindParam(':userid', $_SESSION['uid']);
                $sql->bindParam(':datetime', ${"datetime".$j});
                $sql->bindParam(':public', ${"private".$j});
                $sql->execute();

                //Get the fileID for future reference.
                $getfile=$conn->prepare('SELECT * FROM files WHERE name=:name AND description=:description AND link=:link AND user_id=:userid AND datetime=:datetime AND public=:public');
                $getfile->bindParam(':name', ${"title".$j});
                $getfile->bindParam(':link', $site);
                $getfile->bindParam(':description', ${"description".$j});
                $getfile->bindParam(':userid', $_SESSION['uid']);
                $getfile->bindParam(':datetime', ${"datetime".$j});
                $getfile->bindParam(':public', ${"private".$j});
                $getfile->execute();
                $filerow=$getfile->fetch();
                $fileid=$filerow['ID'];

                //Seperate keywords and insert into database
                ${"thekeyword".$j} = explode(", ", ${"keywords".$j});
                $countkeywords=count(${"thekeyword".$j});
                for ($i=0;$i<$countkeywords;$i++) {
                $keywordsql= $conn->prepare('INSERT INTO filekeywords (fileid, keyword, public, userid) VALUES (:fileid, :keyword, :public, :userid)');
                $keywordsql->bindParam(':fileid', $fileid);
                $keywordsql->bindParam(':userid', $_SESSION['uid']);
                $keywordsql->bindParam(':public', ${"private".$j});
                $keywordsql->bindParam(':keyword', ${"thekeyword".$j}[$i]);
                $keywordsql->execute();
                }


                //Seperate authors and insert into database
                ${"theauthors".$j} = explode(", ", ${"authors".$j});
                $countauthors=count(${"theauthors".$j});
                for ($i=0;$i<$countauthors;$i++) {
                $authorsql= $conn->prepare('INSERT INTO fileauthors (fileid, author, public, userid) VALUES (:fileid, :author, :public, :userid)');
                $authorsql->bindParam(':fileid', $fileid);
                $authorsql->bindParam(':userid', $_SESSION['uid']);
                $authorsql->bindParam(':public', ${"private".$j});
                $authorsql->bindParam(':author', ${"authors".$j});
                $authorsql->execute();
                }

                if(move_uploaded_file($_FILES['uploaded'.$j]['tmp_name'], $target)) {
                    echo "<div style='margin-bottom:10px; font-size:14px;'>Upload successful (File $j).</div>";
                } 
            }
        }
    }
} //END IF(SUBMITUPLOAD)


?>








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



<!--FILE 1-->
<div style="margin-bottom:30px; border:2px solid silver; width:550px; border-radius:15px; box-shadow: 5px 5px 2px #888888;">
    <div class="upload-textbox" style="padding-bottom:8px; padding-top:8px">
        <input type="text" name="title1" placeholder="Title">
    </div>
    <div class="upload-textbox" style="padding-bottom:8px;">
        <input type="text" name="authors1" placeholder="Authors (Ex: Ethan Bise, Jo Cook)">
    </div>
    <div class="upload-textbox" style="padding-bottom:8px;">
        <input type="text" name="description1" placeholder="Description (500 characters)">
    </div>
    <div class="upload-textbox" style="padding-bottom:8px; display:inline-block;">
        <input type="text" name="keywords1" placeholder="Keywords" onfocus="document.getElementById('show_hide1').style.display='inline-block';" onblur="document.getElementById('show_hide1').style.display='none';"> 
    </div>
    <div id="show_hide1" style="margin-top:-10px; margin-left:10px; position:absolute; border:2px solid black; border-radius:10px; padding:10px; display:none; background-color:#E68A2E; text-align:left;">
            Max: 5
            <br>
            Example: Mechanical Engineering, Thermodynamics, Machine Design
    </div>
    <div class="styled-select-upload" style="background:white; margin-bottom:8px;">
        <select name="private1">
        <option value="Public">Public</option>
        <option value="Private">Private</option>
        </select>
    </div>
    <div style="padding-bottom:8px;">
        <input type="file" enctype="multipart/form-data" name="uploaded1" style="display: inline-block;">
    </div>
</div>
<!--END FILE 5-->


<!--FILE 2-->
<div style="margin-bottom:30px; border:2px solid silver; width:550px; border-radius:15px; box-shadow: 5px 5px 2px #888888;">
    <div class="upload-textbox" style="padding-bottom:8px; padding-top:8px">
        <input type="text" name="title2" placeholder="Title">
    </div>
    <div class="upload-textbox" style="padding-bottom:8px;">
        <input type="text" name="authors2" placeholder="Authors (Ex: Ethan Bise, Jo Cook)">
    </div>
    <div class="upload-textbox" style="padding-bottom:8px;">
        <input type="text" name="description2" placeholder="Description (500 characters)">
    </div>
    <div class="upload-textbox" style="padding-bottom:8px; display:inline-block;">
        <input type="text" name="keywords2" placeholder="Keywords" onfocus="document.getElementById('show_hide2').style.display='inline-block';" onblur="document.getElementById('show_hide2').style.display='none';"> 
    </div>
    <div id="show_hide2" style="margin-top:-10px; margin-left:10px; position:absolute; border:2px solid black; border-radius:10px; padding:10px; display:none; background-color:#E68A2E; text-align:left;">
            Max: 5
            <br>
            Example: Mechanical Engineering, Thermodynamics, Machine Design
    </div>
    <div class="styled-select-upload" style="background:white; margin-bottom:8px;">
        <select name="private2">
        <option value="Public">Public</option>
        <option value="Private">Private</option>
        </select>
    </div>
    <div style="padding-bottom:8px;">
        <input type="file" enctype="multipart/form-data" name="uploaded2" style="display: inline-block;">
    </div>
</div>
<!--END FILE 2-->


<!--FILE 3-->
<div style="margin-bottom:30px; border:2px solid silver; width:550px; border-radius:15px; box-shadow: 5px 5px 2px #888888;">
    <div class="upload-textbox" style="padding-bottom:8px; padding-top:8px">
        <input type="text" name="title3" placeholder="Title">
    </div>
    <div class="upload-textbox" style="padding-bottom:8px;">
        <input type="text" name="authors3" placeholder="Authors (Ex: Ethan Bise, Jo Cook)">
    </div>
    <div class="upload-textbox" style="padding-bottom:8px;">
        <input type="text" name="description3" placeholder="Description (500 characters)">
    </div>
    <div class="upload-textbox" style="padding-bottom:8px; display:inline-block;">
        <input type="text" name="keywords3" placeholder="Keywords" onfocus="document.getElementById('show_hide3').style.display='inline-block';" onblur="document.getElementById('show_hide3').style.display='none';"> 
    </div>
    <div id="show_hide3" style="margin-top:-10px; margin-left:10px; position:absolute; border:2px solid black; border-radius:10px; padding:10px; display:none; background-color:#E68A2E; text-align:left;">
            Max: 5
            <br>
            Example: Mechanical Engineering, Thermodynamics, Machine Design
    </div>
    <div class="styled-select-upload" style="background:white; margin-bottom:8px;">
        <select name="private3">
        <option value="Public">Public</option>
        <option value="Private">Private</option>
        </select>
    </div>
    <div style="padding-bottom:8px;">
        <input type="file" enctype="multipart/form-data" name="uploaded3" style="display: inline-block;">
    </div>
</div>
<!--END FILE 3-->


<!--FILE 4-->
<div style="margin-bottom:30px; border:2px solid silver; width:550px; border-radius:15px; box-shadow: 5px 5px 2px #888888;">
    <div class="upload-textbox" style="padding-bottom:8px; padding-top:8px">
        <input type="text" name="title4" placeholder="Title">
    </div>
    <div class="upload-textbox" style="padding-bottom:8px;">
        <input type="text" name="authors4" placeholder="Authors (Ex: Ethan Bise, Jo Cook)">
    </div>
    <div class="upload-textbox" style="padding-bottom:8px;">
        <input type="text" name="description4" placeholder="Description (500 characters)">
    </div>
    <div class="upload-textbox" style="padding-bottom:8px; display:inline-block;">
        <input type="text" name="keywords4" placeholder="Keywords" onfocus="document.getElementById('show_hide4').style.display='inline-block';" onblur="document.getElementById('show_hide4').style.display='none';"> 
    </div>
    <div id="show_hide4" style="margin-top:-10px; margin-left:10px; position:absolute; border:2px solid black; border-radius:10px; padding:10px; display:none; background-color:#E68A2E; text-align:left;">
            Max: 5
            <br>
            Example: Mechanical Engineering, Thermodynamics, Machine Design
    </div>
    <div class="styled-select-upload" style="background:white; margin-bottom:8px;">
        <select name="private4">
        <option value="Public">Public</option>
        <option value="Private">Private</option>
        </select>
    </div>
    <div style="padding-bottom:8px;">
        <input type="file" enctype="multipart/form-data" name="uploaded4" style="display: inline-block;">
    </div>
</div>
<!--END FILE 4-->


<!--FILE 5-->
<div style="margin-bottom:30px; border:2px solid silver; width:550px; border-radius:15px; box-shadow: 5px 5px 2px #888888;">
    <div class="upload-textbox" style="padding-bottom:8px; padding-top:8px">
        <input type="text" name="title5" placeholder="Title">
    </div>
    <div class="upload-textbox" style="padding-bottom:8px;">
        <input type="text" name="authors5" placeholder="Authors (Ex: Ethan Bise, Jo Cook)">
    </div>
    <div class="upload-textbox" style="padding-bottom:8px;">
        <input type="text" name="description5" placeholder="Description (500 characters)">
    </div>
    <div class="upload-textbox" style="padding-bottom:8px; display:inline-block;">
        <input type="text" name="keywords5" placeholder="Keywords" onfocus="document.getElementById('show_hide5').style.display='inline-block';" onblur="document.getElementById('show_hide5').style.display='none';"> 
    </div>
    <div id="show_hide5" style="margin-top:-10px; margin-left:10px; position:absolute; border:2px solid black; border-radius:10px; padding:10px; display:none; background-color:#E68A2E; text-align:left;">
            Max: 5
            <br>
            Example: Mechanical Engineering, Thermodynamics, Machine Design
    </div>
    <div class="styled-select-upload" style="background:white; margin-bottom:8px;">
        <select name="private5">
        <option value="Public">Public</option>
        <option value="Private">Private</option>
        </select>
    </div>
    <div style="padding-bottom:8px;">
        <input type="file" enctype="multipart/form-data" name="uploaded5" style="display: inline-block;">
    </div>
</div>
<!--END FILE 5-->




<div style="padding-bottom:20px;">
<input type="submit" name="submitupload" value="Upload" style="display: inline-block;">
</div>
</form>

在我的php.ini文件中,最大上傳大小必須更改為1024M。 Godaddy將其設置為75MB。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM