Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

php copy not working

Good afternoon,
Ive written what I thought to be a simple program to copy a file to a network server and then copy it back. Issue is it doesnt seem to copy the file, but it also doesnt show any errors!!!

The code I have is:-
<?php

if (isset($_GET['testDest'])==false)
{
	echo "No testDest set in GET";
	exit();	
}
set_time_limit(0); 
error_reporting(-1);


echo "Generating File <br> \n";
$randFile = "myFile_" . rand(0,9999) . ".dat";

$fh = fopen($randFile, 'w') or die("can't open file");
$stringData = chr(rand(1,255));
fwrite($fh, $stringData);
fclose($fh);

while (filesize($randFile) < 100000)
	{
	
	$fh = fopen($randFile, 'a') or die("can't open file");
	$stringData = "";
	for ($x=1;$x<=1000;$x++)
	{
		$stringData = $stringData . chr(rand(1,255)) . chr(rand(1,255)) . chr(rand(1,255)) . chr(rand(1,255)) . chr(rand(1,255)) . chr(rand(1,255)) . chr(rand(1,255)) . chr(rand(1,255));	
	}
	fwrite($fh, $stringData);
	fclose($fh);
	clearstatcache();
	}
	
	
echo "File generated, giving size of " . filesize($randFile) . "<br> \n";

function copyFile($randFile, $strSource)
	{
	echo "Starting to copy file to at " . date("Y-m-d H:i:s.u") . "<br> \n";
	$startTime = microtime(true);
	copy($randFile,$strSource);
	echo "Completed copying file to at " . date("Y-m-d H:i:s.u") . "<br> \n";
	echo "Time Taken was " . (microtime(true) - $startTime) . "<br> \n";
	



	$tmpFile = "myFile_" . rand(0,9999) . ".tmp";
	
	echo "Starting to copy file from at " . date("Y-m-d H:i:s.u") . "<br> \n";
	$startTime = microtime(true);
	copy($strSource, $tmpFile);
	echo "Completed copying file from at " . date("Y-m-d H:i:s.u") . "<br> \n";
	echo "Time Taken was " . (microtime(true) - $startTime) . "ms <br> \n";
	//unlink($tmpFile);
	//unlink($strSource);
	echo "<br> \n <HR> <br> \n";
	}

//copyFile($randFile, "\\\\172.16.8.7\\Public\\" . $randFile);


echo "Testing " . $_GET['testDest'] . "<BR>\n";
copyFile($randFile, $_GET['testDest'] . $randFile);

set_time_limit(30);

//unlink($randFile);	
?>

Open in new window


Which generates the output:-
Generating File 
File generated, giving size of 104001
Testing \\172.16.8.7\Public\
Starting to copy file to at 2011-05-06 15:30:32.u
Completed copying file to at 2011-05-06 15:30:32.u
Time Taken was 0.001579
Starting to copy file from at 2011-05-06 15:30:32.u
Completed copying file from at 2011-05-06 15:30:32.u
Time Taken was 0.001537ms 

Open in new window


So as you can see there is no error, but it doesnt copy the file either to or from?

Any assistance?
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Let's start by adding these to the script:

ini_set('display_errors', TRUE);
error_reporting(E_ALL);
Avatar of tonelm54
tonelm54

ASKER

Whats intresting is it is creating a file on my server where this is running as "\\172.16.8.7\Public\myFile_8959.dat" which is the file its supposed to copy to
Next let's test the return from copy().  Check the man page for how to do that:
http://php.net/manual/en/function.copy.php
After that about all I can recommend is that you use some data visualization.  For that to be effective, reconsider how you write some of the PHP instructions.  Example from line 64:

copyFile($randFile, $_GET['testDest'] . $randFile);

Instead you might do something like this:

$new = $_GET['testDest'] . $randFile;
echo "<pre>";
echo PHP_EOL;
echo $randFile;
echo PHP_EOL;
echo $new;
copyFile($randFile, $new);
Ive added to my code:-
            ini_set('display_errors', TRUE);
            error_reporting(E_ALL);

Same output, apart from slightly different times:-
Generating File 
File generated, giving size of 104001
Testing \\172.16.8.7\Public\
Starting to copy file to at 2011-05-06 15:43:30.u
Completed copying file to at 2011-05-06 15:43:30.u
Time Taken was 0.001534
Starting to copy file from at 2011-05-06 15:43:30.u
Completed copying file from at 2011-05-06 15:43:30.u
Time Taken was 0.001501ms 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial