error in uploading file into mysql database in php -
i'm getting error when upload file mysql: "error! file not sent!".
here php code please , let me know error in code.
<!doctype html> <head> <title>mysql file upload example</title> <meta http-equiv="content-type" content="text/html; charset=utf-8"> </head> <body> <form action="add_file.php" method="post" enctype="multipart/form-data"> <input type="file" name="uploaded_file"><br> <input type="submit" value="upload file"> </form> <p> <a href="list_files.php">see files</a> </p> </body> </html>
add_file.php
<?php // check if file has been uploaded if(isset($_files['uploaded_file'])) { // make sure file sent without errors if($_files['uploaded_file']['error'] == 0) { // connect database $dblink = new mysqli('127.0.0.1', 'user', 'pwd', 'mytable'); if(mysqli_connect_errno()) { die("mysql connection failed: ". mysqli_connect_error()); } // gather required data $name = $dblink->real_escape_string($_files['uploaded_file']['name']); $mime = $dblink->real_escape_string($_files['uploaded_file']['type']); $data = $dblink->real_escape_string(file_get_contents($_files ['uploaded_file'] ['tmp_name'])); $size = intval($_files['uploaded_file']['size']); // create sql query $query = " insert `file` ( `name`, `mime`, `size`, `data`, `created` ) values ( '{$name}', '{$mime}', {$size}, '{$data}', now() )"; // execute query $result = $dblink->query($query); // check if successfull if($result) { echo 'success! file added!'; } else { echo 'error! failed insert file' . "<pre>{$dblink->error}</pre>"; } } else { echo 'an error accured while file being uploaded. ' . 'error code: '. intval($_files['uploaded_file']['error']); } // close mysql connection $dblink->close(); } else { echo 'error! file not sent!'; } // echo link main page echo '<p>click <a href="index.html">here</a> go back</p>'; ?>
if http upload disabled
when doing
if(ini_get('file_uploads') == 1){ echo 'http upload enabled'; } else { echo 'http upload disabled'; }
file_uploads
on server turned off default
if have access / , can modify php.ini
file -
file_uploads = 1
in .htaccess
file -
php_value file_uploads 1
or @ top php page
<?php ini_set('file_uploads',1); ?>
see http://www.php.net/manual/en/ini.core.php#ini.file-uploads
Comments
Post a Comment