What potential issues can arise when using the copy() function in PHP?

One potential issue when using the copy() function in PHP is that it may fail if the destination directory does not exist. To solve this issue, you can use the mkdir() function to create the directory before copying the file.

$source = 'path/to/source/file.txt';
$destination = 'path/to/destination/directory/file.txt';

// Create the destination directory if it does not exist
if (!file_exists(dirname($destination))) {
    mkdir(dirname($destination), 0777, true);
}

// Copy the file
if (copy($source, $destination)) {
    echo 'File copied successfully.';
} else {
    echo 'Failed to copy file.';
}