gpt4 book ai didi

php - 使用 php 上传的图像不能完全工作

转载 作者:行者123 更新时间:2023-11-29 18:08:37 24 4
gpt4 key购买 nike

问题是这个:

我有一个将图片上传到 phpmyadmin 的代码,该代码...由于某种原因无法完全工作,它没有完全上传图像,并且图像最终的尺寸非常小,并且它赢了也不出现。如果我直接将它们上传到数据库,它们可以显示,但是使用下面的代码上传的有这个问题。

这是我的代码:

按钮代码:

   <div id="content">
<form method="POST" action =""/>
<table>
<tr>
<td>
Upload a picture
</td>
<td>
<input type="file" name="image"/>
</td>
</tr>
</table>
<input type="submit" name="submit" value="Registrarme"/> <input type="reset"/>

</form>
<?php
if(isset($_POST['submit'])){
require("iniciar.php");
}
?>
</div>

上传代码: (iniciar.php)

<?php
$image = $_POST['image'];
$reqlen = strlen($image);
if($reqlen > 0){
$conn = mysqli_connect('localhost','root','','image');
$sql = "INSERT INTO `images` VALUES ('', '$image', '')";
mysqli_query($conn, $sql);
echo 'Upload successful';
}else{
echo 'Please insert an image';
}
?>

这是表的结构: The table I have

最佳答案

您正在分配类似的输入数据,而不是

            $_FILES["image"]["name"];

//remove thisline
$image=$_POST['evidence'];
// you have to use it like this

$image= $_FILES["image"]["name"];

你必须像这样分配图像

$target_file =  $target_dir . '/' . basename($_FILES["image"]["name"]);
$image=$target_file;

现在证据保存了图像目录的地址。图像现在也已成为证据。现在它将上传到数据库,您不会传递数据。这就是错误。有任何问题评论。如果它解决了您的问题,请将其标记为已解决:)

供引用,这是用于上传个人资料图片的php脚本,

  <?php
if(isset($_POST["submit"]))
{
$target_dir = "profilepic/";
$targetfile=$target_dir.basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($targetfile,PATHINFO_EXTENSION);

if($imageFileType != "jpg" &&$imageFileType != "JPG" && $imageFileType != "png" && $imageFileType != "PNG" && $imageFileType != "jpeg" && $imageFileType != "JPEG" && $imageFileType != "gif" && $imageFileType != "GIF" )
{
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;

}
if ($uploadOk == 0)
{
echo "<small>Sorry, your file was not uploaded.</small>";
}
else
{
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetfile))
{
echo "<small>upload successful</small>";
$imgdir="UPDATE user SET profilepic='$targetfile' WHERE username='$uname';";
$con->query($imgdir);
header("location:profile.php");

}
else
echo "not uploaded";
}
}
?>

根据您的数据库和操作进行更改。

关于php - 使用 php 上传的图像不能完全工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47599578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com