0

I'm currently working on an extremely simplistic PHP file upload, but it fails to do anything at all. There is nothing in the specified directory.

PHP on the submit of my HTML form:

<?php

if ($_FILES["file"]["error"] > 0)
{
    //error
    echo "Something went wrong";
}
else
{
    move_uploaded_file($_FILES["file"]["tmp_name"],
    "/uploads/" . $_FILES["file"]["name"]);
}

?>

I suspect that something may be up with the fact that I'm using GoDaddy, since they've been quirky with php features in the past.

EDIT: Fixed underscore; now I'm getting an actual error.

Warning: move_uploaded_file(/uploads/asd.docx) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/content/09/11461509/html/php/upload.php on line 11

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpSIA5Jv' to '/uploads/asd.docx' in /home/content/09/11461509/html/php/upload.php on line 11
13
  • 1
    You may need to change "/uploads/" to "uploads/" without the leading slash. You're presently using an absolute path, when you should be using a relative path. Most likely the issue here. Also check to see after, if the folder is writeable. Commented Feb 1, 2014 at 5:54
  • 1
    also move_uploaded_file($FILES["file"]["tmp_name"], "/uploads/" . $_FILES["file"]["name"]); is wrong u have FILES should be $_FILES Commented Feb 1, 2014 at 5:55
  • try to debug using print_r or var_dump you can find the problem Commented Feb 1, 2014 at 5:56
  • @AbhikChakraborty: oops, I'll take a look to see if that fixes anything. Fred: I'm aware it's absolute; uploads is a folder in my root. Commented Feb 1, 2014 at 5:57
  • 1
    @Fred-ii- That's it! No idea why, but it works now. If you make this an answer I'll select it, thanks. Commented Feb 1, 2014 at 6:11

4 Answers 4

2

You're running your code from a different folder "php", from what I've gathered by your error messages. Try running the code from the root of your server, and then use uploads as your uploading folder.

Also make sure the folder has the proper write permissions set. Usually 755 and sometimes 777 although 755 is a safer setting.


As per OP's original posted code

Missing underscore between $ and FILES

move_uploaded_file($FILES["file"]["tmp_name"],
                 ---^

<?php

if ($_FILES["file"]["error"] > 0)
{
    //error
    echo "Something went wrong";
}
else
{
    move_uploaded_file($_FILES["file"]["tmp_name"],
    "/uploads/" . $_FILES["file"]["name"]);
}

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

Comments

0

Please add the enctype in form tag as multipart/form-data and PHP code should as follows-

First make sure that your files has been uploaded successfully in temp directory, then try to debug upload path and finally file moving functionality to specific directory.

<?php

if ($_FILES["file"]["error"] > 0) {
    //error
    echo "Something went wrong";
} else {
// try echoing the following codes first-
  echo "Upload: " . $_FILES["file"]["name"] . "<br>";
  echo "Type: " . $_FILES["file"]["type"] . "<br>";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];

// then try to debug upload path
// move_uploaded_file($_FILES["file"]["tmp_name"],"/uploads/" . $_FILES["file"]["name"]);
// take move_uploaded_file(); in a variable and then print_r($variable);
}

?>

I am referring you to read this post for upload problem the docx file using PHP.

1 Comment

PHP echoed: Upload: asd.docx Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document Stored in: /tmp/phpcWdZU4
0

Try this

<html>
<body>
<form action="" method="post"enctype="multipart/form-data">
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
?>

Comments

-1

please write in form tag enctype="multipart/form-data"

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.