skip to Main Content

I use a switch to check files for their file names during upload.
Only files with specific names may be forwarded. However, it only works if the name is fixed.
Is it possible that my switch can also accept variable names?
Here is an example.
I expect a file every day that starts with the name filesource but always ends with a new date like filesource-2023_05_20.csv. The format to be accepted would therefore be filesource-YYYY_MM_DD.csv.

Here my code actually:

switch($infoFichier['filename']){
    // Here my variable filename
   
    case 'filesource*':
        $newFileName = 'filesource*.csv'; 
        $updir = PATH.'/files/';
        $updirst = PATH.'/files/depot/';
        $uploadfile = $updir.'depot/'.$newFileName;
        $fileName = $_FILES['fic_demande']['name'];
        $fileType = $_FILES['fic_demande']['type'];            
        if (move_uploaded_file($_FILES['fic_demande']['tmp_name'], $uploadfile)) {
            echo "File is valid, and was successfully uploaded.n";
            header('location:depot/import.php?file='.$newFileName.'&path='.$updirst.'&type='.$fileType);
        }
        break;

Is there a solution that my switch could accept this fileformat?

THX in advance.

2

Answers


  1. Chosen as BEST ANSWER

    i have it figure out with substring. I add this before the switch :

    $infoFichier = pathinfo($_FILES['fic_demande']['name']);
    
    $sm_dt_vl = substr($infoFichier['filename'],13);
    $flsrc = 'filesource-'.$sm_dt_vl;
    
    case $flsrc :
                $newFileName = $flsrc.'.csv'; 
    

    Thanks everybody for your ideas and Help.


  2. Can you please use this variable $newFileName to $newFileName = ‘filesource-‘.date(‘Y_m_d’).’.csv’;

    switch($infoFichier['filename']){
       
    case 'filesource*':
        $newFileName = 'filesource-'.date('Y_m_d').'.csv';
        $updir = PATH.'/files/';
        $updirst = PATH.'/files/depot/';
        $uploadfile = $updir.'depot/'.$newFileName;
        $fileName = $_FILES['fic_demande']['name'];
        $fileType = $_FILES['fic_demande']['type'];            
        if (move_uploaded_file($_FILES['fic_demande']['tmp_name'], $uploadfile)) {
            echo "File is valid, and was successfully uploaded.n";
            header('location:depot/import.php?file='.$newFileName.'&path='.$updirst.'&type='.$fileType);
        }
        break;
    

    Thanks

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search