We help IT Professionals succeed at work.

Want to move files from folder1 to folder2, except upload.php and getfile.html

cunoc
cunoc asked
on
Hi all Experts,
It works great in my "localhost" and failed when I placed it in my server (files are not copy - and no error or sucessful message or anything). Source and Destination in my domain are the same as in my localhost, so it is not a case that the source and destination are mispelling.

My purpose is to move all files in "folder1" to "folder2", (except upload.php and getfile.html) - leave those files in folder1 as they were.

Any help from you all. Thanks
---
<?php
$so = 'folder1/';
$de = 'folder2/';

if ($open = opendir($so))
{
  while (false !== ($file = readdir($open)))
  {
    if ($file != '.' && $file != '..')
    {
       rename("$so/$file", "$de/$file");
       echo("Sucessfully move $so/$file to $de/$file<br />");
    }
  }
  closedir($open);
}
?>
Comment
Watch Question

Avinash ZalaWeb Expert
Commented:
This will move the files to the new location.,

<?php
rename("/tmp/tmp_file.txt", "/home/user/login/docs/my_file.txt");
?>


Hope this helps,
Addy
Avinash ZalaWeb Expert
Commented:
also check permission of you live folders.

It should allow to read and write.
Hope this helps,
Addy

Author

Commented:
Hi Experts,

I tried both your advices and still doesn't work

CHMOD Permission for (folder1 and folder) as (Read - Write - Execute) for (Owner - Group - Public)

Thanks

Test 1:

<?php
rename("folder1/*.*", "folder2/");
?>
---
Test 2:

<?php
$so = 'folder1/';
$de = 'folder2/';

if ($open = opendir($so))
{
  while (false !== ($file = readdir($open)))
  {
    if ($file != '.' && $file != '..')
    {
       rename("$so/$file", "$de/$file");
       echo("Sucessfully move $so/$file to $de/$file<br />");
    }
  }
  closedir($open);
}
?>
Commented:
Maybe the script will say more about errors on your server if you begin with

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

Author

Commented:
Hi Experts,

I am sure I have two folders (folder1 and folder2) and files in folder1, the php script i am running is in the current directory with folder1 and folder2.
---
move.php
folder1
folder2
---
Here is the error:

Warning: opendir(folder1/) [function.opendir]: failed to open dir: No such file or directory in /home/public_html/3.php on line 8

Thanks
ioboxWeb Architect
Commented:
post your folder structure and where your file are placed please.

Author

Commented:
Hi Experts,
Here is my directory structure:
---
main folder
  move.php
  folder1
     +upload.php
     +textfile.txt
     +testing.html
     +Music.mp3
  folder2
ioboxWeb Architect
Commented:
okey... you are opening a folder named folder2 INSIDE folder1 (where your script is running...)

test number 2 should work like this.
<?php
$so = 'folder1/';
$de = '../folder2/';

if ($open = opendir($so))
{
  while (false !== ($file = readdir($open)))
  {
    if ($file != '.' && $file != '..')
    {
       rename("$so/$file", "$de/$file");
       echo("Sucessfully move $so/$file to $de/$file<br />");
    }
  }
  closedir($open);
}
?>

Open in new window

Author

Commented:
Hi iobox:

Here is how it looks like:
Thanks

main folder\move.php
main folder\folder1
                         +upload.php
                         +textfile.txt
                         +testing.html
                         +Music.mp3
main folder\folder2

Author

Commented:
Hi again,

(move.php) is the php script that I am running and want to move all files from folder1 to folder2, except for the upload.php and testing.html

Thanks

Author

Commented:
Hi Experts,

It works finally and my last question is how to move all files from folder1 to folder2, except for the upload.php and testing.html
Thanks

Commented:
Don't mean to jump in but there aren't many open questions out there!  You might do it like this:
<?php
$so = 'folder1/';
$de = '../folder2/';

if ($open = opendir($so))
{
  while (false !== ($file = readdir($open)))
  {
    if ($file != '.' && $file != '..')
    {
      if ($file != "upload.php" && $file != "testing.html") {
        rename("$so/$file", "$de/$file");
        echo("Sucessfully move $so/$file to $de/$file<br />");
      }
    }
  }
  closedir($open);
}
?>

Open in new window

What's the O/S?

At this point, I'd try moving the files via shell commands to see if that produces similar errors.
OK, it's working.  Good.  I started my last post and got interrupted.

I think EMB has a good answer to the second question.  Is this resolved now or do you need more help?

Author

Commented:
Hi all Experts,
Thank you all the helps you guys have assisted me and hope you guys have a nice weekend.