2

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

6
  • So it tells you what the problem is. Are you disputing that the directory exists and is writable? Commented Jul 11, 2012 at 14:43
  • I would guess that (a) your web server does not have write permissions in the upload directory, or (b) you need to use an absolute path for the uploads directory instead of "upload/" Commented Jul 11, 2012 at 14:44
  • C:\xampp\htdocs\teecom\admin\upload\ directory exists?? Commented Jul 11, 2012 at 14:44
  • Temporary directory is: C:\Windows\Temp\phpD02C.tmp. However, I think apache does not have permission to read/write to this directory. I cannot find where I can change that directory. Commented Jul 11, 2012 at 14:46
  • Well, C:\xampp\htdocs\teecom\admin\upload\ exists. So does C:\windows\temp\. However, I think the problem is with temporary directory C:\windows\temp\. I don't think I have permission to this directory. Thats why I wanna change it. I have changed all php.ini files I can find. But it still uploads to this directory Commented Jul 11, 2012 at 14:51

4 Answers 4

2

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.

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

Comments

1

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"];
}

Comments

0

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.

6 Comments

@Waygood, good point! But I cannot find the file in temporary folder.
@holdoc, I changed php.ini file so that temporary folder would be something I have permission to. But, error messages still point to C:\windows\temp\ directory
check file_uploads, safe mode, max post size and upload_max_filesize too. I found working on windows previously (linux now) that I sometime had to restart the pc as apache restart didn't always work.
One more thing - do not try to look for the temporary upload files in the temp folder because you won't find them. All temporary upload files are removed after the request has been completed.
@holodoc You're right! Right now I am testing this upload issue on localhost. If I quickly switch from browser to file manager, the file appears in the uploaded files directory (also with correct filesize), but after a second, when the script is finished, it's removed. Yes the whole problem is only not existing target directory.
|
0

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.

1 Comment

also check php.ini for upload_max_filesize = 2M if you are attempting to upload a large file

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.