Reputation: 985
i'm stuck in here
I do this be before,and by using this PHP code to upload image,but when i try to change it to uploading audio file,it just can't upload it?
My PHP code:(Upload Image[Work]):
<?php
$allowed = array('png', 'jpg', 'jpeg', 'gif', 'swf');
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'], 'arts/'.$_FILES['upl']['name'])){
echo '{"status":"success"}';
exit;
}
}
echo '{"status":"error"}';
exit;
}
PHP Code:(Upload Audio[Not Work]):
<?php
$allowed = array('mp3', 'ogg', 'flac');
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'], 'upload/'.$_FILES['upl']['name'])){
echo '{"status":"success"}';
exit;
}
}
echo '{"status":"error"}';
exit;
}
?>
Upvotes: 4
Views: 16363
Reputation: 763
Check you php.ini file and make sure upload_max_filesize is enough. If not please increase the upload_max_filesize value. Then try the code bellow
if(isset($_POST['submit']))
{
$path = "uploads/"; //file to place within the server
$valid_formats1 = array("mp3", "ogg", "flac"); //list of file extention to be accepted
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$file1 = $_FILES['file1']['name']; //input file name in this code is file1
$size = $_FILES['file1']['size'];
$fileInfo=pathinfo($file1);
$ext=$fileInfo['extension'];
if(in_array($ext,$valid_formats1))
{
$actual_image_name = uniqid().".".$ext;
$tmp = $_FILES['file1']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
//success upload
}
else
echo "failed";
}else{
echo "File Support Not Support";
}
}
}
This code is tested so I think it will work fine :)
Upvotes: 0
Reputation: 105
use this code make sure you have the folder on your server
<?php
if(isset($_POST['submit']))
{
$path = "test/music/"; //file to place within the server
$valid_formats1 = array("mp3", "ogg", "flac"); //list of file extention to be accepted
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$file1 = $_FILES['file1']['name']; //input file name in this code is file1
$size = $_FILES['file1']['size'];
if(strlen($file1))
{
list($txt, $ext) = explode(".", $file1);
if(in_array($ext,$valid_formats1))
{
$actual_image_name = $txt.".".$ext;
$tmp = $_FILES['file1']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
//success upload
}
else
echo "failed";
}
}
}
}
?>
<form enctype="multipart/form-data" id="form1" method="post" action="text1.php">
<input type="file" name="file1" accept=".ogg,.flac,.mp3" required="required"/>
<input type="submit" name="submit"/>
</form>
Upvotes: 1
Reputation: 105
try to use
$path = "../test/cover/"; //file to place within the server
$valid_formats1 = array("mp3", "ogg", "flac"); //list of file extention to be accepted
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$file1 = $_FILES['file1']['name']; //input file name in this code is file1
$size = $_FILES['file1']['size'];
if(strlen($name2))
{
list($txt, $ext) = explode(".", $file1);
if(in_array($ext,$valid_formats1))
{
$actual_image_name = $txt.".".$ext;
$tmp = $_FILES['file1']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
//success upload
}
else
echo "failed";
}
}
}
but make sure the form you'll use is correct to support this code asp er include the following
enctype="multipart/form-data" method="post"
Upvotes: 0