php - notification in stream_context_set_params() not called












0















I am using this code in php (Wordpress) to check for download progress:



// Create context
$context = stream_context_create();
stream_context_set_params( $context, [ 'notification' => 'my_stream_notification_callback' ] );

// Declare progress function
function my_stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {
print_r(func_get_args());
}

// Call download
$wp_upload_dir = wp_upload_dir();
file_put_contents( $wp_upload_dir['basedir'] . '/contact.htm', fopen( 'http://php.net/contact', 'r' ), null, $context );


The code does download the file successfully in the /wp-content/uploads/ folder, but does not print any notification / progress.



I tried also by writing to error_log() in the my_stream_notification_callback() function but it does not write anything there and the debug.log file is empty. Which means the notification callback is not being called at all.



Anyone has any idea why this would happen?



Here is a question with similar code but different problem:
Download files file_put_contents with progress
Evidently, the code works for him after the callback function was fixed for a class/object method function. While I am trying a simpler callback function that should definitely work.



Any ideas?



-- EDIT --



I found an example on php.net here:
http://php.net/manual/en/function.stream-notification-callback.php



I used that example and it seems to work.



See the code I used (a bit changed from the example):



$ctx = stream_context_create();
stream_context_set_params( $ctx, [
"notification" => function ( $notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max ) {
print_r(func_get_args());
},
] );

file_get_contents( "http://php.net/contact", false, $ctx );


Now this code works and prints out the progress notifications:



Does it mean that file_put_contents() or fopen() has problems with context/notifications that file_get_contents() do not have?



--EDIT--



Here is the change what actually works:



file_put_contents( $wp_upload_dir['basedir'] . '/contact.htm', fopen( 'http://php.net/contact', 'r', null, $context ) );


Means, instead of applying $context to file_put_contents() we apply it to fopen() call and it works!










share|improve this question





























    0















    I am using this code in php (Wordpress) to check for download progress:



    // Create context
    $context = stream_context_create();
    stream_context_set_params( $context, [ 'notification' => 'my_stream_notification_callback' ] );

    // Declare progress function
    function my_stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {
    print_r(func_get_args());
    }

    // Call download
    $wp_upload_dir = wp_upload_dir();
    file_put_contents( $wp_upload_dir['basedir'] . '/contact.htm', fopen( 'http://php.net/contact', 'r' ), null, $context );


    The code does download the file successfully in the /wp-content/uploads/ folder, but does not print any notification / progress.



    I tried also by writing to error_log() in the my_stream_notification_callback() function but it does not write anything there and the debug.log file is empty. Which means the notification callback is not being called at all.



    Anyone has any idea why this would happen?



    Here is a question with similar code but different problem:
    Download files file_put_contents with progress
    Evidently, the code works for him after the callback function was fixed for a class/object method function. While I am trying a simpler callback function that should definitely work.



    Any ideas?



    -- EDIT --



    I found an example on php.net here:
    http://php.net/manual/en/function.stream-notification-callback.php



    I used that example and it seems to work.



    See the code I used (a bit changed from the example):



    $ctx = stream_context_create();
    stream_context_set_params( $ctx, [
    "notification" => function ( $notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max ) {
    print_r(func_get_args());
    },
    ] );

    file_get_contents( "http://php.net/contact", false, $ctx );


    Now this code works and prints out the progress notifications:



    Does it mean that file_put_contents() or fopen() has problems with context/notifications that file_get_contents() do not have?



    --EDIT--



    Here is the change what actually works:



    file_put_contents( $wp_upload_dir['basedir'] . '/contact.htm', fopen( 'http://php.net/contact', 'r', null, $context ) );


    Means, instead of applying $context to file_put_contents() we apply it to fopen() call and it works!










    share|improve this question



























      0












      0








      0








      I am using this code in php (Wordpress) to check for download progress:



      // Create context
      $context = stream_context_create();
      stream_context_set_params( $context, [ 'notification' => 'my_stream_notification_callback' ] );

      // Declare progress function
      function my_stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {
      print_r(func_get_args());
      }

      // Call download
      $wp_upload_dir = wp_upload_dir();
      file_put_contents( $wp_upload_dir['basedir'] . '/contact.htm', fopen( 'http://php.net/contact', 'r' ), null, $context );


      The code does download the file successfully in the /wp-content/uploads/ folder, but does not print any notification / progress.



      I tried also by writing to error_log() in the my_stream_notification_callback() function but it does not write anything there and the debug.log file is empty. Which means the notification callback is not being called at all.



      Anyone has any idea why this would happen?



      Here is a question with similar code but different problem:
      Download files file_put_contents with progress
      Evidently, the code works for him after the callback function was fixed for a class/object method function. While I am trying a simpler callback function that should definitely work.



      Any ideas?



      -- EDIT --



      I found an example on php.net here:
      http://php.net/manual/en/function.stream-notification-callback.php



      I used that example and it seems to work.



      See the code I used (a bit changed from the example):



      $ctx = stream_context_create();
      stream_context_set_params( $ctx, [
      "notification" => function ( $notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max ) {
      print_r(func_get_args());
      },
      ] );

      file_get_contents( "http://php.net/contact", false, $ctx );


      Now this code works and prints out the progress notifications:



      Does it mean that file_put_contents() or fopen() has problems with context/notifications that file_get_contents() do not have?



      --EDIT--



      Here is the change what actually works:



      file_put_contents( $wp_upload_dir['basedir'] . '/contact.htm', fopen( 'http://php.net/contact', 'r', null, $context ) );


      Means, instead of applying $context to file_put_contents() we apply it to fopen() call and it works!










      share|improve this question
















      I am using this code in php (Wordpress) to check for download progress:



      // Create context
      $context = stream_context_create();
      stream_context_set_params( $context, [ 'notification' => 'my_stream_notification_callback' ] );

      // Declare progress function
      function my_stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {
      print_r(func_get_args());
      }

      // Call download
      $wp_upload_dir = wp_upload_dir();
      file_put_contents( $wp_upload_dir['basedir'] . '/contact.htm', fopen( 'http://php.net/contact', 'r' ), null, $context );


      The code does download the file successfully in the /wp-content/uploads/ folder, but does not print any notification / progress.



      I tried also by writing to error_log() in the my_stream_notification_callback() function but it does not write anything there and the debug.log file is empty. Which means the notification callback is not being called at all.



      Anyone has any idea why this would happen?



      Here is a question with similar code but different problem:
      Download files file_put_contents with progress
      Evidently, the code works for him after the callback function was fixed for a class/object method function. While I am trying a simpler callback function that should definitely work.



      Any ideas?



      -- EDIT --



      I found an example on php.net here:
      http://php.net/manual/en/function.stream-notification-callback.php



      I used that example and it seems to work.



      See the code I used (a bit changed from the example):



      $ctx = stream_context_create();
      stream_context_set_params( $ctx, [
      "notification" => function ( $notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max ) {
      print_r(func_get_args());
      },
      ] );

      file_get_contents( "http://php.net/contact", false, $ctx );


      Now this code works and prints out the progress notifications:



      Does it mean that file_put_contents() or fopen() has problems with context/notifications that file_get_contents() do not have?



      --EDIT--



      Here is the change what actually works:



      file_put_contents( $wp_upload_dir['basedir'] . '/contact.htm', fopen( 'http://php.net/contact', 'r', null, $context ) );


      Means, instead of applying $context to file_put_contents() we apply it to fopen() call and it works!







      php wordpress fopen file-put-contents






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 7:36







      Imran Ahmed

















      asked Nov 22 '18 at 7:08









      Imran AhmedImran Ahmed

      418




      418
























          1 Answer
          1






          active

          oldest

          votes


















          1














          As given in the documentation at http://php.net/stream_context_set_params, the notification callback is only triggered for ftp and http connections. As you use it to write something to a local directory (through another protocol), it's pretty obvious why the callback is never called.






          share|improve this answer
























          • Thanks Nico for your answer. But in this SO post (stackoverflow.com/questions/34292472/…) the user had it working for file_put_content(), is it some difference of php settings/config?

            – Imran Ahmed
            Nov 22 '18 at 7:20











          • Are you sure about this? He claims multiple times that all approaches are not working

            – Nico Haase
            Nov 22 '18 at 7:22






          • 1





            I think you are right, the guy has accepted the answer which makes it feel like his problem is solved. But the code actually does not work. I have made a change in my code a little and it works. The idea is what you said that context is applied to file writing, while it should be applied to file reading part. Let me update my question with the fix.

            – Imran Ahmed
            Nov 22 '18 at 7:35











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425548%2fphp-notification-in-stream-context-set-params-not-called%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          As given in the documentation at http://php.net/stream_context_set_params, the notification callback is only triggered for ftp and http connections. As you use it to write something to a local directory (through another protocol), it's pretty obvious why the callback is never called.






          share|improve this answer
























          • Thanks Nico for your answer. But in this SO post (stackoverflow.com/questions/34292472/…) the user had it working for file_put_content(), is it some difference of php settings/config?

            – Imran Ahmed
            Nov 22 '18 at 7:20











          • Are you sure about this? He claims multiple times that all approaches are not working

            – Nico Haase
            Nov 22 '18 at 7:22






          • 1





            I think you are right, the guy has accepted the answer which makes it feel like his problem is solved. But the code actually does not work. I have made a change in my code a little and it works. The idea is what you said that context is applied to file writing, while it should be applied to file reading part. Let me update my question with the fix.

            – Imran Ahmed
            Nov 22 '18 at 7:35
















          1














          As given in the documentation at http://php.net/stream_context_set_params, the notification callback is only triggered for ftp and http connections. As you use it to write something to a local directory (through another protocol), it's pretty obvious why the callback is never called.






          share|improve this answer
























          • Thanks Nico for your answer. But in this SO post (stackoverflow.com/questions/34292472/…) the user had it working for file_put_content(), is it some difference of php settings/config?

            – Imran Ahmed
            Nov 22 '18 at 7:20











          • Are you sure about this? He claims multiple times that all approaches are not working

            – Nico Haase
            Nov 22 '18 at 7:22






          • 1





            I think you are right, the guy has accepted the answer which makes it feel like his problem is solved. But the code actually does not work. I have made a change in my code a little and it works. The idea is what you said that context is applied to file writing, while it should be applied to file reading part. Let me update my question with the fix.

            – Imran Ahmed
            Nov 22 '18 at 7:35














          1












          1








          1







          As given in the documentation at http://php.net/stream_context_set_params, the notification callback is only triggered for ftp and http connections. As you use it to write something to a local directory (through another protocol), it's pretty obvious why the callback is never called.






          share|improve this answer













          As given in the documentation at http://php.net/stream_context_set_params, the notification callback is only triggered for ftp and http connections. As you use it to write something to a local directory (through another protocol), it's pretty obvious why the callback is never called.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 7:16









          Nico HaaseNico Haase

          2,58992032




          2,58992032













          • Thanks Nico for your answer. But in this SO post (stackoverflow.com/questions/34292472/…) the user had it working for file_put_content(), is it some difference of php settings/config?

            – Imran Ahmed
            Nov 22 '18 at 7:20











          • Are you sure about this? He claims multiple times that all approaches are not working

            – Nico Haase
            Nov 22 '18 at 7:22






          • 1





            I think you are right, the guy has accepted the answer which makes it feel like his problem is solved. But the code actually does not work. I have made a change in my code a little and it works. The idea is what you said that context is applied to file writing, while it should be applied to file reading part. Let me update my question with the fix.

            – Imran Ahmed
            Nov 22 '18 at 7:35



















          • Thanks Nico for your answer. But in this SO post (stackoverflow.com/questions/34292472/…) the user had it working for file_put_content(), is it some difference of php settings/config?

            – Imran Ahmed
            Nov 22 '18 at 7:20











          • Are you sure about this? He claims multiple times that all approaches are not working

            – Nico Haase
            Nov 22 '18 at 7:22






          • 1





            I think you are right, the guy has accepted the answer which makes it feel like his problem is solved. But the code actually does not work. I have made a change in my code a little and it works. The idea is what you said that context is applied to file writing, while it should be applied to file reading part. Let me update my question with the fix.

            – Imran Ahmed
            Nov 22 '18 at 7:35

















          Thanks Nico for your answer. But in this SO post (stackoverflow.com/questions/34292472/…) the user had it working for file_put_content(), is it some difference of php settings/config?

          – Imran Ahmed
          Nov 22 '18 at 7:20





          Thanks Nico for your answer. But in this SO post (stackoverflow.com/questions/34292472/…) the user had it working for file_put_content(), is it some difference of php settings/config?

          – Imran Ahmed
          Nov 22 '18 at 7:20













          Are you sure about this? He claims multiple times that all approaches are not working

          – Nico Haase
          Nov 22 '18 at 7:22





          Are you sure about this? He claims multiple times that all approaches are not working

          – Nico Haase
          Nov 22 '18 at 7:22




          1




          1





          I think you are right, the guy has accepted the answer which makes it feel like his problem is solved. But the code actually does not work. I have made a change in my code a little and it works. The idea is what you said that context is applied to file writing, while it should be applied to file reading part. Let me update my question with the fix.

          – Imran Ahmed
          Nov 22 '18 at 7:35





          I think you are right, the guy has accepted the answer which makes it feel like his problem is solved. But the code actually does not work. I have made a change in my code a little and it works. The idea is what you said that context is applied to file writing, while it should be applied to file reading part. Let me update my question with the fix.

          – Imran Ahmed
          Nov 22 '18 at 7:35


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425548%2fphp-notification-in-stream-context-set-params-not-called%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          To store a contact into the json file from server.js file using a class in NodeJS

          Redirect URL with Chrome Remote Debugging Android Devices

          Dieringhausen