上传的照片不会进入上传/文件夹

时间:2015-03-14 05:22:38

标签: php file-upload

我正在使用w3Schools tutorial来创建我的 upload.php 文件并正确完成所有代码 - 虽然根据教程看起来是正确的我上传的图片到网站不会“存入”它设置的文件夹。

我的 upload.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 an actual image or a 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;
    }
}

// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOK = 0;
}

// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOK = 0;
}

// Allow certain file formats
if($imageFileType != "png") {
    echo "Sorry, only PNG files are allowed.";
    $uploadOK = 0;
}

?>

这是我尝试上传属于'要求'的照片时获得的屏幕: Result of upload

“uploads /”文件夹与upload.php位于同一目录中,但正如我所说,文件仍然无法进入任何地方。提前感谢您的帮助。

祝你好运, CODI

2 个答案:

答案 0 :(得分:0)

您还没有使用move_uploaded_file()函数。没有该功能,文件将不会移动到文件夹。

答案 1 :(得分:0)

您需要移动$_POST["submit"]块内的所有内容,然后在末尾使用move_uploaded_file()

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;
    }

    // Check if file already exists
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOK = 0;
    }

    // Check file size
    if ($_FILES["fileToUpload"]["size"] > 500000) {
        echo "Sorry, your file is too large.";
        $uploadOK = 0;
    }

    // Allow certain file formats
    if($imageFileType != "png") {
        echo "Sorry, only PNG files are allowed.";
        $uploadOK = 0;
    }

    if($uploadOK)
    {
        chmod($target_dir, 0750); // just to make sure the target folder is writable
        if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);)
        {
            echo 'File uploaded succesfully';
        }
        else
        {
            echo 'There was a problem uploading the file';
        }
    }
    else
    {
        echo 'Upload cannot take place as the validation failed';
    }
}