Pixelpost

Authentic Photoblog Flavour


Go Back   Pixelpost Forum > DEVELOPMENT > Hacks and Modifications

Post Reply
 
Thread Tools
  #1  
Old 03-16-2005, 03:17 AM
Rob Offline
pixelpost guru
 
Join Date: Mar 2005
Location: Burlington, Canada
Posts: 342
Send a message via ICQ to Rob Send a message via MSN to Rob
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
and replace with this modified version

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
There are two lines changed, one near the start and one about half way down.

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
__________________
shifted*exposure powered by PixelPost 1.4
Reply With Quote
Post Reply


Thread Tools




All times are GMT. The time now is 03:11 AM.

Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd. | Style Design: d3 designs