I have a code that successfully uploads single or multiple photos. The only problem I face is that I add a random number from the beginning of uploaded image name, but the random number that is added to img name that goes to database is different from the number that is added to uploaded img name. Note: there is only one random number declared in my code. Thanks
<?php
error_reporting(0);
session_start();
$b = $_POST['c'];
$title = $_POST['title'];
$pat = "/[a-zA-Z0-9\-\.]+.(jpg|png|jpeg|JPG|PNG|JPEG)/";
$random = rand(1, 100000000000);
$k = preg_replace_callback_array(
[
$pat => function ($match) {
return 'images/'.$GLOBALS['random'].$match[0].'';
}
],
$b
);
$conn = mysqli_connect("localhost", "root", "", "lessons");
$sql = "INSERT INTO `posts`(`title`, `text`) VALUES ('$title', '$k')";
$res = mysqli_query($conn, $sql);
$sql1 = "DELETE FROM `posts` WHERE `title` = ''";
$res1 = mysqli_query($conn, $sql1);
$sql2 = "SELECT `id` FROM `posts` ORDER BY `id` DESC LIMIT 1";
$res2 = mysqli_query($conn, $sql2);
while ($row = mysqli_fetch_assoc($res2)) {
$_SESSION["row"] = $row['id'];
}
$id = $_SESSION["row"];
$sql3 = "ALTER TABLE `blog` ADD `$id` TEXT NOT NULL";
$res3 = mysqli_query($conn, $sql3);
$count = count($_FILES["image"]["name"]);
for ($i = 0; $i < $count; $i++) {
$filename = $_FILES["image"];
$location = "images/".$GLOBALS['random'].str_replace(" ", "", $filename["name"][$i]);
$location2 = "C:xampp/htdocs/app/images/".$GLOBALS['random'].str_replace(" ", "", $filename["name"][$i]);
copy($filename["tmp_name"][$i],$location);
copy($filename["tmp_name"][$i],$location2);
}
?>
Aucun commentaire:
Enregistrer un commentaire