$allowedMimeTypes = array(
'image/jpeg',
'image/png'
);
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ( 500000 < $_FILES["file"]["size"] )
{
echo"Please provide a smaller file [E/1].";
}
if ( in_array( $_FILES["file"]["type"], $allowedMimeTypes ) )
{
$file = $_FILES["file"]["name"];
$filePath = "../upload/" . $file;
move_uploaded_file($_FILES["file"]["tmp_name"],$filePath);
}
else if(!is_uploaded_file($_FILES["file"]["tmp_name"]))
{
$file='';
$filePath='';
}
if(! get_magic_quotes_gpc() )
{
$name = addslashes ($_POST['name']);
$path = $filePath='';
}
else
{
$name = $_POST['name'];
$path = $filePath='';
}
$sql = "INSERT INTO slides ".
"(name,path) ".
"VALUES ".
"('$name','$path')";
mysql_select_db('emtas');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
}
else
{
?>
我尝试从表单添加图片。它上传图像但我无法写入数据库的路径。它还从表单中写入图像的名称。为什么我无法获取路径并写入数据库?
答案 0 :(得分:1)
在执行查询之前,您始终将$path
和$filePath
都设置为空字符串。将您的代码更改为以下内容:
$name = $_POST['name'];
if(!get_magic_quotes_gpc()) {
$name = addslashes ($name);
}
// execute query
$sql = "INSERT INTO slides ".
"(name,path) ".
"VALUES ".
"('$name','$filePath')";
请注意,如果没有上传文件,则不应运行查询。
由于不推荐使用mysql
扩展程序,因此您应该使用mysqli
或pdo
。