PHP - Moving multiple files with different files names to own directory

我怕爱的太早我们不能终老 提交于 2019-12-13 02:59:58

问题


Hi wonder if you can help,

I'm looking to do this in PHP if someone can help me. I have a number of files that look like this:

"2005532-JoePharnel.pdf"

and

"1205121-HarryCollins.pdf"

Basically I want to create a PHP code that when someone ftp uploads those files to the upload folder that it will 1) Create a directory if it doesn't exist using there name 2) Move the files to the correct directory (E.g. JoePharnel to the JoePharnel Directory ignoring the number at the beginning)

I have looked through alot of code and found this code and have adapted it:

<?php
    $attachments = array();
    preg_match_all('/([^\[]+)\[([^\]]+)\],?/', $attachments, $matches, PREG_SET_ORDER);
    foreach ($matches as $file) {
    $attachments[$file[1]] = $file[2];
    }
    foreach ($attachments as $file => $filename) {
            $uploaddir = "upload" . $file;
            $casenumdir = "upload/JoePharnel" . $CaseNumber;
            $newfiledir = "upload/JoePharnel" . $CaseNumber .'/'. $file;
            $each_file = $CaseNumber .'/'. $file;

   if(is_dir($casenumdir)==false){
       mkdir("$casenumdir", 0700);          // Create directory if it does not exist
    }
   if(file_exists($casenumdir.'/'.$file)==false){
        chmod ($uploaddir, 0777);
        copy($uploaddir,$newfiledir);
    }
    $allfiles = $CaseNumber .'/'. $file . "[" . $filename . "]" . ",";
    $filelistfinished = preg_replace("/,$/", "", $allfiles);
    echo $filelistfinished; 
    // displays multiple file attachments on the page correctly as: casenumber/testfile1.pdf[testfile1.pdf],casenumber/testfile2.pdf[testfile2.pdf],

],

} ?>

Sorry for the lack of code but best i could find.

Any help is much appreciated.

Thanks.

来源:https://stackoverflow.com/questions/26485435/php-moving-multiple-files-with-different-files-names-to-own-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!