我有一个名为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'];更多推荐
发布评论