Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

upload a file to my web directory

I'm trying to upload a file (png) to my web directory using an URL:

$url = "http://api.qrserver.com/v1/create-qr-code/?data=hello_word&size=100x100";
$file = file_get_contents($url);
$fileName = "filename";
$dir = $this->get('kernel')->getRootDir() . '/../web/uploads/img';
$file->move( $dir , $fileName);

Warning: file_get_contents(http://api.qrserver.com/v1/create-qr-code/?data=hello_word&size=100x100): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request

like image 668
Achraf Avatar asked Nov 20 '25 01:11

Achraf


1 Answers

$fileName = "filename";
$dir = $this->get('kernel')->getRootDir() . 
'/../web/uploads/img'.$fileName;
$ch = curl_init('http://api.qrserver.com/v1/create-qr-code/?
data=hello_word&size=100x100');
$fp = fopen($dir, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

Try something like that. If that doesn't work i'd consider using. http://php.net/manual/en/function.file-put-contents.php

like image 147
Nathan Avatar answered Nov 21 '25 14:11

Nathan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!