From a0a8b05fbefab9860660493e77115869fe3e0788 Mon Sep 17 00:00:00 2001 From: Eduardo Humberto Date: Wed, 3 Apr 2019 21:46:10 -0300 Subject: [PATCH] remove tmp file and write in correct file (#196) --- src/classes/class-tainacan-media.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/classes/class-tainacan-media.php b/src/classes/class-tainacan-media.php index 6730e4c7e..db7d587da 100644 --- a/src/classes/class-tainacan-media.php +++ b/src/classes/class-tainacan-media.php @@ -8,6 +8,7 @@ class Media { private static $instance = null; private static $file_handle = null; + private static $file_name = null; public static function get_instance() { if(!isset(self::$instance)) { @@ -67,11 +68,11 @@ class Media { set_time_limit(0); - $wp_upload_dir = wp_upload_dir(); - $filename = $wp_upload_dir['path'] . '/' . basename($url); + $filename = tempnam(sys_get_temp_dir(), basename($url)); # Open the file for writing... self::$file_handle = fopen($filename, 'w+'); + self::$file_name = $filename; $callback = function ($ch, $str) { $len = fwrite(self::$file_handle, $str); @@ -123,6 +124,13 @@ class Media { return false; } + if( @filesize($upload['file']) == 0 && is_resource($blob) ){ + $file_wordpress_stream = fopen( $upload['file'], 'r+'); + stream_copy_to_stream($blob, $file_wordpress_stream); + + if( file_exists(self::$file_name) ) unlink(self::$file_name); + } + $file_path = $upload['file']; $file_name = basename( $file_path ); $file_type = wp_check_filetype( $file_name, null );