如何使用php将音频文件保存到所需文件夹

如何使用php将音频文件保存到所需文件夹,php,Php,在我的项目中,我使用mkdir mkdir("C:/xampp/htdocs/ampache-3.8.6_all/songs/admin/" . $artist_name . "/" . $name. "/", 0700); 文件夹已成功创建 但我需要把音频文件保存到那个文件夹里 音频文件存储在$filename 我使用了fopen($filename,“r”)或die('cannotopenfile'),但音频文件未保存在该文件夹中 这里我分享我的代码 <?php header("A

在我的项目中,我使用mkdir

mkdir("C:/xampp/htdocs/ampache-3.8.6_all/songs/admin/" . $artist_name . "/" . $name. "/", 0700);
文件夹已成功创建

但我需要把音频文件保存到那个文件夹里

音频文件存储在
$filename

我使用了
fopen($filename,“r”)或die('cannotopenfile'),但音频文件未保存在该文件夹中

这里我分享我的代码

<?php

header("Access-Control-Allow-Origin: *");

if(isset($_SERVER['HTTP_ORIGIN']))
{
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400');    // cache for 1 day
}

//Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') 
{
    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         
    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
        header("Access-Control-Allow-Headers: 
{$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
    exit(0);
}

 $con = mysqli_connect("localhost","root","upload","ampache");

        // Check connection
        if (mysqli_connect_errno())
        {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }


 $postdata = file_get_contents("php://input");
 if(isset($postdata))
 {
    $request = json_decode($postdata);
    $name = $request->testname;
    $id = $request->testid; 
    $file = $request->testfile;
    $filename = basename("c:\fakepath\ $file");
     echo $filename; 

    $artist_name = $request->testartist;
     $catalog = 1;

     $enabled = 1;

     $channels = 2;

     $update_time = 0;

     $user_upload = 1;

     $bitrate = 192000;

     $rate = 44100;

     $mode = "cbr"; 

     $filepath = 'C:\\\xampp\\\htdocs\\\ampache-3.8.6_all\\\songs\\\admin\\\ 
    ';


    $audiopath = $filepath.$artist_name."/".$name."\\\ ".$filename;



       $path =  mkdir("C:/xampp/htdocs/ampache-3.8.6_all/songs/admin/" . 
     $artist_name . "/" . $name. "/", 0700);


    if($name != "" && $id != "")
    {
        $con = mysqli_connect("localhost","root","upload","ampache");

        // Check connection
        if (mysqli_connect_errno())
        {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        else
        {
            //echo "Name: " .$name;
            //echo "id: " .$id;

            //$sql  = "INSERT INTO album(name, album_artist) VALUES 
            // ('$name', '$id')";  
            $sql  = "INSERT INTO album(name, album_artist) VALUES ('$name', 
     '$id')";

            $stmt = mysqli_query($con, $sql) or die ("MySQL 
      Error:".mysqli_error($con));

            echo "successfully inserted !";
        }

         $lastid = mysqli_insert_id($con);    

        $sql1 = "INSERT INTO song(catalog, enabled, channels, update_time, 
     user_upload, mode, bitrate, rate, artist, album, title, file) VALUES 
     ('$catalog', '$enabled', '$channels', '$update_time', '$user_upload', 
    '$mode', '$bitrate', '$rate', '$id', '$lastid', '$filename', 
    '$audiopath')";
         $stmt1 = mysqli_query($con, $sql1) or die ("MySQL 
     Error:".mysqli_error($con));
    }
    else 
    {
        echo "Empty name and description parameter!";
    }
    }
    else 
    {
    echo "Not called properly with name and description parameter!";
    }


   ?>   

请为我们提供完整的php上载部分使用
fopen()读取文件不会保存文件,尤其是在只读模式下。我添加了完整的代码@Yanis git你想复制或移动你的文件吗?我想移动我的文件,但我不知道怎么做?@Yanis git