无法更改上载文件的文件夹位置

vojdkbi0  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(420)

我想改变我的$target目录,目前在php/images/上传/images/,但由于某种原因,它不会工作,因为某些原因,没有图片上传到该文件夹,但仅限于php/images,我不知道是什么原因导致这一点。
我上传的所有图片都会上传到phpmyadmin,并加载到myimages.php页面,因此$target的所有工作都不起作用。

<?php

$db = mysqli_connect("localhost", "root", "", "media");

$msg = "";

if (isset($_POST['upload'])) {

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

  $target = "php/images/".basename($image);

  $sql = "INSERT INTO images (image_name) "
          . "VALUES ('$image')";

  mysqli_query($db, $sql);

  if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
      $msg = "Image uploaded successfully";
  }else{
      $msg = "Failed to upload image";
  }
}
?>

<form id='imageForm' method="POST" action="" enctype="multipart/form-data">
      <input type="hidden" name="size" value="1000000">
   <div>
       <input class="file-upload" type="file" name='image' accept='image/*' />
   </div>
   <div class="upload-button">
       <button class="" name='upload'>UPLOAD</button>
   </div>
</form>

图片.php

$db = mysqli_connect("localhost", "root", "", "media");
$uploaded = mysqli_query($db, "SELECT * FROM images");

while ($row = mysqli_fetch_array($uploaded)) {
  echo "<ul class='img_container'>";
      echo "<li><img class='img_box' src='php/images/".$row['image_name']."' ></li>";
  echo "</ul>";
}
svgewumm

svgewumm1#

在mysql数据库中插入blob时,可以使用blob(base64),但如果加载的图像太多,则会减慢网站的运行速度,请看这里

if (isset($_POST['upload'])) {
    if($_FILES['image']['error'] > 0) { echo 'Error during upload, try again'; }

        //We won't use $_FILES['file']['type'] to check the file extension for security purpose

        //Set up valid image extensions
        $extsAllowed = array( 'jpg', 'jpeg', 'png' );

        //Extract extention from uploaded file
            //substr return ".jpg"
            //Strrchr return "jpg"

        $extUpload = strtolower( substr( strrchr($_FILES['image']['name'], '.') ,1) ) ;
        //Check if the uploaded file extension is allowed

        if (in_array($extUpload, $extsAllowed) ) { 

        //Upload the file on the server

      $name = "{$_FILES['image']['name']}";
      $imgName = rand(5, 99999);
      $image = 'php/images/'.$imgName.'.jpg';
      $result = move_uploaded_file($_FILES['image']['tmp_name'], $image);

      }
        if(result){
        $sql = "INSERT INTO images (image_name) "
              . "VALUES ('$image')";
        mysqli_query($db, $sql);
        $msg = "Image uploaded successfully";
        }else{
         $msg = "Failed to upload image";
        }
    }

还要检查您是否有权在该目录上写入。

相关问题