简体   繁体   中英

How to move content from folder to another with php

I have some problem with making a script move picture/files from one folder to another. I put the code belw Can somebody help me to see what im doing wrong? Grateful for any tip that i'll have.

  <?PHP
       chdir( dirname( __FILE__ ) );
       include '../../bootstrap.php';

       config( 'website.config' );
       config( 'website.countries' );
       import( 'system.cli' );

       class adeleflytte extends Script {

          Public function Main(){

    // Get array of all source files
    $files = scandir("pictures");
    // Identify directories


    $source = "pictures";
    $destination = "/movedpictures";
    // Cycle through all source files
    foreach ($files as $file) {
      if (in_array($file, array(".",".."))) continue;
      // If we copied this successfully, mark it for deletion
      if (copy($source.$file, $destination.$file)) {
        $delete[] = $source.$file;
      }
    }
    // Delete all successfully-copied files
    foreach ($delete as $file) {
      unlink($file);
    }
          }
       }

         CLI::Execute();
        ?>

rename function does this

rename

rename('image1.jpg', 'del/image1.jpg');

Outputting what you're trying to copy is useful and would help track down the issue:

copy($source.$file, $destination.$file)

In this case since source is 'pictures' and $file is, say, 'pic.jpg' you are trying to copy

picturespic.jpg

You need to add / between these.

Additionally your destination /movedpictures is in the root directory of the filesystem which is likely not what you intended.

I think that the best way to move a file is to use a native function: http://www.php.net/rename

As a parameters it accepts also full path, so it does move files as you need it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM