0

我正在尝试让 php 上传脚本工作。但我不确定为什么 $_FILES 似乎是空数组?有人可以阐明我下面的脚本出了什么问题吗?

它假设将任意文件上传到 /var/www/test/ 文件夹...

<html>
<body>
<?php
    if (isset($_POST['Submit'])) {

            $target_path = "/var/www/test/";
            $target_path = $target_path . basename($_FILES['myupload']['name']);

            if(!move_uploaded_file($_FILES['myupload']['tmp_name'], $target_path)) {

                echo '<pre>';
                print 'The $_FILES content is :';
                print_r($_FILES);
                var_dump($_FILES);
                echo '<pre>';
                print 'the target path is:' . $target_path;
                echo '<pre>';
                print 'the $_POST variable content is :';
                print_r($_POST) ;
                echo '<pre>';
                echo 'Your image was not uploaded.';
                echo '</pre>';

              } else {

                echo '<pre>';
                echo $target_path . ' succesfully uploaded!';
                echo '</pre>';

            }

        }
?>

<form method="POST" action="" enctype"multipart/form-data">
        Choose an image to upload:
        <br>
        <input type="file" name="myupload">
        <br>
        <br>
        <input type="submit" value="Upload(This_is_just_button_name_display)" name="Submit">
</form>
</body>
</html>
4

1 回答 1

0

不要转储 $_FILES 我从来没有转储它并且对我来说很好用。试试这段代码并将php放在另一个文件中并将表单重定向到它并在脚本末尾添加这一行

<META HTTP-EQUIV="REFRESH" CONTENT="0;URL=samplehtml.html">

所以最终的代码是:

<?php
    if (isset($_POST['Submit'])) {

            $target_path = "/var/www/test/";
            $target_path = $target_path . basename($_FILES['myupload']['name']);

            if(!move_uploaded_file($_FILES['myupload']['tmp_name'], $target_path)) {

                echo '<pre>';
                print 'The $_FILES content is :';
                print_r($_FILES)
                echo '<pre>';
                print 'the target path is:' . $target_path;
                echo '<pre>';
                print 'the $_POST variable content is :';
                print_r($_POST) ;
                echo '<pre>';
                echo 'Your image was not uploaded.';
                echo '</pre>';

              } else {

                echo '<pre>';
                echo $target_path . ' succesfully uploaded!';
                echo '</pre>';

            }

        }
?>

它现在应该可以工作了。

于 2013-07-01T04:38:00.003 回答