Jump to content

Limit upload file size


james101

Recommended Posts

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

<?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>";

?>

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.