注意:未定义的索引:“$ file = $ _POST ['file'];”

时间:2013-11-17 17:27:34

标签: php

<?php

$file = $_POST['file'];

            $conn = mysql_connect("localhost","root","") or die(mysql_error);
                $db = mysql_select_db("no_tiny");


?>

<?php





if (isset($_FILES['file']['name'])) {
    $File_Name = $_FILES['file']['name'];

    $File_Extension = strtolower(substr($File_Name,strpos ($File_Name,'.')+1));

    $File_Type = $_FILES['file']['type'];

    $File_Size = $_FILES['file']['size'];

    $max_Size = 200000;


    $tmp_name = $_FILES ['file']['tmp_name'];


    if (!empty($File_Name)){

        if(($File_Extension == 'jpeg' || $File_Extension == 'jpg') && $File_Type == 'image/jpeg'  || $File_Type == 'image/jpg' && $File_Size <= $max_Size ){    

            $File_Location = 'Upload/';
            if(move_uploaded_file($tmp_name, $File_Location.$File_Name)){

                echo 'File has been Successfully Uploaded';
                $sql = "INSERT into yes (Image) values ('$file')";
                $q = mysql_query($sql); 


            }else{
                echo 'An Error was Encounter while Uploading. ';
            }
        }else{
            echo 'File must be jpeg/jpg and 2.5mb less';
        }
    }else {
        echo 'Noob';
    }


}

?>


<form action = "upload.php" method = "POST" enctype = "multipart/form-data">
<input type = "file" name =  "file"><br /> <br />
<input type =  "submit" value = "Upload">

</form>

所以我不断得到这个$file = $_POST['file'];未定义的索引。 我可以存储照片,但似乎可以在我的sql中存储照片的文件名。

2 个答案:

答案 0 :(得分:3)

因为fie是一个不通过帖子访问的文件,你应该试试$ _FILES ['file']

答案 1 :(得分:1)

使用$_FILES['file']代替$_POST。使用$ _POST无法访问文件。 此外,

文件名未更新的原因,请使用变量$File_Name代替$file

  $sql = "INSERT into yes (Image) values ('$File_Name')";
  $q = mysql_query($sql); 

$_FILES - HTTP文件上传变量

print_r($_FILES['file']);

示例输出:

[file] => Array
    (
        [name] => filename (comes from the browser, so treat as tainted)
        [type] => file type  (not sure where it gets this from - assume the browser, so treat as tainted)
        [tmp_name] => /tmp/php/php1h4j1o (could be anywhere on your system, depending on your config settings, but the user has no control, so this isn't tainted)
        [error] => UPLOAD_ERR_OK  (= 0)
        [size] => 123   (the size in bytes)
    )

参考:http://php.net/manual/en/reserved.variables.files.php