javascript - how to upload all types of audio files to a folder using php
Get the solution ↓↓↓i have this web program, and i am uploading two types of files, one in image format and one in audio format, the image files are uploading perfectly and any type of image can work, but some audio files are not uploading, its not like a specific type of file eg. wav that is not uploading, just that some mp3 files will upload and other mp3 files would not;
here is the form
<form method="post" enctype="multipart/form-data">
<input type="text" id="bname" placeholder="name"><br><br>
<input type="text" id="bprice" placeholder="price"><br><br>
<label>artwork</label><input type="file" id="bartwork" placeholder="artwork" accept="image/*">
<br><br>
<label>beat</label><input type="file" id="bbeat" placeholder="beat" accept="audio/*"><br><br>
<input type="submit" id="beats" value="Upload">
</form>
here is the javascript code
var art = document.getElementById('bartwork')//this gets the file input
const beat = document.getElementById('bbeat')//this gets the file input
document.getElementById('beats').addEventListener('click',beats)
function beats(e) {
e.preventDefault()
// console.log(file1.files[0])
let name = document.getElementById('bname')
let price = document.getElementById('bprice')
const formdata = new FormData();
formdata.append('name',name.value);
formdata.append('price',price.value);
formdata.append('artwork',art.files[0]);
formdata.append('beat',beat.files[0]);
console.log(beat.files[0])
console.log(art.files[0])
fetch("php/uploadbeat.php",{method: 'POST',body:formdata})
.then((res) => res.text())
.then((data) => {
alert(data)
})
}
and here is the php
include('connect.php');
$name = $_POST['name'];
$price = $_POST['price'];
$file= $_FILES['beat'];
$filename = $_FILES['beat']['name'];
$filetmp = $_FILES['beat']['tmp_name'];
$filesize = $_FILES['beat']['size'];
$fileerror = $_FILES['beat']['error'];
$filetype = $_FILES['beat']['type'];
$fileext = explode('.',$filename);
$fileAcualext = preg_replace( "/\r|\n/", "", strtolower(end($fileext)));
if($fileerror === 0)
{
if($filesize <10000000)
{
echo $fileAcualext;
$filenamenew = $name.".".$fileAcualext;
$fiedestination = '../beats/'.$filenamenew;
move_uploaded_file($filetmp,$fiedestination);
}
else{
echo "File to big ";
}
$paco = 0;
}
else{
$paco = 1;
echo "There was an error ". $fileerror;
}
$file2= $_FILES['artwork'];
$file2name = $_FILES['artwork']['name'];
$file2tmp = $_FILES['artwork']['tmp_name'];
$file2size = $_FILES['artwork']['size'];
$file2error = $_FILES['artwork']['error'];
$file2type = $_FILES['artwork']['type'];
print_r($file);
$file2ext = explode('.',$file2name);
$file2Acualext = preg_replace( "/\r|\n/", "", strtolower(end($file2ext)));
if($file2error === 0)
{
if($file2size <10000000)
{
$filenamenew2 = $name.".".$file2Acualext;
$fiedestination2 = '../beatsartwork/'.$filenamenew2;
move_uploaded_file($file2tmp,$fiedestination2);
}
else{
echo "File to big ";
}
$paco = 0;
}
else{
$paco = 1;
echo "There was an error ";
}
if($paco == 1)
{
echo 'we could not upload ur stupid file fuck of bicth';
}
else{
$query ="INSERT INTO `beats`(`name`, `price`, `artwork`, `beat`) VALUES ('$name','$price','../beatsartwork/$filenamenew2','../beats/$filenamenew')";
$selectt = mysqli_query($conn,$query);
if(mysqli_affected_rows($conn)>0){
// header("Location:login.php");
echo 1;
}
else{
echo 0;
}
}
also i decided to see what the error was for myself so i printed the audio file of the screen and i saw that it had no type and no tmpname something like this:
Array
(
[name] => Burna Boy – Anybody.mp3
[type] =>
[tmp_name] =>
[error] => 1
[size] => 0
)
i don't know what the problem is, thanks for helping in advance
Answer
Solution:
You have an error in your upload response [error] => 1
This is because you have selected a file that its size is bigger than your post_max_size or upload_max_filesize.
Increase these values from php.ini to a proper value and restart web server and PHP in your machine.
list of constants that you can use to detect errors while uploading files.
define ('UPLOAD_ERR_OK', 0);
define ('UPLOAD_ERR_INI_SIZE', 1);
define ('UPLOAD_ERR_FORM_SIZE', 2);
define ('UPLOAD_ERR_PARTIAL', 3);
define ('UPLOAD_ERR_NO_FILE', 4);
define ('UPLOAD_ERR_NO_TMP_DIR', 6);
define ('UPLOAD_ERR_CANT_WRITE', 7);
define ('UPLOAD_ERR_EXTENSION', 8);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: trying to access array offset on value of type bool in
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.