试图将$_SESSION['bandname'];附加到mp3文件上载中,此概念
是指当有人上传歌曲时,如果有意义,它会将乐队名称附加到mp3 band name_songname.mp3中。这是我目前的密码。
问题在于这一行我认为$aditionalnewFileName=$band name.=“\u”$aditionofileName;这个奇怪的部分是当我使用var廑u dump($bandname)时,我用字符串(88)“u police.ogg廑u police.ogg廑u police.ogg廑u police.ogg廑u police.mp3廑u police.wav”测试的不是乐队名称而是歌曲。也许mysqli会更简单?

<?php
session_start();
if (isset      ($_SESSION ['band_id'  ]))
{
$band_id  = $_SESSION ['band_id'  ];
$bandname = $_SESSION ['bandname' ];
$username = $_SESSION ['username' ];
}
var_dump($_SESSION['bandname']);

ini_set( "max_execution_time", "3600" ); // sets the maximum execution
time of this script to 1 hour.

$uploads_dir     = $_SERVER['DOCUMENT_ROOT'].'/mp3';

$aditiontmp_name = $_FILES['song_name']['tmp_name']; // get client
//side file tmp_name
// '/[^A-Za-z0-9\-_\'.]/', ''   //$_FILES['song_name']['name']);
$aditionofileName  = preg_replace('/[^A-Za-z0-9\-_\'.]/',
'',$_FILES['song_name']['name']); // get client side file name remove
the special character with preg_replace function.

// remove time() to edit name of mp3
$aditionalnewFileName = $bandname.="_".$aditionofileName; //filename
changed with current time

if ( move_uploaded_file($aditiontmp_name,
"$uploads_dir/$aditionalnewFileName")) //Move uploadedfile
{

$uploadFile = $uploads_dir."/".$aditionalnewFileName; //Uploaded file
path

 $ext = pathinfo($uploads_dir."/".$aditionalnewFileName,
 PATHINFO_EXTENSION); //Get the file extesion.

$uploadFilebasename = basename($uploads_dir."/".$aditionalnewFileName,
".".$ext); //Get the basename of the file without extesion.

$exName = ".mp3";

$finalFile = $uploads_dir."/".$uploadFilebasename.$exName; //Uploaded
file name changed with extesion .mp3

$encode_cmd = "/usr/bin/ffmpeg -i $uploadFile -b:a 256000 $finalFile
2>&1"; // -i means input file -b:a means bitrate 2>&1 is use for debug
command.

exec($encode_cmd,$output); //Execute an external program.

echo "<pre>";
// will echo success , for debugging we can uncomment echo
print_r($output);

// also want to add redirect to this script to send back to profile
after upload

echo "The file was uploaded";

//echo print_r($output); //  Report of command excution process.

            echo "</pre>";

if($ext !== 'mp3'){ // If the uploaded file mp3 which is not remove
from uploaded directory because we need to convert in to .mp3
unlink( $uploadFile );
}

 //0644 vs 0777
chmod( $finalFile, 0777 ); // Set uploaded file the permission.


 }
 else
{
echo "Uploading failed"; //If uploding failed.
}

?>

最佳答案

所以过了一段时间,我决定换一种方式。我使用mysqli,我挖掘了用户名和bandname,然后使用while循环,在盯着我的代码看之后,我发现我在编辑错误的行,所以我更改了$aditionofileName=preg撸replace('/[^A-Za-z0-9-'.]/','$bandname。
$_FILES['song_name']['name']);并将我认为有问题的行改为$adionalnewfilename=“u”。$aditionofileName;revmoed变量并删除。
下面是新代码。

<?php
session_start();

if (isset      ($_SESSION ['band_id'  ]))
{
$band_id  = $_SESSION ['band_id'  ];
$bandname = $_SESSION ['bandname' ];
$username = $_SESSION ['username' ];
}

if (isset      ($_GET ['band_id']))
{                               // Yes
$showband = $_GET ['band_id'];
}
else
{                               // No
  echo "ID not set";       // Just show the member
}

include 'connect.php';

$sql     = "SELECT * from members WHERE band_id=$showband";

$result  = mysqli_query ($dbhandle, $sql);

while ($row = mysqli_fetch_array ($result))
{
$username = $row ["username"    ];
$bandname = $row ["bandname"    ];
}

var_dump($bandname);

ini_set( "max_execution_time", "3600" ); // sets the maximum execution time of
this script to 1 hour.

$uploads_dir     = $_SERVER['DOCUMENT_ROOT'].'/mp3';

$aditiontmp_name = $_FILES['song_name']['tmp_name']; // get client side file
tmp_name
// '/[^A-Za-z0-9\-_\'.]/', ''   //$_FILES['song_name']['name']);
$aditionofileName  = preg_replace('/[^A-Za-z0-9\-_\'.]/', '',$bandname .
$_FILES['song_name']['name']); // get client side file name remove the special
character with preg_replace function.

// remove time() to edit name of mp3
$aditionalnewFileName = "_".$aditionofileName; //filename changed with current
time

关于php - 尝试使用$ _SESSION ['bandname']将乐队名称附加到mp3上传中;,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54104743/

10-09 23:52