Boy Pasmo
Boy Pasmo

Reputation: 8471

php file upload failed

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!

Upvotes: 1

Views: 9480

Answers (3)

Anand Somasekhar
Anand Somasekhar

Reputation: 596

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

  }
?>

Upvotes: 0

sanjeev
sanjeev

Reputation: 4621

$_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

Upvotes: 2

Evan
Evan

Reputation: 1

File & Dir permissions...

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

Upvotes: 0

Related Questions