0

I need to create a simple web page via which you can upload a image file that goes to a directory on the server that is previously created.

Here's the code for the index.php file:

<!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>

Here's the code for the 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 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;
    }
}
?>

The code is hosted on a free hosting server (not a paid one) and I created a dir called 'uploads' in the same subdirectory where both scripts are located. In the php settings uploading seems to be on. The index.php displays fine, I select an image and click upload, it loads for a second and then displays 'File is an image - image/gif.' However when I got to the upload dir, there isn't a single file there.

What could be the issue? Thank you in advance.

3
  • where is upload code? you are just checking file extension! Commented Feb 7, 2017 at 22:17
  • At what point are you moving/copying the files? this should be useful php.net/manual/en/features.file-upload.post-method.php Commented Feb 7, 2017 at 22:17
  • @Arsalan Mithani he is uploading the file when he submits it, but because he didn't use the move_uploaded_file function the temp file is being deleted from the server since it expires shortly after the page submits. Commented Feb 7, 2017 at 22:21

1 Answer 1

4

You aren't moving the file to your server after uploading the temp file so it gets deleted

if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

You may also want to use some file extension checking with an array of allowed filetypes to prevent people from uploading things they may not want on your server. For example. a php script that wipes your entire site or directory. Just a heads up :)
The tutorial I'm following also covered that, so no worries :) Thanks for the warning!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.