Upload script not accepting text files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
attila001122
Forum Newbie
Posts: 3
Joined: Tue Oct 05, 2010 3:48 pm

Upload script not accepting text files

Post by attila001122 »

Hi,

I am about using a script to upload files to the server.
I have find the script as a tutorial, and it works well as it is.
I have added two lines, to include .doc and .txt for accept text documents too.

But I got the "invalid file" message when I try to upload these.

Anyone can help?

The script is below (thanks)


These are tha added lines:

Code: Select all

|| ($_FILES["file"]["type"] == "text/txt")
|| ($_FILES["file"]["type"] == "text/doc")

The whole script:

Code: Select all

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "text/txt")
|| ($_FILES["file"]["type"] == "text/doc")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_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 "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("uploadtest/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"uploadtest/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
Last edited by Benjamin on Wed Oct 06, 2010 6:28 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Upload script not accepting text files

Post by VladSun »

Try adding

Code: Select all

echo $_FILES["file"]["type"]; 
in the very beginning of the code above to see what file types you are receiving. Then change them as appropriate.
There are 10 types of people in this world, those who understand binary and those who don't
attila001122
Forum Newbie
Posts: 3
Joined: Tue Oct 05, 2010 3:48 pm

Re: Upload script not accepting text files

Post by attila001122 »

Hi, thanks.

I was adding the line you suggested, and the result is : application/unknown.

I only guess, that it does not recogmise the .doc.

But I don't know why.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Upload script not accepting text files

Post by VladSun »

[Linux specific]
In my Apache config file (/etc/apache2/mods-available/mime.conf) I have a directive
[text]TypesConfig /ec/mime.conf[/text]

In mime.conf I have (beside the hundreds of the others):
[text]application/msword doc dot[/text]

Do you have these?
There are 10 types of people in this world, those who understand binary and those who don't
attila001122
Forum Newbie
Posts: 3
Joined: Tue Oct 05, 2010 3:48 pm

Re: Upload script not accepting text files

Post by attila001122 »

I could not find the mime.conf...

So uploading files need to be configured on the server?
Post Reply