简体   繁体   English

使用php在单个html页面上上传多个文件的问题

[英]Issue with uploading multiple files on a single html page using php

I am trying to build a web page where users can edit images portrayed on their public pages. 我正在尝试建立一个网页,用户可以在该网页上编辑其公共页面上描绘的图像。 There are 3 images that display on their public page and I've set up 3 HTML forms in order to handle the 3 separate files. 公共页面上显示3张图片,并且我已经设置3个HTML表单来处理3个单独的文件。

I only have 1 listed below because once I figure out the fix to 1 I'll be able to duplicate the fix to the other 2, 我下面只列出了1个,因为一旦找出了对1的修复,我就可以将修复复制到其他2个,

I have other upload pages on my website and they work fine, but for some reason this page is giving me trouble. 我的网站上还有其他上传页面,它们可以正常工作,但是由于某种原因,该页面给我带来了麻烦。 I can select a file but when I want to upload it my php code doesn't read that there is anything being posted. 我可以选择一个文件,但是当我要上传它时,我的PHP代码没有读取任何内容。

*I've commented out the function call (I know it works) I just need to know why my php code won't read that there is a file there. *我已经注释掉了函数调用(我知道它可以正常工作),我只需要知道为什么我的php代码不会读到那里有文件。

If I had to guess it'd be something with how it's named or how it's being tossed to the php code. 如果我不得不猜测这与它的命名方式或如何扔到php代码有关。

The code looks like this: 代码如下:

                                            <div class="academic" style="width:250px;">
        <br>
        <?php 




        if(isset($_FILES['aca']) === true)
        {
            echo 'please print';  //It doesn't
            if(empty($_FILES['aca']['name']) === true)
            {
            echo 'Please choose a file! <br>';
            }
            else
            {

                $allowed = array('jpg', 'jpeg', 'gif', 'png');

                $file_name = $_FILES['aca']['name']; 

                $file_extn = strtolower(end(explode('.', $file_name)));
                $file_temp = $_FILES['aca']['tmp_name'];
                echo '<br>';
                echo '<br>';
                print_r($_FILES['aca']['tmp_name']);
                echo '<br>';
                echo '<br>';
                if(in_array($file_extn, $allowed) === true) {

                //upload_image($file_temp,$file_extn);

                echo 'You have uploaded a picture!';
                echo '<b><h3>Press submit again to finish the upload</h3></b>';
                //echo "<script>window.top.location='../hidden/viewPNM.php?id=$permi'</script>";


                }
                else
                {
                echo 'Incorrect File Type!! <br> Allowed: ';
                echo implode(', ', $allowed);

                }

            }

        }


        ?>
        <form action="" method="post" enctype="multipart/form-data">
        <input type="file" name="aca" id="aca"> 
        <input type="submit" value="Upload">
        </form>

        </div>
        </span>

Okay...here is what you can try to do: 好的...您可以尝试执行以下操作:

Move the whole PHP code UNDER the form code. 将整个PHP代码移到表单代码下。 I remember that sometimes wierd bugs with that were happening, and the code didnt run 我记得有时会发生一些奇怪的错误,并且代码没有运行

If that doesnt work, do following: 如果那不起作用,请执行以下操作:

  1. Change <input type="file" name="aca" id="aca"> to <input multiple="true" type="file" name="aca[]" /> <input type="file" name="aca" id="aca">更改为<input multiple="true" type="file" name="aca[]" />

  2. Add this code above the submit button: <input type="hidden" name="sendfiles" value="Send Files" /> 将此代码添加到“提交”按钮上方: <input type="hidden" name="sendfiles" value="Send Files" />

  3. Replace if(isset($_FILES['aca']) === true) with if(isset($_POST['sendfiles'])) if(isset($_FILES['aca']) === true)替换为if(isset($_POST['sendfiles']))

If this doesn't work, I can offer you a way to allow multiple files being uploaded from one button. 如果这不起作用,我可以为您提供一种方法,允许从一个按钮上载多个文件。 (Took the code from my website I made before) (从我之前制作的网站上获取代码)

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

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