简体   繁体   English

如何从$ _FILES嵌套数组中获取文件名

[英]How to get filename from $_FILES nested array

I'm attempting to list all the filenames of images uploaded via a form to then be sent via email. 我正在尝试列出通过表单上传的图像的所有文件名,然后通过电子邮件发送。

fileupload.html: fileupload.html:

<form method="post" action="upload.php" enctype="multipart/form-data" id="upload" >
<input type="file" name="filesupl[]" multiple id="files" />
<input type="submit" name="UploadBtn" value="Upload" class="formtext" id="UploadBtn">
</form>

upload.php: upload.php的:

$art_file = $_FILES['filesupl'];
$filecounter = 1;
$count = 0;
foreach ( $art_file as $i => $art_inner ){ 
                    echo $filecounter . ") " . $art_file[$i]['name'] . "<br />"; $filecounter++; 
        }
if ($_SERVER['REQUEST_METHOD'] == 'POST' and isset($_FILES['filesupl']))
    {
        $extension = pathinfo($_FILES['filesupl']['name'], PATHINFO_EXTENSION);

        // Upload files
        // loop all files
        foreach ( $_FILES['filesupl']['name'] as $i => $name )
        {
            // if file not uploaded then skip it
            if ( !is_uploaded_file($_FILES['filesupl']['tmp_name'][$i]) )
                continue;

            // skip large files
            if ( $_FILES['filesupl']['size'][$i] >= $max_size )
                continue;

            // skip unprotected files
            if( !in_array(pathinfo($name, PATHINFO_EXTENSION), $extensions) )
                continue;

            // now we can move uploaded files
            if( move_uploaded_file($_FILES["filesupl"]["tmp_name"][$i], $dir . $name) )
                $count++;
        }

    }

echo json_encode(array('count' => $count));

I've attempted numerous ways to access the 'name' of the files uploaded (the code above was one of those attempts), to no avail. 我尝试了很多方法来访问上传文件的“名称”(上面的代码是其中一个尝试),但无济于事。

I'm attempting to list all the filenames of images uploaded ... 我正在尝试列出上传图像的所有文件名...

If your sole purpose is to get all the file names of the uploaded files, then your code should simply be like this: 如果您的唯一目的是获取上传文件的所有文件名,那么您的代码应该是这样的:

foreach($_FILES['filesupl']['name'] as $name){
    // echo $name . '<br />';
}

Now if you want to refactor your code, and to make it work - use a for loop like this: 现在,如果您想重构代码并使其工作 - 请使用这样的for循环:

$count = count($_FILES['filesupl']['name']);
for($i = 0; $i < $count; ++$i){
    // if file not uploaded then skip it
    if ( !is_uploaded_file($_FILES['filesupl']['tmp_name'][$i]) )
        continue;

    // skip large files
    if ( $_FILES['filesupl']['size'][$i] >= $max_size )
        continue;

    // get file extension
    $extension = pathinfo($_FILES['filesupl']['name'][$i], PATHINFO_EXTENSION);

    // skip unprotected files
    if( !in_array($extension, $extensions) )
        continue;

    // now we can move uploaded files
    if( move_uploaded_file($_FILES["filesupl"]["tmp_name"][$i], $dir . $_FILES['filesupl']['name'][$i])){
        // file has been uploaded
    }
}

echo json_encode(array('count' => $count));

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

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