PHP问题(文件上传)

时间:2015-08-31 14:45:55

标签: php file-upload web

我已经在网站上工作了一段时间,我在这里使用的代码大约一个月前工作,然后我尝试将文件放在子文件夹中,并重定向move_uploaded_file函数,但它没有似乎工作。具体来说,两者都是:

if((!empty($_FILES["uploaded_letter"])) &&  ($_FILES['uploaded_letter']['error'] == 0)) 

if((!empty($_FILES["uploaded_list"])) && ($_FILES['uploaded_list']['error'] == 0))

语句返回false。 如果有人可以请求帮助,那将是非常棒的!谢谢!

<html>
    <head>
        <title>VAA Chapter 11</title>
        <!--favicon-->
        <link rel="shortcut icon" href="favicon.ico" />

        <!--global CSS-->
        <link href="vaa.css" rel="stylesheet" type="text/css">

        <!--local CSS-->
        <style type="text/css">

        </style>
    </head>
    <body>
        <?php
        function testPassword(){
            $password = $_POST['password'];
            if($password == 'You aint guessin this'){
                echo 'welcome!!! <br/>
                <form enctype="multipart/form-data" action="" method="post">
                        <input type="hidden" name="MAX_FILE_SIZE" value="1000000" /> <br/>
                        add newsletter: <input name="uploaded_letter" type="file" /><br/><br/>
                        add list: <input name="uploaded_list" type="file" /><br/><br/>
                        <input type="submit" name="load" value="Upload" />
                </form>';
            }
            else{
                echo "you have no power here";
            }
         }
        function upload(){
            if((!empty($_FILES["uploaded_letter"])) &&  ($_FILES['uploaded_letter']['error'] == 0)) {
                $filename = basename($_FILES['uploaded_letter']['name']);
                $ext = substr($filename, strrpos($filename, '.') + 1);

                if (($ext == "pdf") && ($_FILES["uploaded_letter"]["size"] < 35000000)) {
                    $newname = dirname(__FILE__,1).'/newsletters/'.$filename;

                    move_uploaded_file($_FILES['uploaded_letter']['tmp_name'],$newname);
                } 
                else {
                    echo "Error: file is too large";
                }
            } 
            else {
                echo "Error: No file uploaded";
            }
            if((!empty($_FILES["uploaded_list"])) && ($_FILES['uploaded_list']['error'] == 0)) {
                $filename = basename($_FILES['uploaded_list']['name']);
                $ext = substr($filename, strrpos($filename, '.') + 1);

                if (($ext == "txt") && ($_FILES["uploaded_file"]["size"] < 35000)) {
                    $newname = dirname(__FILE__,1).'/'.$filename;

                    move_uploaded_file($_FILES['uploaded_list']['tmp_name'],$newname);
                } 
                else {
                    echo "Error: file is too large";
                }
            } 
            else {
                echo "Error: No file uploaded";
            }
        }
        function testUpload(){
            if(isset($_POST['load'])){
                upload();
            }
        }
        ?>    
        <form method="post">
            <input type="text" name="password" />
            <input type="submit" value="OK" />  
        </form>
        <?php testPassword(); testUpload(); ?>
    </body>
</html>

3 个答案:

答案 0 :(得分:0)

最好是要上传多个文件以使用数组命名文件,例如add newsletter: <input name="uploaded_letter[]" type="file" /> add list: <input name="uploaded_letter[]" type="file" />然后您可以在目标页面中使用foreach

答案 1 :(得分:0)

使用此,

if((!empty($_FILES["uploaded_letter"]["name"]))
{
    // Your Procedures
}

if((!empty($_FILES["uploaded_list"]["name"]))
{
  // Your Procedures
}

答案 2 :(得分:0)

我认为你应该在upload()中使用if和else if条件,如:

function upload(){
        if((!empty($_FILES["uploaded_letter"])) && ($_FILES['uploaded_letter']['error'] == 0)) {
            //your code......
        }
        //missing elseif statement instead of if

        elseif ((!empty($_FILES["uploaded_list"])) && ($_FILES['uploaded_list']['error'] == 0)) {
            //your code....

      else {
                echo "Error: file is too large";
            }
        } 
        else {
            echo "Error: No file uploaded";
        }
    }