Strict Standards: Only variables should be passed by referen

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
attaboy
Forum Newbie
Posts: 13
Joined: Wed Apr 10, 2013 7:09 pm

Strict Standards: Only variables should be passed by referen

Post by attaboy »

This code works as intended( thanks in part to people in this forum) but I keep getting this warning.
Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\dpSite\new_upload.php on line 20
this is the script:

Code: Select all

<!doctype html>
<html>
<head>
<title> upload mp3 </title>
</head>
<body>
<form action="new_upload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" id="submit" value="Submit">
</form>

<?php

if (isset($_POST["submit"])) {
  $path = './upload/';
  $maxFileSize = 1 * (1024 * 1024 * 20);     // 20Mb
  $allowedExts = array("mp3", "wma", "aif");
  $allowedMimes = array("audio/mpeg", "audio/x-ms-wma", "audio/x-aiff");
  $extension = end(explode(".", $_FILES["file"]["name"]));  // this line triggered a warning
  if (($_FILES["file"]["size"] < $maxFileSize) && in_array($_FILES["file"]["type"], $allowedMimes) && in_array($extension, $allowedExts)) {
    if ($_FILES["file"]["error"] > 0) {
      echo "Type: " . $_FILES["file"]["type"] . "<br>";
      echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    } else {
      echo "Upload: " . $_FILES["file"]["name"] . "<br>";
      echo "Type: " . $_FILES["file"]["type"] . "<br>";
      echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
      echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

      if (file_exists($path . $_FILES["file"]["name"])) {
        echo $_FILES["file"]["name"] . " already exists. ";
      } else {
        move_uploaded_file($_FILES["file"]["tmp_name"], $path . $_FILES["file"]["name"]);
        echo "Stored in: $path" . $_FILES["file"]["name"];
      }
    }
  } else {
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Invalid file<br/>";
    echo '<pre>' . print_r($_FILES) . '</pre>';
  }
}
?>
</body>
</html>
line 20 $extension = end(explode(".", $_FILES["file"]["name"])); is the one that was cited in the warning message.
Is it telling me that $extension should be passed by value and if so how would I do that?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Strict Standards: Only variables should be passed by ref

Post by McInfo »

The message tells you that end() expects the argument to be a variable. Your code passes it the unnamed value returned by explode(). For that code to work without the error, you would first have to allocate a variable for what comes out of explode(), then hand that variable to end(). However, a better solution is to use pathinfo() to get the value you are seeking.

Code: Select all

$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
attaboy
Forum Newbie
Posts: 13
Joined: Wed Apr 10, 2013 7:09 pm

Re: Strict Standards: Only variables should be passed by ref

Post by attaboy »

that absolutely fixed it.... thanks!
Post Reply