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:-
Which generates the output:-
So as you can see there is no error, but it doesnt copy the file either to or from?
Any assistance?
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);
?>
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
So as you can see there is no error, but it doesnt copy the file either to or from?
Any assistance?
ASKER
Whats intresting is it is creating a file on my server where this is running as "\\172.16.8.7\Public\myFil e_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
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);
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);
ASKER
Ive added to my code:-
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
Same output, apart from slightly different times:-
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ini_set('display_errors', TRUE);
error_reporting(E_ALL);