mario margo mario margo - 1 month ago 12
Android Question

0 kb when upload and store image to server

i have a table named images. Its have two coloums id and image(varchar300). i have this code upload.php :

<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$image = $_POST['image'];
require_once('dbConnect.php');
$sql ="SELECT id FROM images ORDER BY id ASC";
$res = mysqli_query($con,$sql);
$id = 0;
while($row = mysqli_fetch_array($res)){
$id = $row['id'];
}
$path = "uploads/$id.png";
$actualpath = "/home/kinanday/public_html/$path";
$sql = "INSERT INTO images (image) VALUES ('$actualpath')";

if(mysqli_query($con,$sql)){
file_put_contents($path,base64_decode($image));
echo "Successfully Uploaded";
}
mysqli_close($con);
}else{
echo "Error";
}


this is upload.html :

<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="image" />
<button>Upload</button>
</form>
</body>


the problem is :
1. its save on my database but the image in my file manager in Cpanel is 0kb

i using this code to integration to my android app which need an image with varchar type. So in my app doesnot show image anything because the image is 0KB.

can anybody with your advance to fix my php. every answer is very helpfull for me. thanks before .

Answer

You need to use the $_FILES superglobal, as @Fred-ii- said. but $image = $_FILES['image'] is a array, see the options for it here: http://php.net/manual/en/features.file-upload.post-method.php

Examples:

$image_name = $_FILES['image']['name'];
$image_size = $_FILES['image']['size'];
$image_tmpname = $_FILES['image']['tmp_name'];  // this is a temporary name that php uses on every upload he does.
$image_type = $_FILES['image']['type'];
$image_error = $_FILES['image']['error'];