Web Development

This part is of the PHP Post Series dedicated to spread knowledge about PHP

PHP has a bunch of functions that works with files. copy is a very useful one. copy makes copies of files in the filesystem.

Example Code

copy("ad_images/image1.png", "ad_images/thumb/image1.png");
// Test if a copy has been made.
if (file_exists("ad_images/thumb/images1.png") {
    echo "File has been copied over";
} else {
    echo "File has not been copied over";
}
// A better alternative is to use the copy method's return value
// Copy and Test if a copy has been made.
if (copy("ad_images/image1.png", "ad_images/thumb/image1.png")) {
    echo "File has been copied over";
} else {
    echo "File has not been copied over";
}

The copy function requires two parameters, the original file location with name and the destination filename with a new or old filename.