upload only 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
myasirm
Forum Commoner
Posts: 54
Joined: Sat Sep 12, 2009 3:57 am

upload only text files

Post by myasirm »

hi guys i want to upload only text like like (pdf,note pad and ofiice files) so can any one please tell me how can i restrict that
greg7
Forum Commoner
Posts: 32
Joined: Tue Oct 13, 2009 7:38 am

Re: upload only text files

Post by greg7 »

Hi, you can use

Code: Select all

 
// These are the types of file that will pass the validation.
 $allowed_filetypes =array('.pdf','.txt');
 
// Get the extension from the filename
$ext= strrchr($filename,'.');
 
// Check if the filetype is allowed
    if($ext .....
 
where $filename is the uploaded file, use something like

Code: Select all

$filename=$_FILES['file']['tmp_name'];
or GET/POST method.
adaniels
Forum Newbie
Posts: 1
Joined: Fri Oct 23, 2009 6:54 am

Re: upload only text files

Post by adaniels »

That is not a save option, since i can take an executable and name it abc.pdf. Instead use http://www.php.net/fileinfo
myasirm
Forum Commoner
Posts: 54
Joined: Sat Sep 12, 2009 3:57 am

Re: upload only text files

Post by myasirm »

i used this code when in (if condition) i only give application/pdf than it is working and only pdf files are uploading but when i give or(||) condition to upload word files also it is not working kindly help me in this regards

if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else if($_FILES["file"]["type"] != "application/pdf" || $_FILES["file"]["type"] != "text/plain" )
{
echo "only pdf,text,word and excel files are allowed";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"D:/Departments/weaving/" . $_FILES["file"]["name"]);
echo "Stored in: " . "D:/Departments/weaving/" . $_FILES["file"]["name"];
}
greg7
Forum Commoner
Posts: 32
Joined: Tue Oct 13, 2009 7:38 am

Re: upload only text files

Post by greg7 »

I'm sorry i was in a hurry and careless to my post, of course firstly you have to exclude the executables or files that contain scripts. You can do something like the following

Code: Select all

$blacklist = array(".php", ".phtml", ".php3", ".php4", ".php5",".js", ".shtml", ".pl" ,".py",".htm");
  foreach ($blacklist as $file)
  {
    if(preg_match("/$file\$/i", $_FILES['ufile']['name']))
     {
        echo "ERROR: Uploading executable files Not Allowed\n";
        exit;
     }
then check the content type

Code: Select all

if($_FILES["ufile"]["type"] = "application/pdf" || $_FILES["ufile"]["type"] = "text/plain" )
 
at your last post i saw

Code: Select all

echo "only pdf,text,word and excel files are allowed";
if you wish to upload that files, for excel use "application/vnd.ms-excel", for word "application/msword" etc. instead of plain text for all the types. Finally do what i refer to my first post if you wish to handle with different way the files. Although the above may do your job, for sufficient security take a look at the following links
http://www.mysql-apache-php.com/fileupload-security.htm , http://www.scanit.be/uploads/php-file-upload.pdf
and of course to adaniels post.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: upload only text files

Post by superdezign »

myasirm wrote:

Code: Select all

if($_FILES["file"]["type"] != "application/pdf" || $_FILES["file"]["type"] != "text/plain" )
I think you meant to use && (AND), not || (OR).
If the file type is not a PDF AND it is not a text file, then there is an error.
myasirm
Forum Commoner
Posts: 54
Joined: Sat Sep 12, 2009 3:57 am

Re: upload only text files

Post by myasirm »

no brother in AND you have to meet with both conditions but in OR if any of them is right the program will run other wise it show error

thanks to all i resolved my problem with ur help guys
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: upload only text files

Post by superdezign »

myasirm wrote:no brother in AND you have to meet with both conditions but in OR if any of them is right the program will run other wise it show
But you are using the not-equal operator, not the equal operator.
Checking if something IS one thing or IS another thing is the negation of checking if something IS NOT one thing and IS NOT another thing.
Post Reply