PHP文件上传但在目录中找不到

时间:2016-04-11 19:21:15

标签: php html file-upload

我已经在stackoverflow上检查了几个方法,但没有一个能够解决这个问题。 我已经制作了一个html表单,通过 SELF_PHP 表单输入文件类型。 似乎PHP正在正确上传文件,但它上传的目录是空的。这是代码: 的 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;
    }
}
 ?> 

其他信息:.php文件目录 - /var/www/html/FileUp.php
上传目录(存储文件) - /var/www/html/uploads/ 使用(修改)的代码来自:'* schools.com'

1 个答案:

答案 0 :(得分:1)

我没有看到任何&#34;移动&#34;与您的文件。你把它上传到临时存储器中,现在你必须随时移动它。

使用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;
    }
}
?>