@@ -359,10 +359,8 @@ public function mediaStatus(string $media_id)
359359 */
360360 private function uploadMediaNotChunked (string $ path , array $ parameters )
361361 {
362- if (
363- !is_readable ($ parameters ['media ' ]) ||
364- ($ file = file_get_contents ($ parameters ['media ' ])) === false
365- ) {
362+ $ this ->validateMediaPath ($ parameters ['media ' ]);
363+ if (($ file = file_get_contents ($ parameters ['media ' ])) === false ) {
366364 throw new \InvalidArgumentException (
367365 'You must supply a readable file ' ,
368366 );
@@ -383,6 +381,7 @@ private function uploadMediaNotChunked(string $path, array $parameters)
383381 */
384382 private function uploadMediaChunked (string $ path , array $ parameters )
385383 {
384+ $ this ->validateMediaPath ($ parameters ['media ' ]);
386385 /** @var object $init */
387386 $ init = $ this ->http (
388387 'POST ' ,
@@ -430,6 +429,26 @@ private function uploadMediaChunked(string $path, array $parameters)
430429 return $ finalize ;
431430 }
432431
432+ /**
433+ * Validate upload media path before reading from disk.
434+ *
435+ * @param string $mediaPath
436+ */
437+ private function validateMediaPath (string $ mediaPath ): void
438+ {
439+ if (
440+ !stream_is_local ($ mediaPath ) ||
441+ !file_exists ($ mediaPath ) ||
442+ !is_file ($ mediaPath ) ||
443+ is_link ($ mediaPath ) ||
444+ !is_readable ($ mediaPath )
445+ ) {
446+ throw new \InvalidArgumentException (
447+ 'You must supply a readable file ' ,
448+ );
449+ }
450+ }
451+
433452 /**
434453 * Private method to get params for upload media chunked init.
435454 * Twitter docs: https://dev.x.com/rest/reference/post/media/upload-init.html
0 commit comments