2

Hi all I am trying to upload file in php. But it is not uploading the file

Here is the code

$excel = new PhpExcelReader;
if(isset($_POST["submit"]))
{
    $target_dir="../upload/";
    $target_path=$target_dir.basename($_FILES['fileToUpload']['name']);     

    //move_uploaded_file($_FILES['fileToUpload']['name'],$target_path);
    if(move_uploaded_file($_FILES['fileToUpload']['name'],$target_path))
    {
        echo basename($_FILES['fileToUpload']['name']);
    }
    else
    {
        echo "Possible file upload attack!\n";
    }
        print_r($_FILES);
    /* $handle = realpath($_FILES["fileToUpload"]["name"]);
    $excel = new PhpExcelReader;
    $excel->read($handle);
    echo $handle; */

}

This code always throw me in else condition.In my html form I also added enctype="multipart/form-data" and also check $_FILES array in my $_FILES array I am getting this

Array ( [fileToUpload] => Array ( [name] => Copy of Book1.xlsx [type] => application/vnd.openxmlformats-officedocument.spreadsheetml.sheet [tmp_name] => H:\PHP\xampp\tmp\phpF54F.tmp [error] => 0 [size] => 13459 ) )

2 Answers 2

6

In your if condition change it to move_uploaded_file($_FILES['fileToUpload']['tmp_name'],$target_path)

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

2 Comments

thanks dude its works but why its not work with filename?
while uploading file on server it first store in temp location and move_uploaded_file function get file from temp location and upload in given destination location.
2

try this.

if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'],$target_path))
{
    echo basename($_FILES['fileToUpload']['name']);
} else {
    echo "Possible file upload attack!\n";
}

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.