Results 1 to 2 of 2

Thread: How can I change create_function to anonymous function?

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,410
    Thanks
    101
    Thanked 115 Times in 113 Posts

    Default How can I change create_function to anonymous function?

    I am in the lengthy process of modernizing a lot of code on my website to make it php 8.x compliant. I have never really gotten the hang of user defined functions, so the following code was not created by me.

    I am trying to change create_function, which has been removed as of php 8.0, to an anonymous function.

    I removed the security lines as they were irrelevant.


    Any hints as to how I should proceed?

    Code:
    print_r($_FILES)
    
    ##produces
    
    $_FILES is Array ( [pix] => Array ( [name] => christmas every day.jpg [type] => image/jpeg [tmp_name] => /tmp/phpWPefKh [error] => 0 [size] => 91284 ) )
    Here is the outdated code:

    Code:
    <?php
    $end=substr($_FILES['pix']['name'],-3,3);
    $end=strtolower($end);
    if($end=="jpg"||$end=="peg"||$end=="gif"||$end=="png") {}
    else {echo"<script type=\"text/javascript\">
     window.location = \"http://www.animeviews.com\"
     </script>";
    exit;
    }
    
    function or_f($a, $b) {
      return $a || $b;
    }
    
    function file_has_extension($fn, $ext) {
      if(is_array($ext))
        return array_reduce(array_map(create_function('$a', 'return file_has_extension(\'' . $fn . '\', $a);'), $ext), 'or_f', false);
      else
        return stripos($fn, '.' . strtolower($ext)) === strlen($fn) - strlen($ext) + 1;
    }
    
    $image_extensions = array(
      'png',
      'jpg',
      'jpeg',
      'gif'
    );
      if(!isset($_POST['Upload']) and !isset($_POST['Overwrite']))
      {
        include("picform.php");
      } # endif
      else
      {
        if($_FILES['pix']['tmp_name'] == "none")
        {
          echo "<b>File did not successfully upload. Check the
                file size. File must be less than 500K.<br>";
          include("picform.php");
          exit();
        }
    if(file_has_extension($_FILES['pix']['name'], $image_extensions))
        {
          echo "<b>File is not a picture. Please try another
                file.</b><br>";
          include("picform.php");
          exit();
        }
        else
        {
          $destination = $_FILES['pix']['name'];
          $destination = strtolower($destination);
          $temp_file = $_FILES['pix']['tmp_name'];
    if (file_exists($destination) and $_POST['Overwrite']!="Overwrite") {
        echo "The file $destination already exists.";exit();
    }
    #########
          move_uploaded_file($temp_file,$destination);
          echo "<p><b>The file has successfully uploaded:</b>
                {$destination}
                ({$_FILES['pix']['size']})</p>";
    
    $img="http://www.animeviews.com/images/$destination";
    
    $img    = $destination;
    $width  = "180px";
    $height = "";
    
    // Get new dimensions
    list($width1, $height1) = getimagesize($img);
    if ($width =="") {$j=$height/$height1; $width  = $width1  * $j;}
    if ($height=="") {$j=$width/$width1;   $height = $height1 * $j;}
    $new_width = $width;
    $new_height = $height;
    
    // Resample
    $image_p = imagecreatetruecolor($width, $height);
    $image = imagecreatefromjpeg($img);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width1, $height1);
    
    // Output
    $img=preg_replace('/h.*\//','',$img);
    $img=strtolower($img);
    $dest="thumbs/$img";
    imagejpeg($image_p,$dest);
    
        }
      }
    ?>
    To choose the lesser of two evils is still to choose evil. My personal site

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,410
    Thanks
    101
    Thanked 115 Times in 113 Posts

    Default

    It appears that I need to make changes to the following lines:

    Code:
    function file_has_extension($fn, $ext) {
      if(is_array($ext))
        return array_reduce(array_map(create_function('$a', 'return file_has_extension(\'' . $fn . '\', $a);'), $ext), 'or_f', false);
      else
        return stripos($fn, '.' . strtolower($ext)) === strlen($fn) - strlen($ext) + 1;
    }
    I am trying to follow the advice from the following blog post: Function create_function() is Deprecated in PHP 7.2 - How to Migrate?

    Here is a simpler example given from the article:

    Code:
    $callback = create_function('$matches', "return strtolower(\$matches[1]);");
    is refactored into:

    Code:
    $callback = function ($matches) {
        return strtolower($matches[1]);
    };
    My research into this continues...
    To choose the lesser of two evils is still to choose evil. My personal site

Similar Threads

  1. Self fulfilling anonymous function
    By jscheuer1 in forum JavaScript
    Replies: 0
    Last Post: 07-26-2016, 08:17 PM
  2. Resolved PHP version change, using anonymous functions
    By djr33 in forum PHP
    Replies: 2
    Last Post: 02-21-2014, 04:49 AM
  3. Replies: 1
    Last Post: 06-11-2010, 08:38 AM
  4. Help change compose function
    By oriecat in forum JavaScript
    Replies: 4
    Last Post: 10-26-2008, 01:27 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •