Page 1 of 1

upload only text files

Posted: Fri Oct 23, 2009 5:53 am
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

Re: upload only text files

Posted: Fri Oct 23, 2009 5:57 am
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.

Re: upload only text files

Posted: Fri Oct 23, 2009 7:02 am
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

Re: upload only text files

Posted: Sat Oct 24, 2009 12:52 am
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"];
}

Re: upload only text files

Posted: Wed Nov 04, 2009 12:10 pm
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.

Re: upload only text files

Posted: Wed Nov 04, 2009 12:47 pm
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.

Re: upload only text files

Posted: Tue Nov 10, 2009 2:46 am
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

Re: upload only text files

Posted: Thu Nov 12, 2009 10:15 pm
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.