imgpaste/upload.php

25 lines
696 B
PHP

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