ankit kumar
ankit kumar

Reputation: 75

uploading of file does not work in php

I am working on php web application where i upload a file on server from local machine using following code.

        <form action="upload.php" method="post" enctype="multipart/form-data">
         Select file to upload:
        <input type="file" name="fileToUpload" id="fileToUpload" value="c:/passwords.txt">
        <input type="submit" value="Upload" name="submit">
        </form>

upload.php

$target_dir = "uploads/";
        $target_file =  $target_dir .basename($_FILES["fileToUpload"]["name"]);
        $uploadOk = 1;



        if ($_FILES["fileToUpload"]["size"] > 5000000) {
            echo "Sorry, your file is too large.";
            $uploadOk = 0;
        }


        if ($uploadOk == 0) {
           echo "Sorry, your file was not uploaded.";
            // if everything is ok, try to upload file
        } else {
        echo $_FILES['fileToUpload']['name']."<br>";    
        if(move_uploaded_file($_FILES["fileToUpload"]["name"], $target_file)) {       
             echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
          } else {
             echo "Sorry, there was an error uploading your file.";
           }
        }

But above code is not working. I checked uploads folder permission

drwxrwxrwx 2 ankit ankit 4096 Aug  2 13:41 uploads

which seems to be writable. I changed my project permission using following command

  sudo chmod -R 777 /var/www/myproject

But still i cannot upload the files on server. May i know where i am wrong?

Upvotes: 2

Views: 3641

Answers (3)

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

Change this line:

$_FILES["fileToUpload"]["name"] // contains the original name of the uploaded file from the user's computer

to

$_FILES["fileToUpload"]["tmp_name"] // tmp_name will contain the temporary file name of the file on the server

Documentation for move_uploaded_file. You want to move the uploaded file! ;-)

It will help you

Upvotes: 3

Passionate Coder
Passionate Coder

Reputation: 7294

In your comments you say that you are getting error 1 when you use

print_r($_FILES);

which is due to

 1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',

So do this

upload_max_filesize = 'Size'; // whatever you want

Documentation about $_FILES errors

Upvotes: 5

ameenulla0007
ameenulla0007

Reputation: 2683

move_uploaded_file expects parameter one to be source, which is tmp_name

so change

if(move_uploaded_file($_FILES["fileToUpload"]["name"], $target_file)) {

to

if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {

Upvotes: 2

Related Questions