PHP代码
这个PHP代码有什么问题吗?
<?php
require '../db_connect.php';
if (isset($_POST['message']) && !empty($_POST['message']) or isset($_POST['img_file']) && !empty($_POST['img_file']) or isset($_POST['video_file']) && !empty($_POST['video_file']))
{
$message=$_POST['message'];
$user_id=$_POST['user_id'];
//$user_id=1;
date_default_timezone_set('Asia/Kolkata');
$date = date('Y-m-d H:i:s');
$query=mysqli_query($con,"INSERT INTO posts(user_id,post_description,is_active,created_at) values('".$user_id."','".$message."',1,'".$date."')");
if($query){
$i_post_id = mysqli_insert_id($con);
if(isset($_POST['img_file'])){
$sourcePath = $_POST['img_file'];
// create dir if not exists
if(!is_dir('../img/Post')){
mkdir('../img/Post',0777,true);
}
$s_file_name = time()."_".$_POST['img_file'];
$targetPath = "../img/Post/".$s_file_name;
if(move_uploaded_file($sourcePath,$targetPath)) {
function compress($source, $destination, $quality) {
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source);
elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source);
elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source);
imagejpeg($image, $destination, $quality);
return $destination;
}
$source_img = $targetPath;
if(!is_dir('../img/Post/compress')){
mkdir('../img/Post/compress');
}
$destination_img = '../img/Post/compress/'.$s_file_name;
$d = compress($source_img, $destination_img, 50);
$i_file_type = 1; // 1 for image.
$query=mysqli_query($con,"INSERT INTO post_files(post_id,file_name,file_type,is_active) values('".$i_post_id."','".$s_file_name."',$i_file_type,1)");
}
}
if(isset($_POST['video_file'])){
//echo "in";
$sourcePath = $_POST['video_file'];
// create dir if not exists
if(!is_dir('../img/Post')){
mkdir('../img/Post',0777,true);
}
$s_file_name = time()."_".pathinfo($_POST['video_file'], PATHINFO_FILENAME).".mp4";
$s_file_name = str_replace(' ', '_', $s_file_name);
$targetPath = "../img/Post/convert/".$s_file_name;
if(!is_dir('../img/Post/convert')){
mkdir('../img/Post/convert',0777,true);
}
$s_post_path = dirname(__FILE__).'/';
$handbrake = "HandBrake/HandBrakeCLI";
$cmd = $s_post_path.$handbrake." -i ".$sourcePath." -o ".$s_post_path.$targetPath." -e x264 -q 25 -r 15 -B 64 -X 480 -O";
passthru($cmd,$err);
$i_file_type = 2; // 2 for video.
$query=mysqli_query($con,"INSERT INTO post_files(post_id,file_name,file_type,is_active) values('".$i_post_id."','".$s_file_name."',$i_file_type,1)");
}
$response["success"] = 1;
$response["message"] = "Post successfully added.";
// echoing JSON response
echo json_encode($response);
}
else{
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
}
?>
当我将图像上传到Mysql数据库时,它没有上传并在LOG中将图像显示为字符串,例如=
img_file = /9j/
4 AAQSkZJRgABAQAAAQABAAD / 2 wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkz
ODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P / 2 wBDARESEhgVGC8aGi9jQjhCY2NjY2Nj
Y2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P / wAARCBBACSQDASIA
AhEBAxEB / 8 QAGwABAQEBAQEBAQAAAAAAAAAAAQACBwYFBAP / xABKEAACAgIBAwMDAQUEBgkDAAsA
AREhMUECA1FhBAVxBhKBkQc1cqGxEyIyNBQVFjNCUiM2U1RVYnOSwRclgiTR4fBDRPFjRSaD / 8 QA
FgEBAQEAAAAAAAAAAAAAAAAAAAEC / 8 QAFxEBAQEBAAAAAAAAAAAAAAAAAAERQf / aAAwDAQACEQMR
AD8A8KSAQ2SJQwl4kKkIDoBRP + YJzsANESJ6AGwFuydBEoUypJFoAGPIOhTJhGS4uHOSFAUkxaoy
FQpUSyKj5AGoImKrIQNUCN8nWDAUr5KARqHEgDKARtpAZRpA4yimwEzAvlsJAYrJkQgK0UmbEI0n
CJw1INMgozZaGQCFDNGfgtWBCncfzDRQ9AacSD8otWEgX4FKVbKu5UBlijX2zaKKkDM + B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FIyCICG
aAkFTEvgpqAJWLhpGZKwEieClATgCIIkKsIEAotWQgUptE92Tj4BOwNKkDKYVBIE
如何从此php文件上传图片?这个php文件有什么问题?
答案 0 :(得分:1)
我建议您在发送之前将图片转换为 Base64 字符串,然后使用 PHP <将其转换回图片 / strong>代码。您可以使用以下代码将图片转换为 Base64 :
public void toStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
}
使用您首选的库(我通常使用Volley)将encodedImage
字符串发送到您的PHP网络服务。
收到encodedImage
后,请使用以下代码将其转换回图片并将其存储在数据库的任何位置上:
$imageString = $_POST['encodedImage'];
$path = "../$name.jpg";
file_put_contents($path, base64_decode($imageString));
存储后,在 MySQL 数据库中插入图像的位置,以便您可以使用它来检索图像。
修改强>
不要让图片上传如此复杂,请尝试使用以下PHP代码:
if(isset($_POST['img_file'])){
$sourcePath = $_POST['img_file'];
// create dir if not exists
if(!is_dir('../img/Post')){
mkdir('../img/Post',0777,true);
}
$date = date_create();
$s_file_name = $user_id.date_format($date, 'U');
$targetPath = "../img/Post/$name.jpg";
file_put_contents($targetPath, base64_decode($sourcePath));
$i_file_type = 1; // 1 for image.
$query=mysqli_query($con,"INSERT INTO post_files(post_id,file_name,file_type,is_active) values('".$i_post_id."','".$s_file_name."',$i_file_type,1)");
}
在我的GitHub回购中检查相同的代码:ImagetoString
答案 1 :(得分:0)
在您的PHP文件中尝试一下,它对我有用:
$host = "localhost";
$user = "root";
$password = "";
$dbname = "image_db";
$db = mysqli_connect($host, $user, $password, $dbname);
if ($db) {
echo "connected";
}
else{
echo "not connected";
}
$image = $_FILES['image']['name'];
$image_text = mysqli_real_escape_string($db, $_POST['img_name']);
$target = "images/".basename($image);
$sql = "INSERT INTO images_tbl (image, img_name) VALUES ('$image', '$image_text')";
mysqli_query($db, $sql);
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$msg = "Image uploaded successfully";
echo $msg;
}else{
$msg = "Failed to upload image";
echo $msg;
}
$result = mysqli_query($db, "SELECT * FROM images_tbl");
?>