I
I
Ilya Trusov2016-01-15 23:13:13
CodeIgniter
Ilya Trusov, 2016-01-15 23:13:13

How to upload files to a remote server?

Hello. How to upload files to remote server in codeigniter framework? I know that there is a class for working with ftp, but then you need to first create a file on the first server, and then copy it to the second. It's not cool when there are 1000+ files in the stream. Is there a way without a temporary file?
For now I'm using the loading class in codeigniter.
I process pictures using newImage. I save to the server. You need to implement save to remote.
PS Implementation example with the creation of a temporary file.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin B., 2016-01-16
@artgrosvil

Here is a primitive one, not for CI, but understandable, you can’t do without uploading to the server, but it’s done differently, we just upload it to a temporary folder and immediately upload it from it.

if (isset($_FILES)){
      foreach ($_FILES['uploadfile']['name'] as $k=>$v){

            if($_FILES['uploadfile']['type'][$k] == "image/gif" || $_FILES['uploadfile']['type'][$k] == "image/png" || $_FILES['uploadfile']['type'][$k] == "image/jpg" || $_FILES['uploadfile']['type'][$k] == "image/jpeg"){
                $blacklist = array(".php", ".phtml", ".php3", ".php4");
                foreach ($blacklist as $item){
                    if(preg_match("/$item\$/i", $_FILES['uploadfile']['name'][$k])){
                        echo "Нельзя загружать скрипты.";
                        exit;
                    }
                }
        
        # сюда загружается файл на основном сервере
        $file = $_FILES['uploadfile']['tmp_name'][$k];
        
                                # адрес сервера
        $url = 'http://s1.site.ru/upload.php';
        
        $post = [
              'image'=> new CURLFile($file, mime_content_type($file), date('YmdHis').rand(100,1000)),
            ];
        
        $ch = curl_init($url);

        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        
        $result = curl_exec($ch);

            }
        }      				
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question