EverKB

Knowledge Base Ever

Kako koristiti CURL za prijenos datoteka u PHP

2019-11-18 23:43:46

oznaka: PHP, PHP5.5, cURL, CURLOPT_POSTFIELDS, File Upload, CURLFille, PHP datoteka za prijenos

PHP koristi biblioteku libcurl koju je stvorio Daniel Stenberg za povezivanje s različitim poslužiteljima. Protokoli koje podržava su http、https、ftp、gopher、telnet、dict、file、ldap. Libcurl također podržava HTTPS certifikate, POST, PUT, FTP itd. Također podržava prijenos na temelju oblika, proxy, kolačiće, korisničko ime i lozinku.

PHP koristi cURL knjižnicu za pokretanje ovih HTTP zahtjeva. cURL ovisi o libcurl7.10.5 ili iznad.

U PHP 5.4 ili starijim verzijama, inkapsulacija datoteka za prijenos CURL proceduralna je:

$ch = curl_init();
$data = array('name' => 'File1', 'file' => '@/path/to/file.png');
curl_setopt($ch, CURLOPT_URL, 'https://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);

U PHP 5.5 ili novijoj verziji, inkapsulacija datoteke za prijenos CURL ažurira se u klasu CURLFille:

$ch = curl_init();
$data = array('name' => 'File1', 'file' => new CURLFille('/path/to/file.png'));
curl_setopt($ch, CURLOPT_URL, 'https://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);

oznaka

CentOS7 CentOS PHP Linux Python3 Python MySQL JavaScript MariaDB YUM Redis MySQL8 caching_sha2_password HTML mysql_native_password PHP7 CSS PHPUnit Framework ss5.passwd at-symbol Error Control Operator margin password SOCKET vps daemon process IIS sudoers center Fatal Errors MongoDB4 Phalcon SQL Server 2017 WiFi .htaccess DATE_SUB set_error_handler TestCase Compiling PHP frequency conflict PHPUnit ss5.conf apr Elasticsearch7.3 particles.js Slim Vincent Garreau daemon HTTP Sudo Failed to build these modules: _uuid MongoDB pg_bha.conf SQL Server width DATE_ADD ip command scrollbar Tcp BBR Compiling Apache Frameworks MySQL-python PHPStorm ss5-3.8.9 Apache2.4 Elasticsearch libpython3.7m.so.1.0 show VIM CURLOPT_POSTFIELDS NodeJs substr Bluetooth Failed to build these modules: _ctypes Minimal ISO PECL SQL Date Funcitons Wi-Fi date sub INTERVAL root Tag Cloud CodeIgniter font-size ss5 apache httpd dual network interfaces Laravel ShadowsocksR vertical-align CURLFille horiz-align node.js PyCharm SUBDATE BarManager exec mb_substr PEAR SOCKS5 Proxy Server WebStorm date add IntelliJIDEA remi Tag Chrome find PHP5.5 src Apache du jQuery Shadowsocks-Python V2Ray cURL hide nginx PostgreSQL sub folder availability eth0 MariaDB10.4 pdo_sqlsrv SOCKS5 Web Server date Install MongoDB register_shutdown_function Symfony File Upload mssql-tools php-fpm sqlsrv Zend Framework ADDDATE DevExpress JetBrain Shadowsocks-Libev unixODBC-devel height network interface Port 80 string Aura error_get_last password policy requirements SOCKS Web Development data INDENTIFIED BY Redis5 SVN Fedora Media Writer msodbcsql SQLAlchemy absolute Deepin jemalloc Shadowsocks toggle convert FuelPHP mysqlclient