|
#1
|
|||
|
|||
|
Preventing overwriting of images
If you mistakenly try to upload an image with the same name as you posted before, it will overwrite the previous post's image and thumbnail, so you will end up with two posts with the same newer image. Worse, if you delete either post, it will delete the one image for both posts and the remaining post will not have an image.
![]() The normal way to fix this is to check if the file exists before copying it, but then you need to rename the file and try posting again. Instead, what I do is to add the posting date to the beginning of the filename. That way, the image names will never conflict unless you upload two photos with the same name to the same day. If you feel that you might do that, add the time to the filename too. The fix goes as follows. Edit admin/index.php and find the following lines; Code:
// prepare the file
if($_FILES['userfile'] != "") {
$userfile = strtolower($_FILES['userfile']['name']));
$uploadfile = $upload_dir. $userfile;
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
chmod($uploadfile, 0644);
$result = check_upload($_FILES['userfile']['error']);
$filnamn = strtolower($_FILES['userfile']['name']);
$filtyp = $_FILES['userfile']['type'];
$filstorlek = $_FILES['userfile']['size'];
$status = "ok";
} else {
// something went wrong, try to describe what
echo "Error";
$result = check_upload($_FILES['userfile']['error']);
echo "<br>$result<hr>";
$status = "no";
} // end move
} // end prepare of file
Code:
// prepare the file
if($_FILES['userfile'] != "") {
$userfile = sprintf( "%s%s%s-%s", $_POST['post_year'], $_POST['post_month'], $_POST['post_day'], strtolower($_FILES['userfile']['name']));
$uploadfile = $upload_dir. $userfile;
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
chmod($uploadfile, 0644);
$result = check_upload($_FILES['userfile']['error']);
$filnamn = $userfile;
$filtyp = $_FILES['userfile']['type'];
$filstorlek = $_FILES['userfile']['size'];
$status = "ok";
} else {
// something went wrong, try to describe what
echo "Error";
$result = check_upload($_FILES['userfile']['error']);
echo "<br>$result<hr>";
$status = "no";
} // end move
} // end prepare of file
This also has the advantage of giving all of your images on the server a date stamp, so you can easily see what day they were posted to and sort them in posting order. Happy uploading
|
| Post Reply |
| Thread Tools | |
|
|