How to change the name of the file when the server is downloaded



  • <?php
    
    // A list of permitted file extensions
    $allowed = array('mp3', 'mp4', 'ogg', 'wav');
    $BN = (isset($_POST['BN']) ? $_POST['BN'] : null);
    $AN = (isset($_POST['AN']) ? $_POST['AN'] : null);
    
    $music_path = "../../bands/".$BN."/music/".$AN;
    
    if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){
    
        $extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);
    
        if(!in_array(strtolower($extension), $allowed)){
            echo '{"status":"error"}';
            exit;
        }
    
        if(move_uploaded_file($_FILES['upl']['tmp_name'], $music_path.'/'.$_FILES['upl']['name'])){
            echo '{"status":"success"}';
            exit;
        }
    }
    else {
        echo '{"status":"error"}';
        exit;
    }
    
    ?>
    

    We have: Bigg Mouth - American Girl.mp3

    Out: Bigg Mouth in American Girl.mp3

    Must be: American Girl.mp3

    Please.



    1. Keep the files on the database.
    2. Use the unique name of the file and keep it in the OBD system for storage on the disc. For example http://php.net/manual/en/function.md5.php + salt

    Otherwise:

    1. What happens if you get loaded again? Bigg Mouth – American Girl.mp3?
    2. What happens if you get a Cyrillian in your name?
    3. How are you gonna put the files on the scan? URL does not support gaps.

    And for your specific problem in processing the name of the song as such, I can suggest http://php.net/manual/ru/function.preg-split.php through the divider and take the last part, for example

    // $realFileName Bigg Mouth – American Girl.mp3
    $realFileName = $_FILES['upl']['name']; // имя файла
    $split = preg_split("/[-|–|—|:|\/]/iu", $realFileName); // делим через возможные делители
    $fileName = trim(end($split)); // нам нужна последняя часть имени файла
    // $fileName будет равен American Girl.mp3
    

    UPD

    Example implementation of your option:

    <?php
    

    // A list of permitted file extensions
    $allowed = array('mp3', 'mp4', 'ogg', 'wav');
    $bandName = (isset($_POST['BN']) ? $_POST['BN'] : null);
    $artistName = (isset($_POST['AN']) ? $_POST['AN'] : null);

    $music_path = DIR . "/../../bands/".$bandName."/music/".$artistName;

    if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){

    $extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);
    $reaFilelName = $_FILES['upl']['name'];
    $fileName = $music_path . '/' . md5($reaFilelName . time());
    
    $isAllow = in_array(strtolower($extension), $allowed);
    if($isAllow &amp;&amp; move_uploaded_file($_FILES['upl']['tmp_name'], $fileName)){
        // сохраняем в БД $reaFilelName для показа имени файла клиенту
        // сохраняем в БД $fileName для отдачи этого файла через php
        echo '{"status":"success"}';
        exit;
    }
    

    }

    echo '{"status":"error"}';
    exit;

    ?>


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2