May
20
2005
0

Moving files with PHP

There are two main ways to move files in PHP – either copy the file to the new location, then delete the old one, or rename the file to the new location. There are a few different problems with both ways of doing it, and in this short guide I’ll try to cover as many different advantages and disadvantages of both ways.

Here’s a sample function on the copy/unlink method:

function move_file( $oldfilelocation, $newfilelocation)
    {
    if( copy( $oldfilelocation, $newfilelocation ) )
        {
        unlink( $oldfilelocation );
        }
    else
        {
        echo "Copy failed";
        }
    }

The function’s rather simple – it’s broken up into an if-else pair – if the file copies from the old location to the new location, then unlink (delete) the old file. If the copy fails, echo a generic error message. You could put all sorts of things into this "error" section, things like logging the error to a database, or echo’ing something to the screen.

Pros:
It’s simple, and easy to debug if something goes wrong – and since it’s only copying the file, then checking that it’s there, then deleting the original, the file isn’t going to go missing like if you had just moved it to the wrong spot.
Cons:
Adds another function or another if-else statement to your codebase, adding more complexity in your script, and something else to debug.

The rename() function is quite powerful:

rename( $oldfilename, $newfilename);

That’s all you need to do when you’re renaming a file (or moving it to a new location.) As an example, if I was moving "tempfile.txt" from /var/www/html to /var/www/html/backup all I would have to do is call the function as follows:

rename( "/var/www/html/tempfile.txt", "/var/www/html/backup/tempfile.txt" );

The problem with this is that there is no checking whether the original file is there, there is no check done on if the file was able to be moved or any error reporting – it’ll report whether it worked or not, but there’s no sure-fire way of knowing that it did, unless you were using the rename function inside some if-else code.

(I’ll finish this later, I’m working on it slowly)

Written by James Hodgkinson in: Programming |
May
20
2005
0

Stop-Smoking Update

Well, I’m still surviving so far, the patches give me the incentive and the power not to put more tar in my lungs. I seem to be trying to cough all the tar that’s already in there up at the moment – yay for a hacking cough that hurts like I’m on fire when it happens. I’m nearly through both the week-long packets that I bought, and I went a couple of days without a patch, and I survived fine. I don’t think I could go a day at work without them, it’s a high-stress environment, and I can’t deal with it at the moment without a patch. There was a day that I forgot, and I nearly tore a workmate’s head off after a frustrating call from a customer.

I’m getting better, and when I’m done with another packet of them, as per the instructions, I’ll step down to a lower "dosage" and see how I go. At the moment, they are supposedly delivering ~24mg of nicotine into my system in 24 hours – roughtly 24 cigarettes going by the markings on the Malboro Red packets that I smoked (see! smoked not smoke) I’ve decided not to wear them when I’m sleeping anymore after realising that I was having some really disturbing dreams. Dreaming was weird enough, but it was messing up my sleep patterns horribly, and making me rather irritable at work and in general.

I don’t refer to it as quitting – more as making a concious decision to stop smoking. Reminding myself and others of this fact reinforces that point in my brain – standard practise for convincing someone of something is to use code words and phrases to emphasise the outcome. Rather than saying "I can’t forget to go to the shops" one should use "I must remember to go to the shops." This puts the emphasis on remember, rather than forget. Same with this situation – remembering the phrase "I have made a decision to stop smoking" puts the emphasis for me on stopping smoking.

Written by James Hodgkinson in: Uncategorized |
May
16
2005
0

Amp connector issues

Click to ZoomSeems that the amp that I bought (the Blaupunkt PA2100) is rather unique. The connectors that it has on it aren’t the standard connectors, so it’s probably one from a factory-installed stereo in a prestige car of some sort. Click on the images below to see the amp/connectors on it – if you can work out what they are, please tell me, and if you know where to get them, give me the info!

Click to Zoom Oh, and in other news – the amp is a 2×100W RMS digital amp, class "T" as they describe it – it’s got the sound quality of a class AB amp, and the efficiency of a class D amp. I’ve seen claims of over 350WRMS out of this amp, which should be well and truly enough for the application that i’m putting it into.

Written by James Hodgkinson in: Gemini, The Pimpmobile |
May
13
2005
0

Patches and Dreams

Wow, I really should have listened to the person at the chemist’s counter when I bought the nicotine patches. She made a passing comment about the fact that if you wear them during sleep, it can have some effect on your dreams. They most definitely did – two dreams so far (normally I don’t dream at all unless I’m quite sick) The first one was explainable from dream analysis and recent events, but the one that I had last night was downright weird. I can’t remember any of it (should get up and write them down as soon as I wake) but all I remember is an intense feeling of foreboding when I woke up. I think I’ll leave them off for the next couple of nights, see how they go.

Written by James Hodgkinson in: Uncategorized |
May
10
2005
0

Tip: NDS for J2ME on Gentoo Linux

Tips for installing the Nokia Developer’s Suite for J2ME™ on Gentoo Linux. I’m trying to do my development in Linux, since I use it as my main desktop machine, and I hate using Windows development tools for some reason. I’m sure there will be other snags – I still have to get it working with Eclipse, and then work out how to program in Java. Yay for my ability to pick up new programming languages as I use them – thanks to the high school teacher that taught the theory of programming rather than just how to do it.

Written by James Hodgkinson in: Programming |

Theme: TheBuckmaker.com Wordpress Themes | TechMilieu.com