PHP上传输入数组并如何查找有多少个文件?

发布于 2024-12-05 10:21:16 字数 659 浏览 0 评论 0原文

我有这样的表格:

<form enctype="multipart/form-data" action="upload.php" method="POST">
    <input type="file" name="photo[]" />
    <input type="file" name="photo[]" />
</form>

我用这种方式上传文件:

function upFiles($files){
    for($i = 0 ; $i < $files ; $i++){
        $FV_filename = $_FILES['photo']['name'][$i];
        $fileTYPE = substr($FV_filename , -4);
        move_uploaded_file($_FILES['photo']['tmp_name'][$i], uniqid().$fileTYPE);
    }
}

upFiles($_POST['howManyFiles']);

正如你所看到的,我从客户端获取了已发送的文件数量。 我想做的是在服务器端检查已收到多少文件。 我怎样才能做到这一点?是否有任何内置的 php 函数,如果没有那么如何创建一个?

I have this form :

<form enctype="multipart/form-data" action="upload.php" method="POST">
    <input type="file" name="photo[]" />
    <input type="file" name="photo[]" />
</form>

And I upload the files with php this way :

function upFiles($files){
    for($i = 0 ; $i < $files ; $i++){
        $FV_filename = $_FILES['photo']['name'][$i];
        $fileTYPE = substr($FV_filename , -4);
        move_uploaded_file($_FILES['photo']['tmp_name'][$i], uniqid().$fileTYPE);
    }
}

upFiles($_POST['howManyFiles']);

As you can see I get from the the client side how many files have been sent.
What I want to do is to check in the server side how many files have been received.
How can I do that? Is there any built in php function, if not then how to create one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执着的年纪 2024-12-12 10:21:16
count( $_FILES['photo']['name'] );

应该有效。或者,只需将 for 循环重写为 foreach

foreach ( $_FILES as $name => $file ) {
    var_dump( $file );
    // process file...
}
count( $_FILES['photo']['name'] );

should work. Alternatively, just rewrite your for loop to a foreach:

foreach ( $_FILES as $name => $file ) {
    var_dump( $file );
    // process file...
}
要走干脆点 2024-12-12 10:21:16

您可以使用 is_uploaded_file 检查文件是否已上传,如下所示:

function upFiles($files){
    for($i = 0 ; $i < $files ; $i++){
        if (is_uploaded_file($_FILES['photo']['tmp_name'][$i])) {
            // uploaded file
        }
    }
}

您可以使用 foreach 循环遍历所有上传的照片,而不是使用幻数< /em>.

来自: php.net

You could check whether the file has been uploaded with is_uploaded_file, like so:

function upFiles($files){
    for($i = 0 ; $i < $files ; $i++){
        if (is_uploaded_file($_FILES['photo']['tmp_name'][$i])) {
            // uploaded file
        }
    }
}

You could use foreach to loop over all the uploaded photos instead of using a magic number.

From: php.net

紫竹語嫣☆ 2024-12-12 10:21:16

迭代 $_FILES 数组:

function upFiles( $baseVarName ){

    foreach( $_FILES[ $baseVarName ] as $fileDetails ) {

        $FV_filename = $fileDetails['name'][$i];
        $fileTYPE = substr($FV_filename , -4);
        move_uploaded_file( $fileDetails['tmp_name'][$i], uniqid().$fileTYPE);

    }

}

upFiles( 'photo' );

Iterate over the $_FILES array:

function upFiles( $baseVarName ){

    foreach( $_FILES[ $baseVarName ] as $fileDetails ) {

        $FV_filename = $fileDetails['name'][$i];
        $fileTYPE = substr($FV_filename , -4);
        move_uploaded_file( $fileDetails['tmp_name'][$i], uniqid().$fileTYPE);

    }

}

upFiles( 'photo' );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文