0

This is my php code

<?php
    if ($_FILES["file"]["error"] > 0)
    {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
    else
    {
        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

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

Simple file upload according to W3C, however i added the mkdir to create a directory based on the title the user input.

The folder creates fine but the file won't move into it, not sure if this is something simple i thought but use the concatenate '.' i could just define the file location like i had with mkdir

The HTML is simple just input type = file and and input text type for the title

2
  • 1
    I assume where $title is defined you're parsing it to remove characters you're not allowed in a folder name? Commented Aug 22, 2013 at 15:10
  • thanks for the heads up, i didn't even think, i was just pulling out the users input and storing it in a variable, you have just saved me future headache, thanks again Commented Aug 22, 2013 at 15:17

2 Answers 2

3

You need a / after $title to use the new folder.

move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$title."/".$_FILES["file"]["name"]);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks that did it "upload/".$title.'/' .
Note that your if file_exists statement needs the $title variable, too.
0

You're missing a slash in your path, as pointed out by @Grim above. I'd like to explain why:

echo "Stored in: " ."upload/".$title . $_FILES["file"]["name"];
                                  --^

Changing it to the following should fix the problem:

echo "Stored in: " ."upload/".$title . "/" . $_FILES["file"]["name"];

Your path is incorrect and move_uploaded_file will be unable to find the file in the specified path and your code won't work as you expect it to.

Without the slash, this is what your function would actually look like (assuming you define $title variable somewhere in between):

upload/fooFilename

You actually need:

upload/foo/Filename

Hope this helps!

1 Comment

Changing the echo isn't going to fix the problem (although it will make that line more accurate). I'm constantly changing the wrong thing while displaying a query variable or something to the screen to debug it - drives me nuts!

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.