本文介绍了PHP文件类型限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试PHP我的第一个实际脚本,其中大部分来自教程:(无论如何
I am trying out PHP my first actual script, most of it from tutorial :(Anyway's
我在这方面有问题
// This is our limit file type condition
if (!($uploaded_type=="text/java")||!($uploaded_type=="file/class")||!($uploaded_type=="file/jar")) {
echo "You may only upload Java files.<br>";
$ok=0;
}
基本上,它不允许任何文件,即使那里的文件也不允许帮助!我只希望Java文件被允许!
Basically it doesn't allow any files, even those up therehelp!I want the Java files to be allowed only!
这是完整的代码
<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$uploaded = 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/java")||!($uploaded_type=="file/class")||! ($uploaded_type=="file/jar")) {
echo "You may only upload Java files.<br>";
$ok=0;
}
echo $ok; //Here we check that $ok was not set to 0 by an error
if ($ok==0) {
echo "Sorry your file was not uploaded";
}else {
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
echo "The file ". $uploaded ." has been uploaded";
} else {
echo "Sorry, there was a problem uploading your file.";
}
}
?>
推荐答案
您正在使用OR ...,这意味着如果其成员参数的 ANY 为true,则整个语句的值为TRUE.由于文件只能是一种类型,因此您要排除所有文件.您想要的是与"匹配项:
You're using an OR... that means the whole statement evaluates as TRUE if ANY of its member arguments are true. Since a file can only be of one type, you're excluding ALL files. What you want is an 'and' match:
if (!($uploaded_type == 'text/java') && !($uploaded_type == ....)) {
^^---boolean and
假设我们正在处理文件/类文件类型,则您的版本为:
Pretending that we're working with a file/class file type, then you version reads:
if the (file is not text/java) OR the (file is not file/class) OR the (file is not file/jar)
TRUE FALSE TRUE
TRUE or FALSE or TRUE -> TRUE
切换到AND即可
TRUE and FALSE and TRUE -> FALSE
这篇关于PHP文件类型限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!