0

MAMP Server. Trying to upload image to database via this code:

$msg = "";
if(isset($_POST['upload'])) {
    $target = "cell-phones-store/".basename($_FILES['image']['name']);
    $db = mysqli_connect("localhost", "root", "root", "images");

    $image = $_FILES['image']['name'];
    $text = $_POST['text'];

    $sql = "INSERT INTO image (image, text) VALUES ('$image', '$text')";
    mysqli_query($db, $sql);

    if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
        $msg = "OK!";
    } else {
        $msg = "Error";
    }
}

But there's an error: 500 Internal Server Error without any warnings and any other information on error page.

Here's my .htaccess file:

php_value error_reporting -1
php_flag display_errors on
php_flag display_startup_errors on

php_flag ignore_repeated_source off
php_flag ignore_repeated_errors off

php_flag track_errors on

php_flag log_errors on

php_flag mysql.trace_mode on

php_value display_errors 1
php_value display_startup_errors 1
php_value error_reporting E_ALL

Also tried this code in php.ini:

error_reporting = E_ALL
display_errors On

display_startup_errors = On

Why an error occurs uploading image?

rinatoptimus
  • 427
  • 1
  • 5
  • 21

1 Answers1

0
  1. Please check error log file (you can get its path in apache config file)
  2. Your code $sql = "INSERT INTO image (image, text) VALUES ('$image', '$text')"; is wrong because $image and $text variables won't concatenate. Moreover, you should avoid concatenate variable to query and bind they instead of -- at least if you'd like to avoid SQL injection.
E.K.
  • 1,045
  • 6
  • 10
  • What do you mean "won't concatenate"? – Your Common Sense Jan 07 '17 at 17:37
  • I mean SQL server will get the same string as in $sql variable and $image and $text variable won't be replaced with their values – E.K. Jan 07 '17 at 17:55
  • I've corrected query to $sql = "INSERT INTO image (image) VALUES ('$image')"; but the same result. – rinatoptimus Jan 07 '17 at 18:36
  • but have you ever checked Apache error log file? – E.K. Jan 07 '17 at 18:47
  • why do you think that variables won't be replaced with values? – Your Common Sense Jan 07 '17 at 20:19
  • you are right, it's my bad – E.K. Jan 07 '17 at 21:56
  • File "nginx_error.log": failed (13: Permission denied), client: 127.0.0.1, server: , request: "POST /webformyself/simple-site/cell-phones-store/youtube-upload.php HTTP/1.1", host: "localhost:7888", referrer: "http://localhost:7888/webformyself/simple-site/cell-phones-store/youtube-upload.php" – rinatoptimus Jan 07 '17 at 22:03
  • perhaps your nginx user (usually it's www-data) unable to access to website root or file. Either set enough permissions or change nginx user to yours – E.K. Jan 08 '17 at 07:01