我脚本中的错误在哪里?

时间:2015-11-14 18:00:40

标签: php

朋友,我的php多文件上传脚本有什么问题?运行此脚本时,它什么都不做!请帮我找出错误。 在此先感谢..这是HTML代码..

    <form action="upload.php" method="post" enctype="multipart/form-data">
    <h3>Upload your files here <small>(1GB max)</small> ..</h3>
    <br>
    <br>
    <input type="file" name="file" id="file">
    <br>
    <input type="file" name="file" id="file">
    <br>
    <br>
    <input type="submit" value="Upload file(s)" name="submit" id="submit">
</form>

这是“upload.php”文件..

<?php 
if (isset($_POST['submit'])) { 
    //upload and rename file 
    $filename=$_FILES["file"]["name"];
    $tmp_filename=$_FILES["file"]["tmp_name"];
    $count_tmp_filename=count($tmp_filename);
    for($i = 0; $i < $count_tmp_filename; $i++){ 
        $file_basename = substr($filename[$i], 0, strripos($filename[$i], '.'));
        $file_ext = substr($filename[$i], strripos($filename[$i], '.'));
        $filesize = $_FILES["file"]["size"][$i];
        $allowed_file_types = array('.doc','.docx','.rtf','.pdf','.jpg','.png','.mp4','.mp3');
        if (in_array($file_ext,$allowed_file_types) && ($filesize < 1000000000000)) { 
        // Rename file 
            $newfilename = 'droidsolve_'.$file_basename.$file_ext;
            if (file_exists("upload/" . $newfilename)) {
                // file already exists 
                error echo "You have already uploaded this file.";
            } else{ 
                move_uploaded_file($_FILES[$tmp_filename[$i]], "upload/" . $newfilename);
                echo "File uploaded successfully.";
            }
        }
    }
} 
?>

1 个答案:

答案 0 :(得分:1)

正如chris85在评论中指出的那样,名称在表单集中是唯一的。因此,将代码更改为(省略其中一个文件输入):

<form action="upload.php" method="post" enctype="multipart/form-data">
<h3>Upload your files here <small>(1GB max)</small> ..</h3>
<br>
<br>
<input type="file" name="file" id="file">
<br>
<br>
<input type="submit" value="Upload file(s)" name="submit" id="submit">
</form>

或者(将其作为数组并调整id s):

<form action="upload.php" method="post" enctype="multipart/form-data">
<h3>Upload your files here <small>(1GB max)</small> ..</h3>
<br>
<br>
<input type="file" name="file[]" id="file1">
<br>
<input type="file" name="file[]" id="file2">
<br>
<br>
<input type="submit" value="Upload file(s)" name="submit" id="submit">
</form>

使用后者,您需要相应地更改PHP上传代码(例如,使用foreach循环)。