Skip to content

Instantly share code, notes, and snippets.

@NuckChorris
Created August 3, 2013 02:24
Show Gist options
  • Select an option

  • Save NuckChorris/6144851 to your computer and use it in GitHub Desktop.

Select an option

Save NuckChorris/6144851 to your computer and use it in GitHub Desktop.
<?php
$static_root = "/tmp/";
function generate_name ($name) {
do {
$newname = '';
for ($i = 5; $i > 0; $i--) {
$newname .= base_convert(mt_rand(0, 36), 10, 36);
}
$newname .= strtolower(strrchr($name, '.'));
} while (file_exists($newname));
return $newname;
}
$out = array();
$files = $_FILES["files"];
$num_files = count($files["name"]);
for ($i = 0; $i < $num_files; $i++) {
$newname = generate_name($files["name"][$i]);
if ($files["error"][$i] == 0 && move_uploaded_file($files["tmp_name"][$i], $static_root . $newname)) {
$out[] = array(
"name" => $files["name"][$i],
"url" => $newname
);
} else {
http_response_code(500);
echo json_encode(array(
"success" => false
));
}
}
echo json_encode($out, JSON_PRETTY_PRINT);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment