gpt4 book ai didi

php - 无法更改上传文件的文件夹位置

转载 作者:行者123 更新时间:2023-11-29 17:18:06 25 4
gpt4 key购买 nike

我想将当前位于 php/images/的 $target 目录更改为 uploads/images/,但由于某种原因,它无法工作,并且没有图像上传到该文件夹​​,但仅限于 php/images,我不知道是什么原因造成的。

我上传的所有图像都会上传到 phpmyadmin 并加载到我的 images.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>

images.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>";
}

最佳答案

你正在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";
}
}

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

关于php - 无法更改上传文件的文件夹位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51436078/

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