上传并将图像存储到服务器时为0 kb(0 kb when upload and store image to server)

编程入门 行业动态 更新时间:2024-10-19 23:49:57
上传并将图像存储到服务器时为0 kb(0 kb when upload and store image to server)

我有一个名为images的表。 它有两列id和image (varchar300)。 我有这个代码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:

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

问题是:它保存到我的数据库,但在我的文件管理器在Cpanel中的图像是0kb

我正在使用此代码来整合到我的Android应用程序,它需要一个varchar类型的图像。 所以在我的应用程序不显示图像任何东西,因为图像是0KB。

任何人都可以提前修复我的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"; }

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: it saves into my database but the image in my file manager in Cpanel is 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.

Can anybody with your advance to fix my php. every answer is very helpful for me. Thanks in advance.

最满意答案

您需要使用$ _FILES superglobal,如@ Fred-ii-所述。 但$image = $_FILES['image']是一个数组,请参阅此处的选项: 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'];

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'];

更多推荐

本文发布于:2023-04-28 02:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329770.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并将   图像   上传   服务器   kb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!