George Reith
George Reith

Reputation: 13476

PHP: Copy() producing empty files

I am using the following function to copy() a file, The files are typically 1-50KB in size and a file of the correct name is created but it is completely empty.

public function backup() {
    $backup = $this->_root.'/cfg_'.date('d_m_y').'.backup';
    if (!copy($this->_file, $backup))
        return false;
    return $backup;
}

I know for certain that $this->_file correctly points to the file I am attempting to copy. $this->_file is created by another method of the same class and I perform a chmod() with an octal value of 0755 on it as shown in the following function:

private function createFile($filename) {
    if (!($this->_pointer = @fopen($filename, 'cb')))
        return false;
    fclose($this->_pointer);
    chmod($filename, 0755);
    return true;
}

Does anyone know what could be causing this behaviour?

Upvotes: 2

Views: 1670

Answers (3)

George Reith
George Reith

Reputation: 13476

Problem solved: The issue was that I performed an fopen() call on the $this->_file like so:

if (!($this->_pointer = @fopen($this->_file, "wb")))
    throw new Exception("Unable to retrieve database configuration");

This caused $this->_file to be truncated and I had not yet written to it so at the time my copy() operation was performed $this->_file was empty itself and thus showed no error.

Apologies to the folks who gave answers as the function that was doing the fopen() call was not included in my question.

Upvotes: 3

James Ravenscroft
James Ravenscroft

Reputation: 375

It could be a permissions issue. Are you sure the script has read permission on the file? Provided that $this->_file is a valid path, I can't see why this wouldn't work.

Upvotes: 0

ash108
ash108

Reputation: 1761

One obvious thing is to make sure you have got free space at the location you are copying to.

Upvotes: 1

Related Questions