PHP
last updated 2013-01
convert accented characters to non-accented
setlocale(LC_ALL, "en_US.utf8");
$translit = iconv("utf-8", "us-ascii//TRANSLIT", $_GET['orig_name']);
note: on *nix systems, you can check for supported locales using `locale -a`
note: instead of hard-coding the locale to use, you could also use the values of HTTP_ACCEPT_LANGUAGE and HTTP_ACCEPT_CHARSET, sent by the browser, to tailor the input locale
upload a file using cURL
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, _VIRUS_SCAN_URL);
curl_setopt($ch, CURLOPT_POST, true);
// same as <input type="file" name="file_box">
$post = array(
"file_box" => new CURLFile("/path/to/myfile.jpg"),
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
// if $response === false, something went wrong
?>
update 2026 - this used to be "file_box" => "@/path/to/myfile.jpg", the @ prefix was removed in PHP 7 - CURLFile (PHP 5.5+) replaces it
HTTP POST in pure PHP (without cURL)
$data = array('param1' => '1', 'param2' => '2');
$_data = http_build_query($data);
$options = array
(
'http' => array
(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\nContent-Length: ".strlen($_data)."\r\n",
'content' => $_data
)
);
$context = stream_context_create($options);
$data = @file_get_contents('http://www.example.com/post', 0, $context);
for more information see http_build_query(), stream_context_create(), file_get_contents()
prevent client disconnect from killing PHP
when the client aborts the request (typically the "Stop" button in the browser), the PHP script is killed. to prevent this, just call
ignore_user_abort(true);
note that your script can still be killed by the timeout. you might want to set
set_time_limit(0);
disconnect the client and continue processing
sometimes you need to do something time consuming in the PHP script being called, but you don't want to keep the client "hanging"
just use the following "idiom" (note - if you're having problems with the output being truncated, see below):
set_time_limit(0); // don't let the time limit us ...
ignore_user_abort(true); // ... and the user kill us
if (!ob_start('ob_gzhandler')) // try to compress the output ...
ob_start(); // ... or just make it raw
// ... 'echo' any response to client ...
header('Content-Length: '.ob_get_length());
header('Connection: close'); // browser will know to close the connection
// flush everything
ob_end_flush();
ob_flush();
flush();
// close current session if necessary
if (session_id())
session_write_close();
// if run as CGI, you need to also:
if (is_callable('fastcgi_finish_request'))
fastcgi_finish_request();
// ... from here on, you're on your own ... process to your liking ...
update: sometimes there seems to be a problem with ob_get_length() not returning the full length. typically i prefer to use double caching - one for the contents, and one for the output:
set_time_limit(0); // don't let the time limit us ...
ignore_user_abort(true); // ... and the user kill us
ob_start(); // outer 'ob' to cache the full gzip
if (!ob_start('ob_gzhandler')) // inner 'ob' to catch the data; try to gzip
ob_start();
// ... 'echo' any response to client ...
ob_end_flush(); // flush the inner 'ob' into the outer 'ob'
header('Content-Length: '.ob_get_length()); // now the ob_get_length() should return correct value
header('Connection: close'); // browser will know to close the connection
ob_end_flush(); // flush the outer 'ob'
ob_flush();
flush();
// close current session if necessary
if (session_id())
session_write_close();
// if run as CGI, you need to also:
if (is_callable('fastcgi_finish_request'))
fastcgi_finish_request();
// ... from here on, you're on your own ... process to your liking ...