我对此代码有一些问题。 问题是关于脚本的上传部分,当用户保存信息时,文件不会被脚本上传。也许我犯了一些错误? xml文件正常工作,并保存任何新插入的信息。我有两个文件输入来分隔文件..我认为问题与此有关..?
<?php
if(isset($_POST['savetrack'])){
$errors= array();
foreach($_FILES['audio_file']['tmp_name'] as $key => $tmp_name ){
$Audiofile_name = $key.$_FILES['audio_file']['name'][$key];
$Audiofile_size =$_FILES['audio_file']['size'][$key];
$Audiofile_tmp =$_FILES['audio_file']['tmp_name'][$key];
$Audiofile_type=$_FILES['audio_file']['type'][$key];
$audioextensions = array("mp3");
$Audiofile_ext=explode('.',$_FILES['audio_file']['name'][$key]) ;
$Audiofile_ext=end($Audiofile_ext);
$Audiofile_ext=strtolower(end(explode('.',$_FILES['audio_file']['name'][$key])));
if(in_array($Audiofile_ext,$audioextensions ) === false){
$errors[]="extension not allowed";
}
if($file_size > 50097152){
$errors[]='File size must be less than 50 MB';
}
$desired_dir="mp3";
if(empty($errors)==true){
if(is_dir("$desired_dir/".$Audiofile_name)==false){
move_uploaded_file($Audiofile_tmp,"mp3/".$Audiofile_name);
}else{ //rename the file if another one exist
$new_dir="mp3/".$Audiofile_name.time();
rename($Audiofile_tmp,$new_dir) ;
}
}else{
print_r($errors);
}
}
//封面上传
foreach($_FILES['cover_file']['tmp_name'] as $key => $tmp_name ){
$Imgfile_name = $key.$_FILES['cover_file']['name'][$key];
$Imgfile_size =$_FILES['cover_file']['size'][$key];
$Imgfile_tmp =$_FILES['cover_file']['tmp_name'][$key];
$Imgfile_type=$_FILES['cover_file']['type'][$key];
$imgextensions = array("jpeg","jpg","png");
$Imagefile_ext=explode('.',$_FILES['cover_file']['name'][$key]) ;
$Imagefile_ext=end($Imgfile_ext);
$Imagefile_ext=strtolower(end(explode('.',$_FILES['cover_file']['name'][$key])));
if(in_array($Imgfile_ext,$imgextensions ) === false){
$errors[]="extension not allowed";
}
if($Imgfile_size > 50097152){
$errors[]='File size must be less than 50 MB';
}
$desired_imgdir="img";
if(empty($errors)==true){
if(is_dir("$desired_imgdir/".$Imgfile_name)==false){
move_uploaded_file($Imgfile_tmp,"img/".$Imgfile_name);
}else{ //rename the file if another one exist
$imgnew_dir="img/".$Imgfile_name.time();
rename($Imgfile_tmp,$imgnew_dir) ;
}
}else{
print_r($errors);
}
}
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('tracks.xml');
$filenameAudio = $_FILES['audio_file']['name'];
$element = $xml->getElementsByTagName('track')->item(0);
$artistname = $element->getElementsByTagName('artist')->item(0);
$tracktitle = $element->getElementsByTagName('title')->item(0);
$relyear = $element->getElementsByTagName('year')->item(0);
$relabel = $element->getElementsByTagName('label')->item(0);
$cover = $element->getElementsByTagName('cover')->item(0);
$audio = $element->getElementsByTagName('audio')->item(0);
$newItem = $xml->createElement('track');
$newItem->appendChild($xml->createElement('artist', $_POST['artist_name']));
$newItem->appendChild($xml->createElement('title', $_POST['track_title']));
$newItem->appendChild($xml->createElement('year', $_POST['year']));
$newItem->appendChild($xml->createElement('label', $_POST['record_label']));
$newItem->appendChild($xml->createElement('cover',$Imgfile_name));
$newItem->appendChild($xml->createElement('audio',$Audiofile_name));
$xml->getElementsByTagName('releases')->item(0)->appendChild($newItem);
$xml->save('tracks.xml');
?>
答案 0 :(得分:0)
我通过使用上传课程找到了解决方案,但它似乎有效。现在问题是将上传的音频文件加载到我需要的地方,consolle给我以下错误:
未捕获(在承诺中)DOMException:无法解码音频数据 获取http://localhost:8810/mixtape/mp3/audiofile 404(未找到)
我不知道这个问题是否是因为文件名是从保存到xml中的标记加载的,或者是否存在其他类型的问题。如果我手动插入文件位置它将工作,但我需要从xml加载它。
这里是我需要加载mp3文件的html代码,我已经插入了上传mp3托管的路径,但没有成功。
<ul class="grid">
<?php
try{
$xml=simplexml_load_file("tracks.xml") or die("Error: Cannot create object");
}catch (Exception $e){
echo $e;
}
foreach($xml as $child)
{
$artistname = $child->artist;
$tracktitle = $child->title;
$relyear = $child->year;
$label = $child->label;
$audiofile = $child->audio;
$coverfile = $child->cover;
?>
<li class="grid__item">
<a class="grid__link" href="#album" data-playlist-1="mp3/<?php echo $audiofile ?>" data-playlist-2="mp3/<?php echo $audiofile ?>">
<div class="img-wrap img-wrap--grid">
<svg class="lp lp--grid">
<use xlink:href="#icon-lp-mini"></use>
</svg>
<img class="img img--grid" src="img/<?php echo $coverfile ?>" alt="album cover" />
</div>
<span class="year"><?php echo $relyear ?></span>
<h2 class="artist"><?php echo $artistname ?></h2>
<h3 class="title"><?php echo $tracktitle ?></h3>
</a>
</li>
<?php } ?>
</ul>