0

我想知道的是,当我通过上传按钮选择目录时,如何获取目录中所有文件名的列表[特别是数组],之后我将该文件数组上传到数据库。作为一个文件作为一个条目。那么我该怎么做呢?不要忘记我只需要文件名,我只需要上传这些名称而不是实际文件。

4

4 回答 4

1
 <?
    if (isset($_POST[submit])) {
    $uploadArray= array();
    $uploadArray[] = $_POST['uploadedfile'];
    $uploadArray[] = $_POST['uploadedfile2'];
    $uploadArray[] = $_POST['uploadedfile3'];
    foreach($uploadArray as $file) {
    $target_path = "upload/";
    $target_path = $target_path . basename( $_FILES['$file']['name']);
    if(move_uploaded_file($_FILES['$file']['tmp_name'], $target_path)) {
    echo "The file ". basename( $_FILES['$file']['name']).
    " has been uploaded";
    } else{
    echo "There was an error uploading the file, please try again!";
    }
    }
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <form enctype="multipart/form-data" action="upload-simple.php" method="POST">
    <p>
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    Choose a file to upload:
    <input name="uploadedfile" type="file" />
    </p>
    <p>Choose a file to upload:
    <input name="uploadedfile2" type="file" />
    </p>
    <p>Choose a file to upload:
    <input name="uploadedfile3" type="file" />
    <input name="submit" type="submit" id="submit" value="submit" />
    </p>
    </form>
    </body>
    </html>

这可能会解决您的问题

于 2012-07-11T18:37:22.077 回答
1

是服务器上的文件吗?如果您希望有一个按钮,请单击浏览器并在最终用户上打开一个文件夹,这将不起作用。大多数浏览器只允许选择单个文件

于 2012-07-11T18:34:02.770 回答
1

如果您使用 ftp,此函数将返回数组中目录的所有文件名。

function ftp_searchdir($conn_id, $dir) {
    if(!@ftp_is_dir($conn_id, $dir)) {
        die('No such directory on the ftp-server');
    }
    if(strrchr($dir, '/') != '/') {
        $dir = $dir.'/';
    }

    $dirlist[0] = $dir;
    $list = ftp_nlist($conn_id, $dir);
    foreach($list as $path) {
        $path = './'.$path;
        if($path != $dir.'.' && $path != $dir.'..') {
            if(ftp_is_dir($conn_id, $path)) {
                $temp = ftp_searchdir($conn_id, ($path), 1);
                $dirlist = array_merge($dirlist, $temp);
            }
            else {
                $dirlist[] = $path;
            }
        }

    }

    ftp_chdir($conn_id, '/../');

    return $dirlist;

}
于 2012-07-11T18:32:08.070 回答
0

如果服务器上已经存在文件,您可以使用glob

$files = glob('*.ext'); // or *.* for all files
foreach($files AS $file){
    // $file is the name of the file
}
于 2012-07-11T18:30:37.147 回答