Simple PHP/HTML upload page - no files are saving

為{幸葍}努か 提交于 2019-12-04 06:43:25

问题


I'm new to HTML/PHP and I'm trying to create a simple php file upload page. I have this as my HTML

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

I have this as my 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;
    }
}
?>

I've uploaded both of these to the correct folder in my host (000webhost), yet when I check in my /uploads folder nothing is there. I've granted all my files read write and execute permissions to try and debug it - I'll learn about security later.

Any help would be greatly appreciated!


回答1:


You must add this snippet of code:

move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);

after that:

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



回答2:


You need to move the uploaded file from the temp directory it was uploaded into, into your target directory. See the PHP docs of move_uploaded_files




回答3:


With the PHP function move_uploaded_file(string $filename, string $destination) you can move the file to your desired path.



来源:https://stackoverflow.com/questions/28453850/simple-php-html-upload-page-no-files-are-saving

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!