繁体   English   中英

PHP图片上传可以上传到localhost文件夹和mysql数据库,但图片不加载

[英]PHP image upload can upload on localhost file folder and mysql database but the image not loading

在此处输入图片说明

文件路径:C:\\TEMP\\bcproject\\images

我已经测试了下面的代码。 当我尝试在我的目录路径 C:\\TEMP\\bcproject\\images 中上传图像时,该图像成功插入到数据库中。 但我面临的问题是我无法在我的文件夹中查看图像......上面我确实提供了问题的图片。

现在寻求帮助

我的代码: testaddimage.php

<?php
include("connection.php");

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

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

    // Select file type
    $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

    // Valid file extensions
    $extensions_arr = array("jpg","jpeg","png","gif");

    // Check extension
    if( in_array($imageFileType,$extensions_arr) ){ // this can but no image appear

        // Insert record
        $query = "insert into test(imagename) values('".$name."')";
        mysqli_query($conn,$query);

        // Upload file
        move_uploaded_file($_FILES['file']['tmp_name'],$target_dir);

    }

    /*$name = $_FILES['file']['name'];
    $tmpname = $_FILES['file']['tmp_name'];
    $size = $_FILES['file']['size'];
    $error = $_FILES['file']['error'];
    $type = $_FILES['file']['type'];
    echo $name. " ". $tmpname. " ".$size . " ". $error . " ". $type;
    $fileext = (explode('.',$name));
    $fileactualext = strtolower(end($fileext));
    $allowed = array("jpg","jpeg","png","gif");

    if( in_array($fileactualext,$allowed) ) {
     if ($error === 0)
     {
         if($size < 1000000){
$filenamenew = uniqid('',true). "." . $fileactualext;
$filedir = 'images/'. $filenamenew;
move_uploaded_file( $tmpname,$filedir );
header("location:testaddimage.php");
         } else {
             echo "File to big";
         }
         echo "There was an error.";
     }
    }else
    {
        echo  "cannot uploade";
    }
*/


}
?>

<form method="post" action="testaddimage.php" enctype='multipart/form-data'>
    <input type='file' name='file' />
    <input type='submit' value='Save name' name='but_upload'>
</form>

连接.php

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$database = "playgadget";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

首先, move_uploaded_file 返回 true 还是 false ? ( https://www.php.net/manual/fr/function.move-uploaded-file.php )

其次,您是否检查了目标目录权限?

第三,当您在 Windows 上时,请确保斜杠是正确的(使用 DIRECTORY_SEPARATOR const 而不是“/”或“\\”)。 我不记得 php 是否处理过这个问题。

暂无
暂无

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

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