简体   繁体   中英

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

enter image description here

filepath:C:\\TEMP\\bcproject\\images

I have tested the following code below. When I tried to upload an image in my directory path C:\\TEMP\\bcproject\\images, the image successfully insert into the database. But the problem I faced is I couldn't view the image at my folder... Above I did provide the picture of the problem.

Seeking for help now

my code: 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>

connection.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);
}
?>

First, does move_uploaded_file return true or false ? ( https://www.php.net/manual/fr/function.move-uploaded-file.php )

Second, have you checked the target directory permissions ?

Third, as you are on Windows, be sure that slashes are in good (use DIRECTORY_SEPARATOR const instead of "/" or "\\"). I don't remember if php take care of this or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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