当前位置: 动力学知识库 > 问答 > 编程问答 >

html - PHP file uploading but can't find in directory

问题描述:

I've checked several methods on stackoverflow but none is able to solve this.

I've made an html form to input file type through SELF_PHP form.

Seems like PHP is uploading the file correctly but the directory to which it's being uploaded is empty . Here's the code:

HTML

<form method="post" id="fileUpForm" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data" >

Select image to upload:

<input type="file" name="fileToUpload" id="fileToUpload">

<input type="submit" value="Upload" name="submit">

</form>

PHP

<?php

$target_dir = "uploads/";

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

$uploadOk = 1;

$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if image file is a actual image or fake image

if(isset($_POST["submit"])) {

$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);

if($check !== false) {

echo "File is an image - " . $check["mime"] . ".";

$uploadOk = 1;

} else {

echo "File is not an image.";

$uploadOk = 0;

}

}

?>

Additional Info: .php file directory - /var/www/html/FileUp.php

uploads directory(to store files) - /var/www/html/uploads/

Code used(modified) from : '*schools.com'

网友答案:

I don't see any "move" with your file. You have it uploaded in temporary memory, now you have to move it whenever you want.

Use PHP: move_uploaded_file

<?php 
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        // if check is ok, move the file to the target directory
        move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_dir)
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
?> 
分享给朋友:
您可能感兴趣的文章:
随机阅读: