简体   繁体   English

将同一文件上传到多个文件夹

[英]Upload the same file into multiple folders

I have an uploading form, and when i select the file,i need it to be uploaded in multiple folders.Any ideas how to do that? 我有一个上传表单,当我选择文件时,我需要将它上传到多个文件夹。任何想法如何做到这一点? I've tried with a loop like the following one: 我试过像下面这样的循环:

foreach($_POST['check'] as $check){     
    move_uploaded_file($_FILES['doc']['tmp_name'], $target_path);
    chmod($target_path,0777);    
}

But it only upload once.Any ideas please? 但它只上传一次。有什么想法吗?

After uploading, copy the file from the target path to the other paths with copy() . 上传后,使用copy()将文件从目标路径复制到其他路径。

foreach($_POST['check'] as $check){     
    move_uploaded_file($_FILES['doc']['tmp_name'], $target_path);
    chmod($target_path,0777);    
    // and now...
    copy($target_path, $target_path_2);
    copy($target_path, $target_path_3);
    // etc...
}

By the way, setting 0777 for permissions generally is unnecessary and a bad idea. 顺便说一下,为权限设置0777通常是不必要的,也是一个坏主意。 You want anyone to upload files and let any user execute them? 您希望任何人上传文件并让任何用户执行它们吗? That's the way to start giving anyone full control over your machine. 这是开始让任何人完全控制你的机器的方法。

Also, are you sure that you need the file on multiple places? 此外,您确定需要多个位置的文件吗? Why not have one common storage folder and create symbolic links to it? 为什么没有一个公共存储文件夹并创建它的符号链接? But that depends on your setup, of course. 但这当然取决于你的设置。

上传一次,然后只需复制()它。

$i = 0; 
foreach($_POST['check'] as $check){ 
   $basePath = "/var/www/html/more/phpexm/"; 
   $target_path = $basePath . $check; 

   if (!file_exists($target_path)){ 
       mkdir($target_path, 0777);
   }

   if ($i == 0){
       $sFileNameTmp = $_FILES['doc']['tmp_name'];
       $sFileName = $_FILES['doc']['name'];
       move_uploaded_file($sFileNameTmp, $target_path . '/' . $sFileName);
       $sFirstFileUploaded = $target_path; 
   }
   else{ 
       copy ($sFirstFileUploaded . '/' . $sFileName,  $target_path  . '/' . $sFileName); 
    }
    $i++;   
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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