Azad Salahli
Azad Salahli

Reputation: 916

File doesn't upload to localhost via PHP

I cannot upload file to localhost using PHP. I have created simple html form and php script. However I get these error messages.

'import.html'

<html>
    <body>
        <form action="import.php" method="POST" enctype="multipart/form-data">
            <p>
                <label for="file">Choose import.xml</label><br/>
                <input type="file" name="import" id="import" /></p>
            <p><input type="submit" name="submit" value="Submit" /></p>
        </form>
    <body>
</html>

'import.php'

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

        if (file_exists("upload/" . $_FILES["import"]["name"]))
        {
            echo $_FILES["import"]["name"] . " already exists. ";
        }
        else
        {
            move_uploaded_file($_FILES["import"]["tmp_name"],
            "upload/" . $_FILES["import"]["name"]);
            echo "Stored in: " . "upload/" . $_FILES["import"]["name"];
        }
    }

?>

Error messages:

Warning: move_uploaded_file(upload/import.xml) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\xampp\htdocs\teecom\admin\import.php on line 20

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\Windows\Temp\phpD02C.tmp' to 'upload/import.xml' in C:\xampp\htdocs\teecom\admin\import.php on line 20 Stored in: upload/import.xml

Upvotes: 2

Views: 18068

Answers (4)

Oscar
Oscar

Reputation: 150

This is old, but for people who have this problem in the future, all I did for my localhost (wamp) is click the server icon, go to PHP, PHP Settings, and select File Uploads.

This worked for me.

Upvotes: 0

Stegrex
Stegrex

Reputation: 4024

It looks like you're using Windows.

I'd change the destination path from a relative to an absolute path if possible. For example:

move_uploaded_file($_FILES["import"]["tmp_name"],
            "C:/upload/" . $_FILES["import"]["name"]);

Or try the path:

$_SERVER['DOCUMENT_ROOT'] . '/upload/' . $_FILES['import']['name']

Also try creating that C:\upload\ or C:\xampp\htdocs\upload\ directory first before trying to upload to it.

Upvotes: 2

Stano
Stano

Reputation: 8939

Well, according to your localhost directories you can try this:

if (!file_exists("teecom/upload"))
{
    mkdir("teecom/upload", 0777, true);
}
if (file_exists("teecom/upload/" . $_FILES["import"]["name"]))
{
    echo $_FILES["import"]["name"] . " already exists. ";
}
else
{
    move_uploaded_file($_FILES["import"]["tmp_name"],
    "teecom/upload/" . $_FILES["import"]["name"]);
    echo "Stored in: " . "upload/" . $_FILES["import"]["name"];
}

Upvotes: 1

user188654
user188654

Reputation:

Your file was clearly not uploaded to the temporary folder from which the move_uploaded_file function is supposed to move it. There is plenty of reasons why that might happen the most frequent one being that you don't have write permissions to the temporary folder PHP is using.

Upvotes: 0

Related Questions