如何使用PHP从android WebView上传图像到MySQL数据库?我已经实现了一个文件选择器,用于从设备中选择图像。
您可以查看这个演示示例HTML
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<button type="submit" name="upload">upload</button>
</form>
</body>
</html>
PHP代码:
<?php
include_once 'database.php';
if(isset($_POST['upload']))
{
$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$folder="upload/";
/* new file size in KB */
$new_size = $file_size/1024;
/* new file size in KB */
/* make file name in lower case */
$new_file_name = strtolower($file);
/* make file name in lower case */
$final_file=str_replace(' ','-',$new_file_name);
if(move_uploaded_file($file_loc,$folder.$final_file))
{
$sql="INSERT INTO image(file,type,size) VALUES('$final_file','$file_type','$new_size')";
mysqli_query($conn,$sql);
echo "File sucessfully upload";
}
else
{
echo "Error.Please try again";
}
}
?>
感谢