0

i have this error when i am trying to upload a picture to the database and this is the code :

<?php   

include('connection.php');

if(isset($_POST['button'])){
    $file = $_FILES['file']['name'];
    $file_tmp = $_FILES['file']['tmp_name'];
    $random_name = rand();
             
    if(empty($file)){
        echo "Please choose a picture <br/><br/>";
    } else {
        move_uploaded_file($file_tmp, '../profilepics/'.$random_name.'.jpg');
        mysql_query("INSERT INTO userphoto VALUES ('$random_name.jpg')");
                 
        header('location: timeline.php');
    }
}                   
?>

and then its says :

Notice: Undefined index: file in C:\xampp\htdocs\Echo\php\userphotoupload.php on line 6

Notice: Undefined index: file in C:\xampp\htdocs\Echo\php\userphotoupload.php on line 7

Community
  • 1
  • 1
  • 1
    What error do you have? – Jay Blanchard Jul 08 '15 at 14:33
  • 1
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jul 08 '15 at 14:33
  • 1
    Does your form have `enctype` attribute? https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form – chris85 Jul 08 '15 at 14:38
  • 2
    Usually indicates missing or mislabeled form `` field. See also [Notice: Undefined index: file for $_FILES](https://www.google.de/search?q=site:stackoverflow.com+Notice:+Undefined+index:+file+for+%24_FILES+form) – mario Jul 08 '15 at 14:39
  • The key to the $_FILES array - `'file'` has to match the name attribute used on the HTML `` does yours? If all you have used is a `` that will not do – RiggsFolly Jul 08 '15 at 14:40
  • Also note just randomly selecting a number for the filenames isnt the best. The script could pick the same number twice. Why not use auto-incrementing column and pull the last inserted id (for the move, e.g. insert first then move)? – chris85 Jul 08 '15 at 14:43
  • Where are you `$_POST` values? – pattyd Jul 08 '15 at 15:17
  • Thanks guys i solved the problem – aiden pearce Jul 08 '15 at 23:53

0 Answers0