james101 Posted March 31, 2009 Share Posted March 31, 2009 I have the following code which i have set so only certain file types can be uploaded, but i want to do the same for file size. I want to only allow files under 10mb to be uploaded, if the file is over 10mb a message will appear saying so. I was using $max_file_size="10485760"; with an if statement but i couldn't get it to work correctly. If you could help me out, that would be great. Thanks! <?php session_start(); include "db_connect.php"; if($_SESSION['id']) { $sql = "SELECT username FROM `users` WHERE `id`='".$_SESSION['id']."'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) != 1) { session_destroy(); echo "<script language=\"Javascript\" type=\"text/javascript\">document.location.href='login.php'</script>"; } else { $row = mysql_fetch_assoc($res); $title = protect($_POST['title']); if(!$title) { echo "<script language=\"Javascript\" type=\"text/javascript\"> alert(\"You must choose a title for your music!\") document.location.href='editmusic.php'</script>"; } $target = $row['username']; if(!is_dir($target)) @mkdir($target); $target = $target . '/music'; if(!is_dir($target)) @mkdir($target); $target = $target."/".basename($_FILES['music']['name']) ; $size = $_FILES['music']['size']; $music = $_FILES['music']['name']; $type = $_FILES['music']['type']; $max_file_size="10485760"; if($type == "audio/mpeg" || $type == "audio/x-wav" || $type == "audio/x-ms-wma" || $type == "audio/mp3" || $type == "audio/mpeg3") { $sql2= "INSERT INTO `user_music` (`profile_id`,`title`,`size`,`type`,`reference`) VALUES ('".$_SESSION['id']."','$title','$size','$type','$music'); "; move_uploaded_file($_FILES['music']['tmp_name'], $target); $res2 = mysql_query($sql2) or die(mysql_error()); echo "<script language=\"Javascript\" type=\"text/javascript\"> alert(\"Your Music has been uploaded\") document.location.href='editmusic.php'</script>"; } else { echo "<script language=\"Javascript\" type=\"text/javascript\"> alert(\"Wrong file format. Please upload only .mp3, .wav or .wma files\") document.location.href='editmusic.php'</script>"; } } } else echo "<script language=\"Javascript\" type=\"text/javascript\">document.location.href='login.php'</script>"; ?> Link to comment https://forums.phpfreaks.com/topic/151958-limit-upload-file-size/ Share on other sites More sharing options...
Brian W Posted March 31, 2009 Share Posted March 31, 2009 <?php session_start(); include "db_connect.php"; if($_SESSION['id']) { $sql = "SELECT username FROM `users` WHERE `id`='".$_SESSION['id']."'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) != 1) { session_destroy(); echo "<script language=\"Javascript\" type=\"text/javascript\">document.location.href='login.php'</script>"; } else { $row = mysql_fetch_assoc($res); $title = protect($_POST['title']); if(!$title) { echo "<script language=\"Javascript\" type=\"text/javascript\"> alert(\"You must choose a title for your music!\") document.location.href='editmusic.php'</script>"; } $target = $row['username']; if(!is_dir($target)) @mkdir($target); $target = $target . '/music'; if(!is_dir($target)) @mkdir($target); $target = $target."/".basename($_FILES['music']['name']) ; $size = $_FILES['music']['size']; $music = $_FILES['music']['name']; $type = $_FILES['music']['type']; $max_file_size=10485760; if($size > $max_file_size){ echo "<script language=\"Javascript\" type=\"text/javascript\"> alert(\"You may not upload files over 10mb\") document.location.href='editmusic.php'</script>"; } else { if($type == "audio/mpeg" || $type == "audio/x-wav" || $type == "audio/x-ms-wma" || $type == "audio/mp3" || $type == "audio/mpeg3") { $sql2= "INSERT INTO `user_music` (`profile_id`,`title`,`size`,`type`,`reference`) VALUES ('".$_SESSION['id']."','$title','$size','$type','$music'); "; move_uploaded_file($_FILES['music']['tmp_name'], $target); $res2 = mysql_query($sql2) or die(mysql_error()); echo "<script language=\"Javascript\" type=\"text/javascript\"> alert(\"Your Music has been uploaded\") document.location.href='editmusic.php'</script>"; } else { echo "<script language=\"Javascript\" type=\"text/javascript\"> alert(\"Wrong file format. Please upload only .mp3, .wav or .wma files\") document.location.href='editmusic.php'</script>"; } } } } else echo "<script language=\"Javascript\" type=\"text/javascript\">document.location.href='login.php'</script>"; ?> Link to comment https://forums.phpfreaks.com/topic/151958-limit-upload-file-size/#findComment-797988 Share on other sites More sharing options...
lonewolf217 Posted March 31, 2009 Share Posted March 31, 2009 filesize limitations should be set in php.ini Link to comment https://forums.phpfreaks.com/topic/151958-limit-upload-file-size/#findComment-797994 Share on other sites More sharing options...
james101 Posted March 31, 2009 Author Share Posted March 31, 2009 Thanks for the replies. As for setting the limit in php.ini, i have done that, i just wanted a custom message to appear for the user stating what the problem was. I have tried the new code, but when i try and upload a file over 10mb, i get the "please enter a title" instead of the file size error message. Any ideas? Link to comment https://forums.phpfreaks.com/topic/151958-limit-upload-file-size/#findComment-798052 Share on other sites More sharing options...
james101 Posted March 31, 2009 Author Share Posted March 31, 2009 I have tried a changing the code, but still having no luck. If anyone could help me out that would be great. Thanks Link to comment https://forums.phpfreaks.com/topic/151958-limit-upload-file-size/#findComment-798123 Share on other sites More sharing options...
Brian W Posted March 31, 2009 Share Posted March 31, 2009 what does protect() do? Link to comment https://forums.phpfreaks.com/topic/151958-limit-upload-file-size/#findComment-798128 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.