SuperMarioMC98
SuperMarioMC98

Reputation: 21

opening a variable in php from a text file

i want to copy some files the hard way (dont ask me why its just too complicated) and use a variable created to a file and opened from another script then it initiates the coping

i already made a functional page that saves the value i want now i have this code that i want to extract the variable that is in a file called tmpCraft.txt and make it copy the specified file to the destination from the tmp file so this is my code its called accountCrafter.php

<?php

$dst = fopen("tmpCraft.txt", "r") or die("Unable to open file!");   
echo fread($dst,filesize("tmpCraft.txt"));  
fclose($dst);

$file = 'structure/index.html';

if (!copy($file, $dst . "/index.html")) {
    echo "failed to copy $file...\n"; 
}else{
    echo "copied $file into $newfile\n"; 
}

?>

i ran it and this was the result:

noni
Warning: copy(Resource id #3/index.html) [function.copy]: failed to open stream: No such file or directory in H:\xampp\htdocs\dev4\test2\accountCrafter.php on line 9
failed to copy structure/index.html...

The value was noni

for some reason it extracts the value as

Upvotes: 0

Views: 66

Answers (1)

Barmar
Barmar

Reputation: 780889

This is probably what you want:

<?php
$dst = trim(file_get_contents("tmpCraft.txt"));
$file = 'structure/index.html';
$newfile = $dst . "/index.html";

if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n"; 
}else{
    echo "copied $file into $newfile\n"; 
}

?>

You need to set $dst to the contents of the file, not the file handle. trim() will remove any extraneous whitespace around the name in the file.

Upvotes: 1

Related Questions