Jump to content

[SOLVED] File does not copy... Why?


pugboy

Recommended Posts

I have absolutely no clue why this file is not copying into an existing directory...

 

My file tree is below:

 

htdocs

-beta

--account

---admin

--usercp

---add.php

---index.php

--template

---home

----index.html

----login.php

----files.txt

 

If you can understand it, it shows that I need index.php to copy everything from the home template folder over to account>admin (or another user)

 

So far, it creates the directory. However, it does not copy the files. It outputs the message below:

 

Checking files...
Files Intact... Copying files...
Creating directory: ../account/admin/home
Directory Created.
Template Exists... Copying Files...Failed to copy ../template/home/index.html to ../account/admin/home/index.html

 

 

I have done everything to look for errors, and pinpoint a problem. I have found nothing running my code through php Design Studio, and numerous online syntax checkers...

 

My code:

 

if($do=="add"){
echo "Checking files...<br/>";
$baseDir = "../account/" . $user;
$dir = "../account/" . $user . "/" . $type;
$dirTemplate = "../template/" . $type;
$fileList = $dirTemplate . "/files.txt";
$passwords = md5($user);
$passwords = $passwords . ".txt";
$indexfile = $dirTemplate . "/index.html";
echo "Files Intact... Copying files... <br/>";
echo "Creating directory: $dir<br/>";
if(!mkdir($dir)){
	echo "Fatal Error. Directory did not create";
	exit;
} else {
	echo "Directory Created.<br/>";
}
$fileName = $fileList;
$fh = fopen($fileName, 'r');
$fileListB = fread($fh, filesize($fileName));
fclose($fh);
$fileListC = explode("\n",$fileListB);
echo "Template Exists... Copying Files...";
for ( $id = 0; $id <= $maxid; $id += 1) {
	$fileT = $dirTemplate . "/" . $fileListC[$id];
	$fileD = $dir . "/" . $fileListC[$id];
	if(!copy($fileT, $fileD)){
		echo "Failed to copy $fileT to $fileD";
		exit;
	} else {
		echo "Copy file: $fileT. Success!";
	}
}
}

 

$do = $_GET["do"] fyi...

 

 

Does ANYONE see ANYTHING wrong, or is the code hopeless?

Link to comment
https://forums.phpfreaks.com/topic/109260-solved-file-does-not-copy-why/
Share on other sites

Are you sure the home directory in account/admin has the correct permissions for writing files?

 

EDIT: I have modified your code slightly:

if($do=="add")
{
echo "Checking files...<br/>";

$baseDir     = '../account/' . $user;
$dir         = $baseDir . $user . '/' . $type;
$dirTemplate = "../template/" . $type;

$fileList  = $dirTemplate . "/files.txt";

$passwords = md5($user);
$passwords = $passwords . ".txt";

$indexfile = $dirTemplate . "/index.html";

echo "Files Intact... Copying files... <br/>";

    echo "Creating directory: $dir<br/>";
    if(!mkdir($dir))
    {
        echo "Fatal Error. Directory did not create";
        exit;
    }
    else
    {
        echo "Directory Created.<br/>";
    }

echo "Template Exists... Copying Files...<br />";

    $fileList = file($fileList);

    foreach($filesList as $file)
    {
        $sourceFile = $dirTemplate . '/' . trim($file);

        $targetDest = $dir . '/' . trim($file);

        if(is_writable($dir))
        {
            if(!copy($sourceFile, $targetDest))
            {
                echo "<br />Failed to copy $fileT to $fileD";
                exit;
            }
            else
            {
                echo "Copy file: $fileT. Success!";
            }
        }
        else
        {
            exit($dir . ' is not writable! Unable to copy files!');
        }
    }
}

Ok then.... I rewrote the copy funtion, BUT IT STILL FAILS!  :o>:(

 

Here is my new code:

 

if($do=="add"){
$type = $_POST["type"];
echo "Creating account for $user<br/>";
$tDir = "../template/" . $type;
$dDir = "../account/" . $user . "/" . $type;
$pFileName = $dDir . "/" . md5($user) . ".txt";
if(!mkdir($dDir)){
	echo "Directory did not create. Aborting";
	exit;
}
$fList = $tDir . "/files.txt";
$fileList = file($fList);
foreach( $fileList as $id => $name){
	$tFileN = $tDir . "/" . $name;
	$dFileN = $dDir . "/" . $name;
	if(!copy($tFileN,$dFileN)){
		echo "Transfer of $tFileN to $dFileN failed. Aborting";
		exit;
	}
}
}

 

I KNOW the copy function works outside of a for loop.... I just created a test function that succeeded! Is this a PHP limitation?

 

EDIT:

Fails in a while loop as well.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.