简体   繁体   English

在上传时获取所有文件名在数组中的PHP

[英]on upload get all file names in array in php

What i want to know is how can I get a list [specifically array] of all the files name in a directory when I select it through upload button, after which I would upload that array of files to the database. 我想知道的是,当我通过上载按钮选择目录时,如何获取目录中所有文件名的列表(特别是数组),然后将其上传到数据库中。 As one file as a single entry. 作为一个文件作为单个条目。 So how do I do that? 那我该怎么做呢? No to forget that I just need files names and I have to upload these names only not the actual files. 别忘了,我只需要文件名,而我只需要上传这些名称而不是实际文件。

If you are using ftp, this function will return all of the filenames of a directory in an array. 如果使用的是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;

}

are the files on the server? 文件在服务器上? if you hopping to have a button you click on browser and open a folder on the end user this will not work. 如果您希望有一个按钮,请单击浏览器并在最终用户上打开一个文件夹,这将不起作用。 most browsers only allow single file selection 大多数浏览器只允许选择单个文件

 <?
    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>

This Might be Solve your Problems 这可能会解决您的问题

If the files already exist on the server you can use glob 如果文件已经存在于服务器上,则可以使用glob

$files = glob('*.ext'); // or *.* for all files
foreach($files AS $file){
    // $file is the name of the file
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM