0

I've been working for a while on this file uploading for php, but I can't get it to work. When I click upload the 'if' statement goes to false.

If someone could point out to me where I'm going wrong, it would be great.

<?php
    $name = $_FILES["file"]["name"];


    $tmp_name = $_FILES["file"]["tmp_name"];

    if(isset($name)) {
        if(!empty($name)) {

            $location = "uploads/";
            if(move_uploaded_file($tmp_name, $location . $name)) {
                echo "Uploaded";
            } else {
                echo $location . $name;
                echo "<br>";
                echo $tmp_name;
            }

        } else {
            echo "Please choose a file!";
        }
    }
?>

<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" name="file"><br><br>
<input type="submit" value="Submit">
</form>
4
  • 1
    is it isset($name) or isset($file)..?? Commented Jan 18, 2014 at 17:40
  • @FranciscoCarvalho Actually empty($_FILES), because $_FILES is always set Commented Jan 18, 2014 at 17:43
  • try a var_dump($_FILES) to see if there are any uploads at all Commented Jan 18, 2014 at 17:45
  • @djay sorry, unfocused... Commented Jan 18, 2014 at 17:48

2 Answers 2

3

your $locatIon is probably wrong, try passing an absolute path ( the path in your example is relative to the current working directory, and it may not be writable )

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, worked perfectly. Forgot to make the uploads folder writable.
0

This works,

<html>
<head>
<title>File Upload Form</title>
</head>
<body>
This form allows you to upload a file to the server.<br>

<form action="getfile.php" method="post"><br>
Type (or select) Filename: <input type="file" name="uploadFile">
<input type="submit" value="Upload File">
</form>

</body>
</html>

getfile.php

<html>
<head>
<title>Process Uploaded File</title>
</head>
<body>
<?php

move_uploaded_file ($_FILES['uploadFile'] ['tmp_name'],
       "../uploads/{$_FILES['uploadFile'] ['name']}")

?>
</body>
</html>

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.