TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

PHP coding question

3 点作者 amrithk大约 17 年前
To all PHP hackers out there, I have a basic PHP question. The application I am working on currently allows a user to upload a jpeg or gif image only. My code for enforcing this rule is as follows:<p>if($type!="image/gif" || $type!="image/jpeg"){ $error = "Your image needs to have a gif or jpeg format"; }<p>$type in this case is obtained from $_FILES['file']['type']<p>However, this does not seem to work. Non-JPEG images get rejected properly. However even JPEG images are rejected. Any ideas on how I can check the type of the image.

4 条评论

pmorici大约 17 年前
Ummm, that statement is always going to be true. This isn't a PHP question this is a basic logic question.<p>You really want...<p>if (!(type == gif or type == jpeg)) { error }<p>or change your 'or' to an 'and' ie:<p>if (type!=gif and type!=jpeg) { error }<p><a href="http://en.wikipedia.org/wiki/De_Morgan's_laws" rel="nofollow">http://en.wikipedia.org/wiki/De_Morgan's_laws</a>
评论 #167047 未加载
评论 #167020 未加载
noodle大约 17 年前
consider using the getimagesize function. <a href="http://us.php.net/getimagesize" rel="nofollow">http://us.php.net/getimagesize</a> the comments have some useful info on how to get the image type.<p>iirc, the problem using the file type string is that its not consistent.
symbiotic大约 17 年前
An easy way to debug is to just echo $type for a file that you want to allow and see what it is :)
prime2大约 17 年前
try: ($type!="image/gif" || $type!="image/jpeg" || $type!="image/jpg" || $type!="image/prjpeg")