上传页面不占用多个文件

时间:2014-02-14 14:59:46

标签: php html file upload

我有一个页面,允许用户上传5个文件和每个文件的信息。我已经成功地一次上传了1个文件,但每当我尝试上传2个或更多文件时,网页就会吓坏,然后谷歌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>

1 个答案:

答案 0 :(得分:0)

我的php.ini文件中的最大上传大小必须更改为1024M。 Godaddy把它设置为75MB。