我想使用php将图像上传到mysql服务器。 我创建了html和sql连接,但图片上传显示错误。 我无法上传图片,它显示有效图片的错误,即你必须上传jpeg,bmp,gif;并在目录中读/写。
any1可以帮助我解决这个问题 php文件是
<?php
//Start session
session_start();
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
// Check to see if the type of file uploaded is a valid image type
function valid($file)
{
// This is an array that holds all the valid image MIME types
$valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif");
//echo $file['type'];
if (in_array($file['type'], $valid_types))
return 1;
return 0;
}
// Build our target path full string. This is where the file will be moved do
// i.e. images/picture.jpg
$TARGET_PATH = "image/";
$TARGET_PATH = $TARGET_PATH . basename( $_FILES['image']['name']);
$pimage = $_FILES['image']['name'];
// Check to make sure that our file is actually an image
// You check the file type instead of the extension because the extension can easily be faked
if (!valid($pimage))
{
$_SESSION['ERRMSG_ARR'] = array('You must upload a jpeg, gif, or bmp');
header("Location: admin.php");
exit;
}
// Here we check to see if a file with that name already exists
// You could get past filename problems by appending a timestamp to the filename and then continuing
if (file_exists($TARGET_PATH))
{
$_SESSION['ERRMSG_ARR'] = array('A file with that name already exists');
header("Location: admin.php");
exit;
}
// Lets attempt to move the file from its temporary directory to its new home
if (move_uploaded_file($_FILES['image']['tmp_name'], $TARGET_PATH))
{
// NOTE: This is where a lot of people make mistakes.
// We are *not* putting the image into the database; we are putting a reference to the file's location on the server
$sql = "insert into people (p_category, p_name, p_quantity, p_desc, p_image) values ('$pcategory', '$pname','$pquantity','pdesc', '" . $pimage['name'] . "')";
$result = mysql_query($sql);
//Check whether the query was successful or not
if($result) {
$_SESSION['ERRMSG_ARR'] = array('Product added');;
$_SESSION['MSG_FLAG'] = 0;
session_write_close();
header("location: admin.php");
exit();
}else {
die("Query failed: ".mysql_error());
}
}
else
{
// A common cause of file moving failures is because of bad permissions on the directory attempting to be written to
// Make sure you chmod the directory to be writeable
$_SESSION['ERRMSG_ARR'] = array('Could not upload file. Check read/write persmissions on the directory');
header("Location: admin.php");
exit;
}
?>
答案 0 :(得分:1)
我认为
$pimage = $_FILES['image']['name'];
应该是
$pimage = $_FILES['image'];
您可能错过了这个,因为您的代码非常不一致 - 有时您使用$pimage
,而在其他地方您直接引用$_FILES
数组。如果文件字段的名称发生更改,这将使维护变得更加困难。您还可以键入提示valid()
函数,以便在$file
不是数组时让PHP抱怨:
function valid(array $file) { ... }
您设置了什么级别的错误报告?它会突出显示错误,例如尝试访问未定义的数组键。
答案 1 :(得分:0)
看到您在第if (!valid($pimage))
行传递图片类型
但是在valid()函数中,您再次尝试获取图像类型$file['type']
。
乔治所说的也应该有用,但由于你为图像类型$ptype
和名称$pimage
制作变量,你可以自己使用它们。
因此,更改应$file['type']
变为$file
和$file['type']
&amp;在插入查询$pimage['name']
中变为$pimage
我确定这解决了,巴华;)