Search code examples
phplaraveltwitter-oauth

Media upload filesize(): stat failed for uploads error Laravel - TwitterOauth


I am using Abraham's https://twitteroauth.com/ package for sending twitter messages from Laravel App.

Also I scheduled messages with Laravel task scheduling. I also need to send media twitter message with this package. It works normally. But it does not work with laravel task schedule console command.

My twitter message sending code is like this:

                $screenshot = $this->takeScreenShot();
                $media = $this->twitter->upload('media/upload', array('media' => "uploads/$screenshot", 'media_type' => 'image/png'), true);
                $parameters = [
                    'status' => $twitterMsg,
                    'media_ids' => implode(',', [$media->media_id_string])
                ];
                $result = $this->twitter->post('statuses/update', 

$parameters);

It throws this error:

filesize(): stat failed for uploads/screenshots_1620116807.png

at vendor/abraham/twitteroauth/src/TwitterOAuth.php:417
  413▕             'shared',
  414▕         ];
  415▕         $base = [
  416▕             'command' => 'INIT',
➜ 417▕             'total_bytes' => filesize($parameters['media']),
  418▕         ];
  419▕         $allowed_parameters = array_intersect_key(
  420▕             $parameters,
  421▕             array_flip($allowed_keys),

    +3 vendor frames
4   app/Services/TwitterService.php:219
    Abraham\TwitterOAuth\TwitterOAuth::upload() 

How can I solve this error for console task command?

Thanks


Solution

  • This maybe because of the relative path you provided as uploads/screenshots_1620116807.png on media. Try to pass to the absolute path of your media, e.g. via storage_path or public_path helper methods:

    $media = $this->twitter->upload('media/upload', [
        'media' => public_path("uploads/$screenshot"),
        'media_type' => 'image/png',
    ], true);