Jump to content

[SOLVED] Restricting file type uploads....need help please have tried everything!!!


Tony187uk

Recommended Posts

Hi there,

 

I am just starting to learn PHP coding and have been set a task to created an upload php file that has restrictions on it. I have managed to get the uploading part done but as soon as i try and put restrictions on it the hole thing goes to pot!! I have tried countless examples i have found on the web and nothing seems to be working.

This is the script i have been working with and the current if statement in there doesn't work!!! I am confused i want to be able to restrict the upload type to MP3's and JPG's. If anyone could help would be greatly appreciated!!

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>

 

<body>

<?php

$target = "C:/webs/test/upload/";

$target = $target . basename( $_FILES['uploaded']['name']) ;

$ok=1;

 

//This is our size condition

if ($uploaded_size > 350000)

{

echo "Your file is too large.<br>";

$ok=0;

}

 

//This is our limit file type condition

if ($uploaded_type =="text/php")

{

echo "No PHP files<br>";

$ok=0;

}

 

 

 

//Here we check that $ok was not set to 0 by an error

if ($ok==0)

{

Echo "Sorry your file was not uploaded";

}

 

//If everything is ok we try to upload it

else

{

if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))

{

echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";

}

else

{

echo "Sorry, there was a problem uploading your file.";

}

}

?>

 

?>

</body>

</html>

 

 

Link to comment
Share on other sites

Firstly, Always put your code in [ code ] tags (without spaces).

 

Secondly, this should be changed:

 

//This is our limit file type condition 
if (strtolower(strlen($_FILES['uploaded']['type'], 4)) !== ".mp3" || strtolower(strlen($_FILES['uploaded']['type'], 4)) !== ".jpg") 
{ 
echo "Only MP3 and JPG files allowed!<br>"; 
$ok=0; 
} 

 

 

Link to comment
Share on other sites

try implementing this...

 

<?php
if ((($_FILES["file"]["type"] == "audio/mpeg")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
    }
  }
else
  {
  echo "Invalid file";
  }
?>

Link to comment
Share on other sites

Cheers mattal999,

 

I attempted your solution but it came up with the following error on when i tried to upload a file

 

Wrong parameter count for strlen() in C:\webs\test\upload.php on line 15

Only MP3 and JPG files allowed!

 

any ideas on how to correct this?? thanks

Link to comment
Share on other sites

Firstly, Always put your code in [ code ] tags (without spaces).

 

Secondly, this should be changed:

 

//This is our limit file type condition 
if ($_FILES['uploaded']['type'] == "audio/mpeg" || $_FILES['uploaded']['type'] == "image/jpeg" || $_FILES['uploaded']['type'] == "image/pjpeg") 
} else {
echo "Only MP3 and JPG files allowed!<br>"; 
$ok=0; 
} 

 

Change it to the above.

Link to comment
Share on other sites

Thanks Mattal999,

 

I tried your coding again but received the following error:

Parse error: parse error in C:\webs\test\upload.php on line 16.

If you could look at my code and perhaps see were im going wrong or if you have any other suggestions would be appreciated.

 

Thanx

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php 
$target = "C:/webs/test/upload/"; 
$target = $target . basename( $_FILES['uploaded']['name']) ; 
$ok=1; 

//This is our limit file type condition 
if ($_FILES['uploaded']['type'] == "audio/mpeg" || $_FILES['uploaded']['type'] == "image/jpeg" || $_FILES['uploaded']['type'] == "image/pjpeg") 
} else {
echo "Only MP3 and JPG files allowed!<br>"; 
$ok=0; 
}


//If everything is ok we try to upload it 
else 
{ 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{ 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 
} 
else 
{ 
echo "Sorry, there was a problem uploading your file."; 
} 
} 
?>

?>
</body>
</html>

 

 

Link to comment
Share on other sites

Try this:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php 
$target = "C:/webs/test/upload/"; 
$target = $target . basename( $_FILES['uploaded']['name']) ; 
$ok=1; 

//This is our limit file type condition 
if ($_FILES['uploaded']['type'] == "audio/mpeg" || $_FILES['uploaded']['type'] == "image/jpeg" || $_FILES['uploaded']['type'] == "image/pjpeg") 
} else {
echo "Only MP3 and JPG files allowed!<br>"; 
$ok=0; 
}

//If everything is ok we try to upload it 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{ 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 
} 
else 
{ 
echo "Sorry, there was a problem uploading your file."; 
} 

?>

?>
</body>
</html>

Link to comment
Share on other sites

Oops! Silly me! I forgot a {. Code is below.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php 
$target = "C:/webs/test/upload/"; 
$target = $target . basename( $_FILES['uploaded']['name']) ; 
$ok=1; 

//This is our limit file type condition 
if ($_FILES['uploaded']['type'] == "audio/mpeg" || $_FILES['uploaded']['type'] == "image/jpeg" || $_FILES['uploaded']['type'] == "image/pjpeg") { //Forgot the opening curly bracket here...
} else {
echo "Only MP3 and JPG files allowed!<br>"; 
$ok=0; 
}

//If everything is ok we try to upload it 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{ 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 
} 
else 
{ 
echo "Sorry, there was a problem uploading your file."; 
} 

?>

?>
</body>
</html>

 

My bad. :P

Link to comment
Share on other sites

As an extra layer of 'security', you're best setting up .htaccess to disallow anything but mp3 and image files from being executed from that directory. That way, if someone does get past your validation check - mime type can be spoofed - even if they upload a php file, it won't execute because it's blocked.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.