Skip to main content

Ace Editor Mode Getter

Nov 18, 2022AustinLeath
Loading...

More PHP Posts

List Files in Directory

Nov 18, 2022AustinLeath

0 likes • 1 view

<?php
$listOfFiles = glob("*.{jpg,png,gif,tiff,jpeg}", GLOB_BRACE);
echo 'images = []; ';
for($x = 0; $x <= count($listOfFiles)-1; $x++) {
echo "images[" . $x . "] =" . '"' . $listOfFiles[$x] . '"' . "; ";
}
?>

Make Random Number

Nov 19, 2022CodeCatch

0 likes • 0 views

<?php
function getRandomId($min = NULL, $max = NULL) {
if (is_numeric($min) && is_numeric($max)) {
return mt_rand($min, $max);
}
else {
return mt_rand();
}
}
?>

Search Function Display

Nov 18, 2022AustinLeath

0 likes • 0 views

<?php
session_start();
if(isset($_POST['search-submit'])) {
$currentFilter;
if(isset($_POST['search-filter']) && $_POST['search-filter'] == "search-all") {
$currentFilter = "All";
} else if(isset($_POST['search-filter']) && $_POST['search-filter'] == "search-by-title") {
$currentFilter = "Title";
} else if(isset($_POST['search-filter']) && $_POST['search-filter'] == "search-by-language") {
$currentFilter = "Language";
} else {
$currentFilter = "Username";
}
}
if(isset($_POST['search-submit']) && $_POST['search'] !== "") {
require_once 'includes/config.php';
$searchQuery = '%' . htmlspecialchars($_POST['search']) . '%';
$statement;
if(htmlspecialchars($_POST['search-filter']) == "search-all") {
$statement = $conn->prepare("SELECT * FROM user_posts WHERE privacy = 0 AND title LIKE ? OR username LIKE ? OR language LIKE ?");
$statement->bind_param("sss", $searchQuery, $searchQuery, $searchQuery);
} else if(htmlspecialchars($_POST['search-filter']) == "search-by-title") {
$statement = $conn->prepare("SELECT * FROM user_posts WHERE privacy = 0 AND title LIKE ?");
$statement->bind_param("s", $searchQuery);
} else if(htmlspecialchars($_POST['search-filter']) == "search-by-language") {
$statement = $conn->prepare("SELECT * FROM user_posts WHERE privacy = 0 AND language LIKE ?");
$statement->bind_param("s", $searchQuery);
} else {
$statement = $conn->prepare("SELECT * FROM user_posts WHERE privacy = 0 AND username LIKE ?");
$statement->bind_param("s", $searchQuery);
}
$statement->execute();
$result = $statement->get_result();
}
?>
<?php
if(isset($_POST['search-submit']) && $_POST['search'] !== "") {
if($result->num_rows > 0) {
$i = 1;
echo '<ul class="list-group mt-5">';
while($row = $result->fetch_assoc()) {
$linkName = "post.php?postID=" . $row['postID'];
$privacy = ($row['privacy'] == 0) ? "Public" : "Private";
if($privacy == "Public" || ($privacy == "Private" && (isset($_SESSION['username']) && ($row['username'] == $_SESSION["username"])))) {
echo '
<a class="post-box-link" href="' . $linkName . '">
<li class="list-group-item mb-2 border rounded post-box">
<h4><span style="color: #007bff;">' . $row['title'] . '</span> | ' . $row['language'] . '</h4>
<h5>' . $row['username'] . ' - ' . '<small>' . $row['uploadDate'] . '</small></h5>
</li>
</a>
';
$i++;
}
}
echo '</ul>';
}
}
?>

ID Generator

Nov 18, 2022AustinLeath

0 likes • 0 views

<?php
function uniqidReal($length = 8) {
// uniqid gives 8 chars, can be changed
if (function_exists("random_bytes")) {
$bytes = random_bytes(ceil($length / 2));
} elseif (function_exists("openssl_random_pseudo_bytes")) {
$bytes = openssl_random_pseudo_bytes(ceil($length / 2));
} else {
throw new Exception("no cryptographically secure random function available");
}
return substr(bin2hex($bytes), 0, $length);
}
/*
//for testing
for($i = 0; $i < 10; $i++) {
echo "<br>";
echo uniqidReal();
}
*/
?>

Crop an image

Nov 19, 2022CodeCatch

0 likes • 0 views

<?php
$im = imagecreatefrompng('example.png');
$size = min(imagesx($im), imagesy($im));
$im2 = imagecrop($im, ['x' => 0, 'y' => 0, 'width' => $size, 'height' => $size]);
if ($im2 !== FALSE) {
imagepng($im2, 'example-cropped.png');
imagedestroy($im2);
}
imagedestroy($im);
?>

RGBA Generator

Nov 18, 2022AustinLeath

0 likes • 0 views

<?php
//generate 239 rgba colors for codecatch's 239 supported programming languages!
//this was used to generate the colors that are used in the pie chart on https://codecatch.net/graphs.php
function rgbagenerator() {
$count = 239;
for ($i=0; $i < $count; $i++) {
echo "'rgba(". mt_rand(0,255) . ",". mt_rand(0,255) . ",". mt_rand(0,255) . ",1)',";
echo "<br>";
}
}
rgbagenerator();
?>