Daan Verberne
Daan Verberne

Reputation: 45

Copy PHP file from other URL without executing the code

When I duplicate or copy a PHP file from one url to another, the content within the PHP executes. The php files contain mysqli query's.

I tried 2 methods, the file_get_contents and php's default copy function. Both transfer the file from one to another but both times the copied PHP file shows:

<br />
<b>Fatal error</b>:  Call to a member function query() on a non-object in  <b>/home/uu/domains/domain.net/public_html/library/folder/index.php</b> on line <b>2</b><br />

Method 1:

if ( copy($from.$element_file['file'], $to.$element_file['file']) ) {
    chmod($to.$element_file['file'], 0777);
}

Method 2:

$content = file_get_contents($from.$element_file['file']); 
$new_file = $to.$element_file['file'];
$handle = fopen($new_file, 'w');
file_put_contents($new_file, $content);

Reason: I'm not trying to hack or steal anything. I've build a external library with files I can use while developing new websites, like little templates (dir with an index.php, less file and a query file). All the php files are on the same server.

Upvotes: 0

Views: 1117

Answers (1)

Ariyan
Ariyan

Reputation: 15138

When You copy a URL you're copying server's output for that url!
So the script will execute (as it should execute to generate output).
to copy a file content (script itself) you need to copy it from a file server or other method that give you file output.
If the files are on the same server you should use file path instead of URL.
For an example if you have access you can use CURL to access FTP on the target server and copy the file.

Upvotes: 2

Related Questions