-1
<?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>

So i keep getting this $file = $_POST['file']; undefined index. i can store the photo but seems could store the file name of the photo in my sql.

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • This question appears to be off-topic because it is a duplicate of the [official documentation](http://php.net/manual/en/features.file-upload.php). – PeeHaa Nov 17 '13 at 17:30

2 Answers2

3

because fie is a file its not accessed via post, you should try $_FILES['file']

Syed Sajid
  • 1,380
  • 5
  • 20
  • 34
1

Use $_FILES['file'] instead of $_POST. File cannot be accessed using $_POST. Also,

Reason for filename not being updated, use variable $File_Name instead of $file

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

$_FILES - HTTP File Upload variables

print_r($_FILES['file']);

Sample output:

[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)
    )

Ref: http://php.net/manual/en/reserved.variables.files.php

Krish R
  • 22,583
  • 7
  • 50
  • 59