Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP file_get_contents to read from string

So I am using the Solr client library Solarium PHP to control my Solr server. I want to index PDF documents which I download previously from my website. Since the website is huge, I wrote a crawler which uses the curl_multi_* functions to fetch the content. Now, I noticed that Solarium uses the following function internally to build up the request to post the PDF file to the Solr server, in solariumphp/vendor/solarium/solarium/src/Core/Client/Adapter/AdapterHelper.php:

    public static function buildUploadBodyFromRequest(Request $request): string
    {
        $baseName = basename($request->getFileUpload());
        $body = "--{$request->getHash()}\r\n";
        $body .= 'Content-Disposition: form-data; name="file"; filename="'.$baseName.'"';
        $body .= "\r\nContent-Type: application/octet-stream\r\n\r\n";
        $body .= file_get_contents($request->getFileUpload(), 'r');
        $body .= "\r\n--{$request->getHash()}--\r\n";

        return $body;
    }

The obstacle is now the line containing file_get_contents(), because actually the post content is not present as file but as string (it is already downloaded). I thought about working with streams (something like mem://), but I could not find out how to do it. May someone please help? If this is really not possible, I can pass the URL of the PDF file (and not download it previously), but then I lose some of the ability of parallel downloading from the website. Another ugly solution would be to save the downloaded files temporarily using file_put_contents beforehand, but I really do not want to do that.

like image 638
rexkogitans Avatar asked Dec 21 '25 16:12

rexkogitans


1 Answers

I use the following function for similar problems:

// simulate a file from given string
// return string fileName
function virtualFile(string $content):string
{
   return 'data://text/plain,'.urlencode($content);
}

Works great with file_get_contents too. A little test:

$myString = "abc"; 

$fileName = virtualFile($myString);

$myStringCopy = file_get_contents($fileName);

//test
var_dump($myString === $myStringCopy);  //bool(true)

Try self on 3v4l.org.

like image 55
jspit Avatar answered Dec 23 '25 05:12

jspit