将图像上传并存储到服务器时为0 kb [英] 0 kb when upload and store image to server
问题描述
我有一个名为 images
的表.它具有两列 id
和 image
(varchar300).我有这段代码upload.php:
I have a table named images
. It has two columns 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";
}
这是upload.html:
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>
问题是:它保存到我的数据库中,但是我在Cpanel中的文件管理器中的映像为0kb
The problem is: it saves into my database but the image in my file manager in Cpanel is 0kb
我正在使用此代码将其集成到需要varchar类型图像的android应用中.因此,在我的应用中,由于图片大小为0KB,因此不会显示任何图片.
I am using this code to integration to my android app which need an image with varchar type. So in my app does not show image anything because the image is 0KB.
任何人都可以提前解决我的php.每个答案对我都很有帮助.预先感谢.
Can anybody with your advance to fix my php. every answer is very helpful for me. Thanks in advance.
推荐答案
您需要使用$ _FILES超全局变量,如@ Fred-ii-所述.但是 $ image = $ _FILES ['image']
是一个数组,请在此处查看其选项:
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
示例:
$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'];
这篇关于将图像上传并存储到服务器时为0 kb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!