-1

I've recently migrated my website to PHP 7.3. There is an input box where I allow the user to upload a picture using their media device. Since upgrading, I get a few "Use of undefined constant..." errors.

Here is the PHP code:

function hasGetUserMedia() {
  return !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia);
}

if (hasGetUserMedia()) {
  echo '<input type="file" accept="image/* application/pdf" capture="camera" name="expimage" id="expimage" onchange="setExif(this.files)" />';
} 

Although the code works, the webpage shows the following warnings:

Warning
: Use of undefined constant navigator - assumed 'navigator' (this will throw an Error in a future version of PHP)

Warning
: Use of undefined constant mediaDevices - assumed 'mediaDevices' (this will throw an Error in a future version of PHP) 

Warning
: Use of undefined constant navigator - assumed 'navigator' (this will throw an Error in a future version of PHP)

Warning
: Use of undefined constant mediaDevices - assumed 'mediaDevices' (this will throw an Error in a future version of PHP)

Warning
: Use of undefined constant getUserMedia - assumed 'getUserMedia' (this will throw an Error in a future version of PHP)

I'm following WebRTC. What is the new format for defining the constants?

Ghonima
  • 2,978
  • 2
  • 14
  • 23

1 Answers1

2

This code never worked, and is a good example of why those messages, which you previously missed as Notices, are now Warnings, and will soon be Errors.

The only reason it appeared to work is because the line

!!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia);

Is interpreted by PHP as concatenating a bunch of strings, giving you

!!('navigatormediaDevices' && 'navigatormediaDevicesgetUserMedia')

Which by PHP's type juggling rules is

!!(true && true)

Which is just a very elaborate way of writing

true

Your underlying problem in this case is that you've muddled up JS (which can detect information about the user's browser, and where that line would do something useful) with PHP (which runs on the server before the browser is involved).

Probably, you want to always echo the form control in the PHP, and then show and hide it on the JS. But first of all, you need to read some tutorials about how the two fit together, so you don't get in a muddle like this again.

IMSoP
  • 89,526
  • 13
  • 117
  • 169