Question

I have quite a difficult question. I want to upload files from dropbox, using the DropPHP class. Unfortunately, my shared hosting environment cannot write files on the web server. The only option is using the tmp folder, or to the database.

Since DropPHP iuses the custom function DownloadFile() to download a file to the webserver, I have to change the function to make it write to the tmp folder. How do I do that?? I am not familiar with tmp yet...

The function is the following:

public function DownloadFile($dropbox_file, $dest_path='', $rev=null, $progress_changed_callback = null)
    {
        if(is_object($dropbox_file) && !empty($dropbox_file->path))
            $dropbox_file = $dropbox_file->path;

        if(empty($dest_path)) $dest_path = basename($dropbox_file);

        $url = $this->cleanUrl(self::API_CONTENT_URL."/files/$this->rootPath/$dropbox_file");
        $content = (!empty($rev)) ? http_build_query(array('rev' => $rev),'','&') : null;
        $context = $this->createRequestContext($url, "GET", $content);

        $fh = @fopen($dest_path, 'wb'); // write binary
        if($fh === false) {
            @fclose($rh);
            throw new DropboxException("Could not create file $dest_path !");
        }

        if($this->useCurl) {
            curl_setopt($context, CURLOPT_BINARYTRANSFER, true);
            curl_setopt($context, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($context, CURLOPT_FILE, $fh);
            $response_headers = array();
            self::execCurlAndClose($context, $response_headers);
            fclose($fh);
            $meta = self::getMetaFromHeaders($response_headers);
            $bytes_loaded = filesize($dest_path);
        } else {
            $rh = @fopen($url, 'rb', false, $context); // read binary
            if($rh === false)
                throw new DropboxException("HTTP request to $url failed!");



            // get file meta from HTTP header
            $s_meta = stream_get_meta_data($rh);
            $meta = self::getMetaFromHeaders($s_meta['wrapper_data']);
            $bytes_loaded = 0;
            while (!feof($rh)) {
              if(($s=fwrite($fh, fread($rh, self::BUFFER_SIZE))) === false) {
                @fclose($rh);
                @fclose($fh);
                throw new DropboxException("Writing to file $dest_path failed!'");
              }
              $bytes_loaded += $s;
              if(!empty($progress_changed_callback)) {
                call_user_func($progress_changed_callback, $bytes_loaded, $meta->bytes);
              }
            }

            fclose($rh);
            fclose($fh);
        }

        if($meta->bytes != $bytes_loaded)
            throw new DropboxException("Download size mismatch!");

        return $meta;
    }
Was it helpful?

Solution

Found it! It was the following line I had to change:

curl_setopt($context, CURLOPT_FILE, $fh); 

If I remove it, the file is returned as a binary string if I do this: replace this

self::execCurlAndClose($context, $response_headers);

by this

$thefilebinarystring = self::execCurlAndClose($context, $response_headers);

Or if I want to write to a tmp file, do not remove the line I showed above, but replace $fh by the $temp file you create before with $temp = tmp file()

However, I haven't updated the rest of the code yet, but this is the core!

Thank for your great answer, myself! ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top