SmashCode
SmashCode

Reputation: 4257

move_uploaded_file failing with no error

This question has been asked similarly a few times, but those answer's didn't apply to the problem I'm have. I've checked them all.

Basically, the function move_uploaded_file is returning false every time, even though I feel like I have all my ducks in a row. There is no error, it just returns false.

I have checked the file that is being uploaded, it has no errors.

It may be a permissions problem, I tried to change the directory I'm uploading the images to using chmod(dir, 0777). If it were a permissions problem, I'm not sure if this would've fixed it. Edit - Checked iswritable(dir) of the directory and it says its writable.

I do have enctype="multipart/form-data" attribute set in my form.

This is my code:

    function uniqueName()
    {
    $target = dirname(__FILE__) . '/TestProject/';
    $uid = uniqid();
    $ext = pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION);

    $_FILES['photo']['name'] = $uid . "." . $ext;

    if(move_uploaded_file($_FILES['photo']['name'], $target . $_FILES['photo']['name']))
        echo("upload succeeded");
    else {
        echo("upload failed");
    }

    return $target . $_FILES['photo']['name'];
    } 

Am I missing anything? Any help would be appreciated.

Upvotes: 1

Views: 3734

Answers (2)

SmashCode
SmashCode

Reputation: 4257

Fixed it, the first parameter of move_uploaded_files() expects the tmp_name, not the name.

Upvotes: 2

Andres
Andres

Reputation: 2023

first off, and i sometimes forget make sure your form looks like this:

<form id="myform" name="myform" action="#" method="post" enctype="multipart/form-data">

key part of this is the

enctype="multipart/form-data">

and as far as uploading I use the following and it works everytime:

if ($_FILES["fuImg"]["error"] > 0)
    {
    echo "Error: " . $_FILES["fuImg"]["error"] . "<br>";
    }
else
    {
       move_uploaded_file($_FILES["fuImg"]["tmp_name"], "../img/bands/" . $_FILES["fuImg"]["name"]);
    }

Upvotes: 1

Related Questions