1

So I have a file upload portion on my website where the user can upload any doc or docx folder. Heres my html code:

<form action="upload_file.php" method="post" enctype="multipart/form-data">
    Select a file: <input type="file" name="upload">
<input type="submit">

And here's the code for upload_file.php:

<?php
    session_start();
    $allowedExts = array("doc", "docx");
    $extension = end(explode(".", $_FILES["upload"]["name"]));

    if (($_FILES["upload"]["size"] < 200000)
    && in_array($extension, $allowedExts)) {
        if ($_FILES["upload"]["error"] > 0)
        {
            echo "Return Code: " . $_FILES["upload"]["error"] . "<br />";
        }
        else
        {
            echo "Upload: " . $_FILES["upload"]["name"] . "<br />";
            echo "Type: " . $_FILES["upload"]["type"] . "<br />";
            echo "Size: " . ($_FILES["upload"]["size"] / 1024) . " Kb<br />";
            echo "Temp file: " . $_FILES["upload"]["tmp_name"] . "<br />";

            if (file_exists("Proposals/".$_SESSION["FirstName"] ."/" . $_FILES["upload"]["name"]))
            {
                echo $_FILES["upload"]["name"] . " already exists. ";
            }
            else
            {
                move_uploaded_file($_FILES["upload"]["tmp_name"],
                "Proposals/". $_SESSION["FirstName"] ."/". $_FILES["upload"]["name"]);
                echo "Stored in: " . "Proposals/". $_SESSION["FirstName"] ."/". $_FILES["upload"]["name"];
            }
        }
    } else {
        echo "Invalid file";
    }
?>

But this won't upload anything even though it replied back with with a successful message. I'm thinking it's because it won't let me create a directory. Is there anything wrong with the above code or do I have to add some more code to make it create a directory. Here my folder structure if it helps at all:

Folder

1

3 Answers 3

14

You have to create the directory you're trying to move the file to, it won't automatically get created by move_uploaded_file.

Use mkdir(), http://php.net/mkdir, to create the directory and then move the file.

Here's an alternative ending to your script, which should do

// Create directory if it does not exist
if(!is_dir("Proposals/". $_SESSION["FirstName"] ."/")) {
    mkdir("Proposals/". $_SESSION["FirstName"] ."/");
}

// Move the uploaded file
move_uploaded_file($_FILES["upload"]["tmp_name"], "Proposals/". $_SESSION["FirstName"] ."/". $_FILES["upload"]["name"]);

// Output location
echo "Stored in: " . "Proposals/". $_SESSION["FirstName"] ."/". $_FILES["upload"]["name"];
Sign up to request clarification or add additional context in comments.

1 Comment

This code is a great starting point (error handling being left to the reader, of course).The only enhancement I would suggest would be to use the constant DIRECTORY_SEPARATOR instead of hard-coding the "/"
2

You need to check if the directory exists, and if not, create it.

if (!file_exists("Proposals/". $_SESSION["FirstName"])) {
      mkdir("Proposals/". $_SESSION["FirstName"]);
}

Comments

0

You are uploading file to directory that does not exist so you need to create it first, your upload_file.php should be like

<?php
session_start();
$allowedExts = array("doc", "docx");
$extension = end(explode(".", $_FILES["upload"]["name"]));

if (($_FILES["upload"]["size"] < 200000)
&& in_array($extension, $allowedExts)) {
    if ($_FILES["upload"]["error"] > 0)
    {
        echo "Return Code: " . $_FILES["upload"]["error"] . "<br />";
    }
    else
    {
        echo "Upload: " . $_FILES["upload"]["name"] . "<br />";
        echo "Type: " . $_FILES["upload"]["type"] . "<br />";
        echo "Size: " . ($_FILES["upload"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["upload"]["tmp_name"] . "<br />";

        if (file_exists("Proposals/".$_SESSION["FirstName"] ."/" . $_FILES["upload"]["name"]))
        {
            echo $_FILES["upload"]["name"] . " already exists. ";
        }
        else
        {
            // Check if directory exists if not create it 
            if(!is_dir("Proposals/". $_SESSION["FirstName"] ."/")) {
               mkdir("Proposals/". $_SESSION["FirstName"] ."/");
             }
             move_uploaded_file($_FILES["upload"]["tmp_name"],
            "Proposals/". $_SESSION["FirstName"] ."/". $_FILES["upload"]["name"]);
            echo "Stored in: " . "Proposals/". $_SESSION["FirstName"] ."/". $_FILES["upload"]["name"];
        }
    }
} else {
    echo "Invalid file";
}
?>

Comments

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.