1

I followed every tutorial in the internet that I find to upload a file. But still, It failed. It gave me this error:

Warning: move_uploaded_file(/var/www/projects/upload/TASK.txt): failed to open stream: No such file or directory in /var/www/projects/test/upload.php on line 6 Warning: move_uploaded_file(): Unable to move '/tmp/phpjr2JJA' to '/var/www/projects/upload/TASK.txt' in /var/www/projects/test/upload.php on line 6 Something went wrong

index.html

 <head>
        <title></title>
    </head>
    <body>
        <form method="POST" action="upload.php" enctype="multipart/form-data">
            <input type="file" name="upload" ><br />
            <input type="hidden" name="MAX_FILE_SIZE" value="1024" />
            <input type="submit" name="submit" value="Submit" />
        </form>
    </body>
    </html>

upload.php

<?php

$target_path = $_SERVER['DOCUMENT_ROOT'] . "/upload/";
$target_path = $target_path . basename( $_FILES['upload']['name'] );

if ( move_uploaded_file($_FILES['upload']['tmp_name'], $target_path) ) {
    echo "has been uploaded";
} else {
    echo "Something went wrong";
}

Can you help me and point out where I went wrong? I'm using ubuntu 12.04 and also I tried to change the permission for the /upload folder to 755 and checked the file_upload in php.ini is ON

Any help would be much appreciated. Thanks!

5
  • first check the directory permission and directory location. are you server user and current user on same group? Commented Jan 11, 2014 at 15:18
  • Check file permissions and check whether $_SERVER['DOCUMENT_ROOT'] . "/upload/"; really results in the directory you think it is. Commented Jan 11, 2014 at 15:20
  • @crack, yes I've checked it and did change the permission to 755. As for group. There are not in the same group. peeha yes, the $target_path is the directory I want the file to be uploaded. Commented Jan 11, 2014 at 15:24
  • to where should I apply? Commented Jan 11, 2014 at 15:41
  • By the way, the real problem is you are trying to copy the file to "/var/www/projects/upload/TASK.txt" location instead of "/var/www/projects/test/upload/TASK.txt". Nouphal.M's solution fixes the problem. Commented Jan 11, 2014 at 15:44

3 Answers 3

2

$_SERVER['DOCUMENT_ROOT'] gives /var/www/projects/upload.. as output / in the start is cause of error

Hence

try with relative path

<?php

$target_path = "upload/";
$target_path = $target_path . basename( $_FILES['upload']['name'] );

if ( move_uploaded_file($_FILES['upload']['tmp_name'], $target_path) ) {
echo "has been uploaded";
} else {
echo "Something went wrong";
}
?>

worked for me

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

Comments

0

File & Dir permissions...

Add in your FTP Program 666 to the files and dirs what you need to write.

Comments

0

I think ur file upload location incorrect

index.php

<html>
<head>
    <title>Upload your file</title>
</head>
<body>

<form action="upload.php" method="post"
enctype="multipart/form-data">
   <label for="file">Filename:</label>
    <input type="file" name="file" id="file"><br>
    <input type="submit" name="Upload" value="Submit">
</form>
</body>

upload.php

<?php

 $target="give the path you want to store the file/";


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

  }
?>

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.