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
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!');
        }
    }
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.