0

Do you see what's wrong here? I can't find anything.

this prints:

echo $_FILES["new_text_file"]["name"];

and this too:

echo $_FILES["new_text_file"]["tmp_name"];

php:
"; echo $_FILES["new_text_file"]["tmp_name"]; //Uploads the text file to the server

        if(move_uploaded_file($_FILES["new_text_file"]["tmp_name"]), $_FILES["new_text_file"]["name"]) 
        {
            //header('Location: ga-dev-exercise-pavan.php');
            echo 'worked';
        }else {
            echo 'did not work';
        }
}
?>

html:

    <form enctype="multipart/form-data" action="ga-dev-exercise-pavan.php" method="POST">
        Choose a text file you want to upload and search through: 
        <input type='file' name='new_text_file'>

        <input type="hidden" name="submit_yes_file" value="true" />
        <br /><br />
        <input type="submit" value="upload">
    </form>
7
  • Move the file to another folder. You're currently leaving it in the tmp folder Commented Nov 2, 2012 at 18:26
  • Use method=GET so you can see what's going inside the parameters. It might help you find out where your error is. Commented Nov 2, 2012 at 18:27
  • Do: var_dump($_FILES); to see the complete information inside the variable and be sure that is not a typo on the key names. Commented Nov 2, 2012 at 18:28
  • @user1534664 not recommended on production and potentially hard to switch from (can be anticipated) Commented Nov 2, 2012 at 18:28
  • @JanDvorak true. I only recommend it for now because he can't find his error, and often it might turn out that the parameters are empty or stuff like that. Commented Nov 2, 2012 at 18:33

3 Answers 3

2

You aren't automatically allowed to move the uploaded file everywhere. You need to move it somewhere your PHP script has permissions to write.

So e.g.

$src = $_FILES["new_text_file"]["tmp_name"];
$dst = './tmp_dir/'.basename($_FILES["new_text_file"]["name"]);

if (move_uploaded_file($src, $dst))
{

The basename protects you against the user specifying malicious file paths (does nothing against duplicate file names), the tmp_dir is a directory you can write to.

Using $src and $dst clears a bit the code, and allowed me to see that you had an extra parenthesis in your sample...

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

1 Comment

Thanks Iserni. I figured out the issue below. It was the parentheses.
2

you should specify a target path

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}

http://www.tizag.com/phpT/fileupload.php

Edit : as mentioned in comments , parentheses are misplaced :

wrong :

if(move_uploaded_file($_FILES["new_text_file"]["tmp_name"]), $_FILES["new_text_file"]["name"])

correct

if(move_uploaded_file($_FILES["new_text_file"]["tmp_name"], $_FILES["new_text_file"]["name"]))

1 Comment

thanks RezaSh. The issue was the parentheses. I submitted an answer below. I needed the file to be uploaded into the current directory, so that's why I didn't specify a sub directory.
1

The parentheses were off. It should be like this:

if(move_uploaded_file($_FILES["new_text_file"]["tmp_name"], $_FILES["new_text_file"]["name"])) 

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.