PHP,同时使用$ _FILES和$ _POST

时间:2016-04-08 15:59:31

标签: php

我在使用$ _FILES和$ _POST时遇到问题,因为我有一个表单来上传图像和一些数据总线,当我使用其中一个时,它可以工作,但是当我使用另一个id没有&#39工作。 我的代码是:

    <?php
include 'debugging.php';

//phpinfo();
echo '<br />';

echo '<h1>Image Upload</h1>';

//create a form with a file upload control and a submit button
echo <<<_END
<br />
<form method='post' action='uplaodex.php' enctype='multipart/form-data'>
Select File: <input type='file' name='picName' size='50' />

name: <input type='text' name='usName' size='50' />
username : <input type='text' name='usUsername' size='50' />
pass: <input type='password' name='usPass' size='50' />
email: <input type='text' name='usEmail' size='50' />
<br />
<input type='submit' value='Upload' />
<input type="hidden" name="submit" value="1" />
</form>
<br />
_END;
//above is a special use of the echo function - everything between <<<_END 
//and _END will be treated as one large echo statement

//$_FILES is a PHP global array similar to $_POST and $_GET
if (isset($_FILES['picName'])and isset($_POST['submit'])) {
//we access the $_FILES array using the name of the upload control in the form created above    
//

    //create a file path to save the uploaded file into     
    $name = "images//" . $_FILES['picName']['name']; //unix path uses forward slash
    //'filename' index comes from input type 'file' in the form above
    //
    //move uploaded file from temp to web directory
    if (move_uploaded_file($_FILES['picName']['tmp_name'], $name)) {
       // Create the file DO and populate it.
        include 'Do_profile.php';
        $file = new Do_profile();

        //we are going to store the file type and the size so we get that info
        $type = $_FILES['picName']['type'];
        $size = $_FILES['picName']['size'];
        $usName = trim($_POST['usName']);
        $usUsername = trim($_POST['usUsername']);
        $usPass = trim($_POST['usPass']);
        $usEmail = trim($_POST['usEmail']);

        $file->FileName = $name; //$name is initialised previously using $_FILES and file path
        $file->FileSize = $size;
        $file->Type = $type;
        $file->usName = $usName;
        $file->usUsername = $usUsername;
        $file->usPass = $usPass;
        $file->usEmail = $usEmail;

        if ($file->save()) {
//select the ID of the image just stored so we can create a link                

//display success message                      
                echo "<h1> Thankyou </h1><p>Image stored successfully</p>";
//this above line of code displays the image now stored in the images sub directory                      
                echo "<p>Uploaded image '$name'</p><br /><img src='$name' height='200' width='200'/>";
//create alink to the page we will use to display the stored image                      
                echo '<br><a href="Display.php?id=' . $fileId . '">Display image ' .
                $file->FileName . '</a>';
            } else
                echo '<p class="error">Error retrieving file information</p>';
        }
        else {
            echo '<p class="error"> Oh dear. There was a databse error</p>';
        }
    } else {
        //error handling in case move_uploaded_file() the file fails
        $error_array = error_get_last();
        echo "<p class='error'>Could not move the file</p>";

      //  foreach($error_array as $err)
        //    echo $err;
    }


echo "</body></html>";


?>

我不知道问题是什么,有什么帮助??

3 个答案:

答案 0 :(得分:4)

if (isset($_FILES['picName'])and isset($_POST['submit']))内的所有内容都无法正常工作,因为超级全局$_FILES可能没有名为picName的密钥。要查看此问题,请尝试var_dump $_FILES,例如var_dump($_FILES);

根据var_dump的输出,您可以了解$_FILES内是否有任何内容。如果已填充,只需查看密钥名称,然后使用该密钥访问该文件。

但是如果数组为空,则PHP或APACHE中可能存在一些错误配置。

一个可能的解决方法是在php的ini文件中设置file_uploads = On

希望它有所帮助!

答案 1 :(得分:0)

  1. 如果要进行isset,则必须验证文件的大小。我不知道这是否有效,但更好的方法是首先检查验证的大小是否为isset或是否发送到服务器。

  2. 然后,在您的<form method='post' action='uplaodex.php' enctype='multipart/form-data'>中,您必须创建另一个名为uplaodex.php的PHP文件,您将在其中发送数据。所以,你的代码如下面的代码并考虑了第1步。这将是你的uploadex.php

    $name_file       = $_FILES['picName']['name'];
    $type            = $name_file['type'];
    $size            = $name_file['size'];
    $tmp_folder      = $name_file['tmp'];
    $usName          = trim($_POST['usName']);
    $usUsername      = trim($_POST['usUsername']);
    $usPass          = trim($_POST['usPass']);
    $usEmail         = trim($_POST['usEmail']);
    
    if ( $size > 0 ) {
        //REMOVE another slash images//
        $name = "images/" . $name_file; //unix path uses forward slash
        //'filename' index comes from input type 'file' in the form above
        //
        //move uploaded file from temp to web directory
        if ( move_uploaded_file($tmp_folder, $name) ) {
           // Create the file DO and populate it.
            include 'Do_profile.php';
            $file             = new Do_profile();
            $file->FileName   = $name; //$name is initialised previously using $_FILES and file path
            $file->FileSize   = $size;
            $file->Type       = $type;
            $file->usName     = $usName;
            $file->usUsername = $usUsername;
            $file->usPass     = $usPass;
            $file->usEmail    = $usEmail;
                if ($file->save()) {   
                        //USE PRINTF
                        printf('<h1> Thankyou </h1><p>Image stored successfully</p><br> 
                                <p>Uploaded file: %s</p>. <img src="%s" height="200" width="200" />',
                                $name_file, $name );
    
                        #WHAT IS $fileId? In which moment was define?
                        //echo '<br><a href="Display.php?id=' . $fileId . '">Display image ' .
                        $file->FileName . '</a>';
                } 
    
                else
                    echo '<p class="error">Error retrieving file information</p>';
                }
    
        else {
            echo '<p class="error"> Oh dear. There was a databse error</p>';
        } //ENDIF OF if (move_uploaded_file($_FILES['picName']['tmp_name'], $name))
    
    } //ENDIF OF  if ( $size > 0 )
    
    #ELSE OF YOUR if ( $size > 0 )
    else {
        //error handling in case move_uploaded_file() the file fails
        $error_array = error_get_last();
        echo "<p class='error'>Could not move the file</p>";
    
      //  foreach($error_array as $err)
        //    echo $err;
    }
    

答案 2 :(得分:0)

我解决了这个问题,你不能同时执行$ _FILES和$ _post,或者在另一个内执行其中一个。 从$ _Post开始,然后是$ _FILES,在$ _FILES之外运行你的保存功能

多数民众赞成