简体   繁体   English

使用php上传的图片无法正常使用

[英]Uploaded image using php doesn't fully work

The problem is this one: 问题是这个:

I have a code that uploads a picture to phpmyadmin, the code... doesn't fully work for some reason, it doesn't upload the image fully and the image ends up having a very small size, and it won't show up either. 我有一个将图片上传到phpmyadmin的代码,由于某些原因该代码无法完全正常工作,它没有完全上传图像,并且图像最终尺寸很小,并且不会显示要么。 If I upload them directly to the database, they can be shown, but the ones uploaded using the code below have that problem. 如果我将它们直接上传到数据库,则可以显示它们,但是使用下面的代码上传的文件则存在该问题。

This is the code I have: 这是我的代码:

Code for the button: 按钮的代码:

   <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>

Code for the uploading: (iniciar.php) 上传代码: (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';  
}
?>

This is the structure of the table: The table I have 这是表的结构: 我有的表

you're assigning like input data instead of 您正在分配类似输入数据,而不是

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

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

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

you have to assign image like this 您必须分配这样的图像

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

now evidence hold address of image so dir. 现在证据保存图像的地址如此直接。 of image is now on evidence too.now it will be uploaded to data base you aren't passing data. 图像的证据现在也有证据。现在它将被上传到您不传递数据的数据库。 that's the mistake. 那是错误。 any questions comment .if it solves your problem mark it as solved :) 任何问题评论。如果它解决了您的问题,则将其标记为已解决:)

for reference ,this is the php script used to upload profile picture, 供参考,这是用于上传个人资料图片的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";
         } 
    }
?>

make changes according to your data base & operations. 根据您的数据库和操作进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM