By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,821 Members | 1,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,821 IT Pros & Developers. It's quick & easy.

copy($source, $destination) not working for me

P: n/a
Hello PHP group,

I am trying to use the copy function to copy files from one directory
to another.
This does not seem to work for me. What am I missing?

Below is the code I am using.

<?php
$filecount = 0;
$filelist[0] = '';
$idx = 0;

//define('loc1', '/usr/local/Apache2/htdocs/portfolios', true);
$path = 'testimage';
$thumb_path = 'thumbs';

$dir_handle = @opendir($path) or die("Unable to open directory
$path");

//$filename2 = $filename . '.old';
//copy($filename, $filename2);

while ($file = readdir($dir_handle))
{
$filetyp = strtolower(substr($file, -3));
if ($filetyp == 'jpg' )
{
//loc1. '/'.$path. '/' .$thumb_path . '/' .
$filelist[$idx] = $path . '/' . $file;
$thumbfile = $thumb_path . '/'. 'thumb-'. $file;

if (!@copy($file, $thumbfile)) {
echo "unable to copy file" .'<br>';
}

$idx++;
$filecount++;

}
}

closedir($dir_handle);

echo '<h2>'."counted: " . $filecount . " files.".'</h2>';

?>
I have tried to copy to the same directory as well - no luck. the
webserver has access to the folders in question. What do I need to do
to get this to work?

Thanks Again,

eholz1

Jan 23 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 22 Jan 2007 20:30:05 -0800, "eholz1" <ew****@gmail.comwrote:
>Hello PHP group,

I am trying to use the copy function to copy files from one directory
to another.
This does not seem to work for me. What am I missing?

Below is the code I am using.

<?php
$filecount = 0;
$filelist[0] = '';
$idx = 0;

//define('loc1', '/usr/local/Apache2/htdocs/portfolios', true);
$path = 'testimage';
$thumb_path = 'thumbs';

$dir_handle = @opendir($path) or die("Unable to open directory
$path");

//$filename2 = $filename . '.old';
//copy($filename, $filename2);

while ($file = readdir($dir_handle))
{
$filetyp = strtolower(substr($file, -3));
if ($filetyp == 'jpg' )
{
//loc1. '/'.$path. '/' .$thumb_path . '/' .
$filelist[$idx] = $path . '/' . $file;
$thumbfile = $thumb_path . '/'. 'thumb-'. $file;

if (!@copy($file, $thumbfile)) {
echo "unable to copy file" .'<br>';
}

$idx++;
$filecount++;

}
}

closedir($dir_handle);

echo '<h2>'."counted: " . $filecount . " files.".'</h2>';

?>
I have tried to copy to the same directory as well - no luck. the
webserver has access to the folders in question. What do I need to do
to get this to work?

Thanks Again,

eholz1
Permissions problem? On Apache *nix servers, PHP generally runs as
"nobody". Try to chmod 777 the directory to see if thast solves the
problem.
Jan 23 '07 #2

P: n/a
"eholz1" <ew****@gmail.comwrote:
>Hello PHP group,

I am trying to use the copy function to copy files from one directory
to another.
This does not seem to work for me. What am I missing?

Below is the code I am using.

<?php
$filecount = 0;
$filelist[0] = '';
$idx = 0;

//define('loc1', '/usr/local/Apache2/htdocs/portfolios', true);
$path = 'testimage';
$thumb_path = 'thumbs';

$dir_handle = @opendir($path) or die("Unable to open directory
$path");

//$filename2 = $filename . '.old';
//copy($filename, $filename2);

while ($file = readdir($dir_handle))
{
$filetyp = strtolower(substr($file, -3));
if ($filetyp == 'jpg' )
{
//loc1. '/'.$path. '/' .$thumb_path . '/' .
$filelist[$idx] = $path . '/' . $file;
$thumbfile = $thumb_path . '/'. 'thumb-'. $file;

if (!@copy($file, $thumbfile)) {
echo "unable to copy file" .'<br>';
}
Follow this through manually. readdir returns only the names of the files
in that directory, not the whole path. If the directory "testimage"
contains a file called "xxx.jpg", your copy command will expand to:

@copy( "xxx.jpg", "thumbs/thumb-xxx.jpg" )

See the problem? "xxx.jpg" doesn't exist in the current directory. It's
inside of "testimage".

You probably want
if( !@copy( $filelist[$idx], $thumbfile )) {
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jan 23 '07 #3

P: n/a
eholz1 wrote:
Hello PHP group,

I am trying to use the copy function to copy files from one directory
to another.
This does not seem to work for me. What am I missing?

Below is the code I am using.

<?php
$filecount = 0;
$filelist[0] = '';
$idx = 0;

//define('loc1', '/usr/local/Apache2/htdocs/portfolios', true);
$path = 'testimage';
$thumb_path = 'thumbs';

$dir_handle = @opendir($path) or die("Unable to open directory
$path");

//$filename2 = $filename . '.old';
//copy($filename, $filename2);

while ($file = readdir($dir_handle))
{
$filetyp = strtolower(substr($file, -3));
if ($filetyp == 'jpg' )
{
//loc1. '/'.$path. '/' .$thumb_path . '/' .
$filelist[$idx] = $path . '/' . $file;
$thumbfile = $thumb_path . '/'. 'thumb-'. $file;

if (!@copy($file, $thumbfile)) {
echo "unable to copy file" .'<br>';
}

$idx++;
$filecount++;

}
}

closedir($dir_handle);

echo '<h2>'."counted: " . $filecount . " files.".'</h2>';

?>
I have tried to copy to the same directory as well - no luck. the
webserver has access to the folders in question. What do I need to do
to get this to work?

Thanks Again,

eholz1
In addition, remove the '@' from the copy command and see what error you
get. It will help your troubleshooting.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jan 23 '07 #4

P: n/a
Hello All,

As always the PHP group comes through! I will check all these options
out.

Thanks

eholz1
If I get the copy copying i will advise.
Thanks again,

eholz1

Jerry Stuckle wrote:
eholz1 wrote:
Hello PHP group,

I am trying to use the copy function to copy files from one directory
to another.
This does not seem to work for me. What am I missing?

Below is the code I am using.

<?php
$filecount = 0;
$filelist[0] = '';
$idx = 0;

//define('loc1', '/usr/local/Apache2/htdocs/portfolios', true);
$path = 'testimage';
$thumb_path = 'thumbs';

$dir_handle = @opendir($path) or die("Unable to open directory
$path");

//$filename2 = $filename . '.old';
//copy($filename, $filename2);

while ($file = readdir($dir_handle))
{
$filetyp = strtolower(substr($file, -3));
if ($filetyp == 'jpg' )
{
//loc1. '/'.$path. '/' .$thumb_path . '/' .
$filelist[$idx] = $path . '/' . $file;
$thumbfile = $thumb_path . '/'. 'thumb-'. $file;

if (!@copy($file, $thumbfile)) {
echo "unable to copy file" .'<br>';
}

$idx++;
$filecount++;

}
}

closedir($dir_handle);

echo '<h2>'."counted: " . $filecount . " files.".'</h2>';

?>
I have tried to copy to the same directory as well - no luck. the
webserver has access to the folders in question. What do I need to do
to get this to work?

Thanks Again,

eholz1

In addition, remove the '@' from the copy command and see what error you
get. It will help your troubleshooting.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jan 23 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.