imgpaste/upload.php

36 lines
832 B
PHP

<?php
if(is_file('config.php')) {
include('config.php');
} else {
include('config.dist.php');
}
if(!is_dir($datadir)) {
mkdir($datadir);
}
if(isset($_FILES['upload'])) {
$extension = pathinfo($_FILES['upload']['name'], PATHINFO_EXTENSION);
if($extension == 'jpg' || $extension == 'jpeg' || $extension == 'png') {
$filename = generate_filename($length) . '.' . $extension;
move_uploaded_file($_FILES['upload']['tmp_name'], $datadir . '/' . $filename);
echo json_encode(['datadir' => $datadir, 'filename' => $filename]);
}
}
function generate_filename($length) {
$filename = "";
for($i = 0; $i < $length; $i++) {
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$random_char = $chars[rand(0, strlen($chars) - 1)];
$filename = $filename . $random_char;
}
return $filename;
}
?>