Archive for the 'php' Category

How to move a file using PHP

This article will tell you how to move a file on your webserver using the filesystem functions of PHP. It’s something that is very easy when you know how, but not immediately obvious due to the lack of an explicit move() function.

The trick is to use the rename() function. If you just wanted to rename a file, you would use the code:

<?php
rename("oldname.txt", "newname.txt");
?>

To move a file, it is something similar:

<?php
// Move file into folder called 'store'
rename("file.txt", "./store/file.txt");
?>

In the above example, we assume that both the file and the folder are in the same directory as the php script. The period at the front of the new file path means that it starts looking in this directory - if I omitted that, then it would look for a folder called ’store’ in the root directory: /.

Rather than waffle on about the specifics of what can go wrong, I’ll point you at the PHP manual page: rename. If you have any difficulties, feel free to leave a comment.


About

You are currently browsing the vdhri.net weblog archives for the php category.

Longer entries are truncated. Click the headline of an entry to read it in its entirety.

Categories