Pavan Katepalli
Pavan Katepalli

Reputation: 2464

why doesn't my file upload script work?

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>

Upvotes: 0

Views: 941

Answers (3)

Shahrokhian
Shahrokhian

Reputation: 1114

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"]))

Upvotes: 2

Pavan Katepalli
Pavan Katepalli

Reputation: 2464

The parentheses were off. It should be like this:

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

Upvotes: 1

LSerni
LSerni

Reputation: 57388

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

Upvotes: 2

Related Questions