0

I am new to php, taking my first course now. I cannot, for the life of me, figure out why only $screenshot = $_FILES["screenshot"]["name"] needs double quotes. I originally had single quotes and it wasn't working. I randomly decided to try double quotes and it started working... can somebody tell me why?

<?php
  // Define the upload path and maximum file size constants
  define('GW_UPLOADPATH', 'images/');

  if (isset($_POST['submit'])) {
    // Grab the score data from the POST
    $name = $_POST['name'];
    $score = $_POST['score'];
    $screenshot = $_FILES["screenshot"]["name"];

    if (!empty($name) && !empty($score)) {
      // Move the file to the targe upload folder
      $target = GW_UPLOADPATH . $screenshot;
      // Connect to the database
      $dbc = mysqli_connect('localhost', 'root', 'Bandito8', 'gwdb')
        or die('Unable to connect to databse');

      // Write the data to the database
      $query = "INSERT INTO guitarwars VALUES (0, NOW(), '$name', '$score', '$screenshot')";
      mysqli_query($dbc, $query)
        or die('Unable to complete query');

      // Confirm success with the user
      echo '<p>Thanks for adding your new high score!</p>';
      echo '<p><strong>Name:</strong> ' . $name . '<br>';
      echo '<strong>Score:</strong> ' . $score . '</p>';
      echo '<img src="' . GW_UPLOADPATH . $screenshot . '" alt="Score image"></p>';
      echo '<p><a href="index.php">&lt;&lt; Back to high scores</a></p>';

      // Clear the score data to clear the form
      $name = "";
      $score = "";
      $screenshot = "";

      mysqli_close($dbc);
    }
    else {
      echo '<p class="error">Please enter all of the information to add your high score.</p>';
    }
  }
?>
Alessandro
  • 900
  • 12
  • 23
Melvin
  • 150
  • 1
  • 17
  • 1
    Possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – Pedro Lobito Apr 30 '17 at 18:58
  • 2
    `$screenshot = $_FILES["screenshot"]["name"]` and `$screenshot = $_FILES['screenshot']['name']` should work in exactly the same way; you must have changed something else as well – Mark Baker Apr 30 '17 at 18:58
  • It should work with single quotes, i believe there must be some error, try `var_dump($_FILES['screenshot']['error'])` and check the error detail [here](http://php.net/manual/en/features.file-upload.errors.php) – Mahesh Singh Chouhan Apr 30 '17 at 19:00
  • I got it working with single quotes somehow, after lots of trial and error changes using var_dump - thank you Mahesh! – Melvin Apr 30 '17 at 21:09

1 Answers1

0

The php parser try to interpret string in double quotes as variable while string in single quotes are interpreted as literally string...

the php interpret is searching for variable and takes more php time since it doesn't find any variable...

echo $_FILES["upload"]["size"];

the php interpret is searching for string and takes less php time since it hasn't to find any variable...

echo $_FILES['upload']['size'];

Another example:

this example treats $hidden as variable and works fine...

echo "the red fox was $hidden to the hunter";

this example treats $hidden as string and cannot works as expected...

echo 'the red fox was $hidden to the hunter';

Hope this helps!

Alessandro
  • 900
  • 12
  • 23