php图像上传问题,视频扩展不返回错误
php代码:php图像上传问题,视频扩展不返回错误,php,html,image,upload,Php,Html,Image,Upload,php代码: <?php if (isset($_FILES['image'])) { $errors = array(); $allowed_ext = array ('jpg','gif','png','jpeg'); $file_name = $_FILES ['image']['name']; $file_ext = explode('.', $file_name); $file_ext = end ($file_ext); $f
<?php
if (isset($_FILES['image'])) {
$errors = array();
$allowed_ext = array ('jpg','gif','png','jpeg');
$file_name = $_FILES ['image']['name'];
$file_ext = explode('.', $file_name);
$file_ext = end ($file_ext);
$file_ext = strtolower($file_ext);
echo $file_ext;
$file_size = $_FILES ['image']['size'];
$file_tmp = $_FILES ['image']['tmp_name'];
if (!in_array($file_ext, $allowed_ext)) {
$errors[] = "File extension is not allowed.";
}
if ($file_size > 2097152 ) {
$errors[] = "File size is greater than 2 megabytes.";
}
if (count($errors) > 0) {
foreach ($errors AS $error) {
echo $error . "<br />";
}
} else {
$file_name = md5(microtime()).".".$file_ext;
if (move_uploaded_file($file_tmp, $avatar_folder.$file_name)) {
}
}
}
?>
html代码:
<div id="form_part">
<form action="" method="POST" enctype="multipart/form-data">
<p>
<input type="FILE" name="image">
<input type="submit" value="upload">
</p>
</form>
</div> <!-- end form_part -->
现在,,问题是,正如您所看到的,我在数组中指定图像文件类型,以允许上载特定扩展名并防止其他扩展名,如果此数组中不存在文件类型,则返回错误消息,但当我使用视频文件对此进行测试时,文件未上载到上载目录,但错误消息不会显示,尽管它是当我上传txt、rar、pdf等时显示,这是为什么?我如何修复它?编写test.php并放置以下代码:
phpinfo();
此代码将打印服务器配置页。查找“上载\u最大\u文件大小”值。它告诉你可以上传多大的文件
如果此值太小,则大多数托管公司允许使用以下行在.htaccess文件中更改此值:
php_value upload_max_filesize 50M
顺便说一下,您的代码不会检查错误。在顶部,您需要添加行:
if ($_FILES['image']['error'] == UPLOAD_ERR_OK) {
//do your code
} else {
echo $_FILES['audio']['error'];
//Error messages explained here: http://php.net/manual/en/features.file-upload.errors.php
}
试试这个代码
$allowed_exts = array('.png','.gif','.jpg','.jpeg') ; // Our Allowed Extentions
$errors = array() ; // Our Error Array
$file_name = $_FILES['image']['name'] ;
$file_size = $_FILES['image']['size'] ;
$file_ext = strtolower(strrchr($file_name,'.')); // File Extention
if(!in_array($file_ext,$allowed_exts))
{
array_push($errors,'NoT Allowed');
}
elseif($file_size > 2097152)
{
array_push($errors,'More Than 2 mb');
}
if(count($errors) > 0)
{
var_dump($errors) ;
}
else
{
$new_name = substr(md5(rand().time()),0,8).$file_ext ;
$upload = move_uploaded_file($_FILES['image']['tmp_name'],$avatar_folder.$new_name ) ;
if($upload)
{
echo 'Image Uploaded ';
}
else
{
var_dump($errors) ;
}
}
flv、avi、mp4、vob、ts文件,这是我测试的文件。可能是PHP上传错误。尝试回显$\u文件['image']['error']。下面是可能的错误列表[link],我正在检查文件大小和文件类型错误,上面这一行是为了什么?那么错误代码是什么?这条线的结果是什么?echo$_文件['audio']['error'];没有工作,我只是更换了$file_ext部分,但它不工作,当我尝试上载任何图像文件时,我得到的文件扩展名是不允许的!哦,很抱歉,它正在工作,我忘了在数组元素中添加“.”,但同样的问题仍然存在!例如,当我尝试上载.avi文件时,它没有上载,但没有显示错误消息!