3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php /** * Simple file uplaod script. * NO WARRANTY. THIS SCRIPT IS ONLY AN EXAMPLE. * INPUT VALIDATION IS MINIMAL. DO NOT USE IN PRODUCTION * * @copyright 2014 Alex Soban * @license http://opensource.org/licenses/MIT * @author Alex Soban <me@soban.co> */ define('TOKEN', '<secure_token>'); define('UPLOADS', __DIR__ . '/uploads/'); define('URL', 'http://www.example.com/uploads/'); function output(array $data = array()) { header('Content-Type: application/json'); echo json_encode($data); exit; } // `name` check is optional if (empty($_POST) || !isset($_POST['image']) || !isset($_POST['token']) || !isset($_POST['name'])) { output(array('error' => 'Invalid input.')); } // `token` verification if ($_POST['token'] !== TOKEN) { output(array('error' => 'Invalid token.')); } // get the filename and contents $filename = trim(preg_replace('/[^\d\w.]+/i', '', $_POST['name'])); $image = base64_decode($_POST['image']); // "upload" the file to our uploads directory if (!file_put_contents(UPLOADS . $filename, $image)) { output(array('error' => 'Failed saving file.')); } // return file public URL output(array('href' => URL . $filename));
Output for 5.3.0 - 7.1.0
{"error":"Invalid input."}
Output for 5.2.3 - 5.2.17
Notice: Use of undefined constant __DIR__ - assumed '__DIR__' in /in/jHkSU on line 13 Warning: Cannot modify header information - headers already sent by (output started at /in/jHkSU:13) in /in/jHkSU on line 17 {"error":"Invalid input."}
Output for 5.2.0 - 5.2.2
Notice: Use of undefined constant __DIR__ - assumed '__DIR__' in /in/jHkSU on line 13 {"error":"Invalid input."}
Output for 5.1.0 - 5.1.6
Notice: Use of undefined constant __DIR__ - assumed '__DIR__' in /in/jHkSU on line 13 Fatal error: Call to undefined function json_encode() in /in/jHkSU on line 18
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/jHkSU on line 16
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_ARRAY, expecting ')' in /in/jHkSU on line 16
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_ARRAY, expecting ')' in /in/jHkSU on line 16
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `')'' in /in/jHkSU on line 16
Process exited with code 255.