So i am having an issue with a project i'm woking on, and have tried everything possible (i can think of).
now, There is a bug on the "editBanner.php" page. When you delete an image, that number is deleted. When a new file is uploaded, it is given the number of one more than the total of current banners. There is a major problem with this, eg if we have four banners: 1.jpg, 2.jpg, 3.jpg and 4.jpg. If we delete 2.jpg, we are left with 1.jpg, 3.jpg and 4.jpg. If we then uploaded a new image, the page would count the number of images (in this case three) and add 1, giving us a new file name of "4.jpg". We already have a file called 4.jpg, therefore, the old 4.jpg would be replaced.
I have been told i have to use the microtime function, to solve this, but as i have not be exposed to this function i really don't know how to use it. If you can have a look at my code and give me an example, that would be amazing.
<?php
require_once ("Includes/simplecms-config.php");
require_once ("Includes/connectDB.php");
include("Includes/header.php");
confirm_is_admin();
$dir = 'Images/banner';
$confirmation = "";
if(isset($_GET["del"])){
if (file_exists($dir . "/" . $_GET["del"])) {
unlink($dir . "/" . $_GET["del"]);
$confirmation = $_GET["del"] . " deleted. <br><br>";
} else {
$confirmation = $_GET["del"] . " doesn't exist. <br>
<br>";
}
}
$files = scandir($dir);
array_shift($files);
array_shift($files);
$newImage = COUNT($files)+1;
if(isset($_GET["add"])) {
$confirmation = "Image successfully uploaded.";
}
if(isset($_FILES["file"]["name"])){
$allowedExts = array("gif", "jpeg", "jpg", "png");
sort($allowedExts);
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 50 * 1024 * 1024) //50mb (1024
* 1kb = 1mb, 50 x 1024kb = 50mb)
&& in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . "
kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
<br>";
echo round(microtime(true)) . $_FILES["file"]
["tmp_name"];
move_uploaded_file($_FILES["file"]["tmp_name"], $dir .
"/" . $newImage . "." . $extension);
echo "Stored in: " . $dir . "/" . $_FILES["file"]
["name"];
header("Location: editBanner.php?add=1");
if(file_exists($_FILES["file"]["tmp_name"]["name"]
["type"]["size"]) && is_file($_FILES["file"]["tmp_name"]
["name"]["type"]["size"])) {
unlink($_FILES["file"]["tmp_name"]["name"]["type"]
["size"]);
}
}
} else {
echo "Invalid file";
}
}
?>
<div id="container">
<div id="admin">
<?php
if(!empty($confirmation)) {
echo "<div id='confirmation'>" . $confirmation . "
</div>";
}
for ($i = 0; $i < COUNT($files); $i++) {
echo '<div id="bannerImage">';
echo $files[$i] . ' - <a href="editBanner.php?del=' .
$files[$i] . '">Delete</a><br>';
echo '<img src="' . $dir . '/' . $files[$i] . '"
width="200px" height="100px" /><br>';
echo '</div>';
}
?>
<div id="bannerAdd">
<form action="editBanner.php" method="post"
enctype="multipart/form-data">
<h3>Upload a New Banner Image</h3>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</div>
</div>
</div>
</div> <!-- End of outer-wrapper which opens in header.php -->
<?php
include ("Includes/footer.php");
?>
For saving an image you can use the following option.
Current Time - You can use time()
in php which saves the image with current time in unix format. You can concatenate it with a name if you want. Example:
$newImageName = 'banner_'.time(); // output: banner_1487223503
For deleting an image, I see you are scanning the directory and fetching the images in $files
. So, you can apply the same.
In this way, names of all images will be unique. Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.