upload only text files
Moderator: General Moderators
upload only text files
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
Hi, you can use
where $filename is the uploaded file, use something like or GET/POST method.
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 .....
Code: Select all
$filename=$_FILES['file']['tmp_name'];Re: upload only text files
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
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"];
}
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
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 followingthen check the content type at your last post i saw 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.
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;
}Code: Select all
if($_FILES["ufile"]["type"] = "application/pdf" || $_FILES["ufile"]["type"] = "text/plain" )
Code: Select all
echo "only pdf,text,word and excel files are allowed";http://www.mysql-apache-php.com/fileupload-security.htm , http://www.scanit.be/uploads/php-file-upload.pdf
and of course to adaniels post.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: upload only text files
I think you meant to use && (AND), not || (OR).myasirm wrote:Code: Select all
if($_FILES["file"]["type"] != "application/pdf" || $_FILES["file"]["type"] != "text/plain" )
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
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
thanks to all i resolved my problem with ur help guys
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: upload only text files
But you are using the not-equal operator, not the equal operator.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
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.