简体   繁体   中英

Create folder (mkdir) and move a file into this (rename)

I searched on StackOverflow but I have not found a solution to my problem .. I should create a directory (if not already present), and move a file (.Zip) previously created on this new folder

  • I create the zip file correctly
  • I create the new directory correctly (if not present)
  • I move the zip file properly in the new directory, but sometimes, when I have to either create the folder, and move the file, the folder is created, but the file is not moved, probably because the command is executed before the folder is created

I tried the following solutions

//create zip
Zip('editor', 'backup_'.date("d_m_Y__H_i_s").'.zip');
//create folder
mkdir('backup', 0777, true);
//move file
rename('backup_'.date("d_m_Y__H_i_s").'.zip', 'backup/backup_'.date("d_m_Y__H_i_s").'.zip');



//create zip
Zip('editor', 'backup_'.date("d_m_Y__H_i_s").'.zip');
//create folder
$ret = mkdir('backup', 0777, true);
//move file
rename('backup_'.date("d_m_Y__H_i_s").'.zip', 'backup/backup_'.date("d_m_Y__H_i_s").'.zip');



//create zip
Zip('editor', 'backup_'.date("d_m_Y__H_i_s").'.zip');
//create folder
$ret = mkdir('backup', 0777, true);
//move file
move_backup();

function move_backup(){

     rename('backup_'.date("d_m_Y__H_i_s").'.zip', 'backup/backup_'.date("d_m_Y__H_i_s").'.zip');

}

UPDATE : SOLUTION

  @date_default_timezone_set("Europe/Rome");
  $my_time = date("d_m_Y__H_i_s");


  function Zip($source, $destination){
      ....
  }

  function sposto_backup($t){

    rename('backup_'.$t.'.zip', 'backup/backup_'.$t.'.zip');

  }


  Zip('editor', 'backup_'.$my_time.'.zip');

  if (!file_exists('backup')) {
       $r = mkdir('backup', 0777, true);
       sposto_backup($my_time);
  }else{
    sposto_backup($my_time);
  }

这很可能是权限问题-如果您在代码中启用了错误报告功能或捕获了错误,则可以自行识别问题。

You could try moving your mkdir()'s into if statements since the PHP documentation shows that mkdir returns true on success. This means that once the mkdir function has successfully executed, the content inside the if statement will execute.

if(mkdir('backup', 0777, true))
{

}

or

if($ret = mkdir('backup', 0777, true))
{

}

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