brad62
January 16, 2013, 10:10am
1
I have been thinking and thinking about different solutions to a web page I’m working on.
I have users, that will be able to upload their sound files to my site.
Since people don’t know how to convert things the right way, I guess there might be different things showing up.
I would like the users to be able to upload either a wav or mp3 file and nothing else.
Is there ready scripts out there that I can copy and paste and modify? I guess people have been doing this before, so I guess there might be one or many working codes.
What I need is a form where the user can upload a file to a dir on my server.
Probably the file size might be as large as 30MB sometimes (if using wav).
But I want the script to only accept mp3 or wav files.
I found a code that might be a part of what I’m looking for, but not everything.
Any ideas or maybe someone knows where I should look?
Or is it a simple code just adding bits to this code?
<?php
// Upload location
//$sound_path = getcwd().DIRECTORY_SEPARATOR;
$sound_path = "audio/";
$result = 0;
$goal_path = $sound_path . basename( $_fileS['myfile']['name']);
if(@move_uploaded_file($_fileS['myfile']['tmp_name'], $goal_path))
{
$result = 1;
setcookie("audiofileOne", $goal_path);
}
?>
MarPlo
January 16, 2013, 11:53am
2
Hi,
Try this script (it can be used to allow to upload the type of files you want, see the comments in code):
<?php
// Simple PHP Upload Script: http://coursesweb.net/php-mysql/
$uploadpath = 'upload/'; // directory to store the uploaded files
$max_size = 30000; // maximum file size, in KiloBytes
$alwidth = 900; // maximum allowed width, in pixels
$alheight = 800; // maximum allowed height, in pixels
$allowtype = array('wav', 'mp3'); // allowed extensions
if(isset($_FILES['fileup']) && strlen($_FILES['fileup']['name']) > 1) {
$uploadpath = $uploadpath . basename( $_FILES['fileup']['name']); // gets the file name
$sepext = explode('.', strtolower($_FILES['fileup']['name']));
$type = end($sepext); // gets extension
list($width, $height) = getimagesize($_FILES['fileup']['tmp_name']); // gets image width and height
$err = ''; // to store the errors
// Checks if the file has allowed type, size, width and height (for images)
if(!in_array($type, $allowtype)) $err .= 'The file: <b>'. $_FILES['fileup']['name']. '</b> not has the allowed extension type.';
if($_FILES['fileup']['size'] > $max_size*1000) $err .= '<br/>Maximum file size must be: '. $max_size. ' KB.';
if(isset($width) && isset($height) && ($width >= $alwidth || $height >= $alheight)) $err .= '<br/>The maximum Width x Height must be: '. $alwidth. ' x '. $alheight;
// If no errors, upload the image, else, output the errors
if($err == '') {
if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath)) {
echo 'File: <b>'. basename( $_FILES['fileup']['name']). '</b> successfully uploaded:';
echo '<br/>File type: <b>'. $_FILES['fileup']['type'] .'</b>';
echo '<br />Size: <b>'. number_format($_FILES['fileup']['size']/1024, 3, '.', '') .'</b> KB';
if(isset($width) && isset($height)) echo '<br/>Image Width x Height: '. $width. ' x '. $height;
echo '<br/><br/>Image address: <b>http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\\\/').'/'.$uploadpath.'</b>';
}
else echo '<b>Unable to upload the file.</b>';
}
else echo $err;
}
?>
<div style="margin:1em auto; width:333px; text-align:center;">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
Upload File: <input type="file" name="fileup" /><br/>
<input type="submit" name='submit' value="Upload" />
</form>
</div>
Here’s another, slightly more simplistic, possibility - using your code:
<?php
$file_type_full = explode('/',$_FILES['myfile']['type']);
echo '<pre>';
print_r($file_type_full);
echo '</pre>';
$file_type = $file_type_full[1];
print 'File type: '.$file_type.'<br>';
echo '<pre>';
print_r($_FILES['myfile']);
echo '</pre>';
if(ISSET($_FILES)){
$sound_path = "audio/";
if($file_type == 'wav' || $file_type == 'mp3'){
$result = 0;
$goal_path = $sound_path . basename( $_FILES['myfile']['name']);
if(move_uploaded_file($_FILES['myfile']['tmp_name'], $goal_path))
{
$result = 1;
setcookie("audiofileOne", $goal_path);
}
if(file_exists($goal_path)){
echo 'File '.$_FILES['myfile']['name'].' uploaded successfully.';
}
}else{
echo 'Wrong file type';
}
}else{
echo 'Please go back and selct a file';
}
?>
brad62
January 16, 2013, 1:55pm
4
Thanks a lot. I will try these within a few days… or next time I have the time to keep working on this little project.