dapperwaterbuffalo
dapperwaterbuffalo

Reputation: 2748

unclear regarding uploading of csv file in php

So I want to:

I have been lookin at a tutorial found here.

however I am slightly confused with this:

if(isset($_FILES['csv_file']) && is_uploaded_file($_FILES['csv_file']['tmp_name'])){...

where does he establish 'tmp_name' from?

anyway, if somebody could explain how I should be going about this I would appreciate the help.

many thanks,

EDIT: added progress of where it is not working.

if(isset($_POST['csv_submit'])){
    if(isset($_FILES['csv_file']) && is_uploaded_file($_FILES['csv_file']['tmp_name'])){



        //upload directory
        $upload_dir = "/ece70141/csv_files/";
        //create file name
        $file_path = $upload_dir . $_FILES['csv_file']['name'];

        //move uploaded file to upload dir
            // GETTING THE ERROR BELOW.
        if (!move_uploaded_file($_FILES['csv_file']['tmp_name'], $file_path)) {
            //error moving upload file
            echo "Error moving file upload";
        }

        print_r($_FILES['csv_file']);

        //delete csv file
        unlink($file_path);
    }
}

Upvotes: 0

Views: 180

Answers (1)

Rudi Visser
Rudi Visser

Reputation: 21979

$_FILES is a magic superglobal similar to $_POST. It's an array of every file that's been uploaded in the last request, and where that file is stored (tmp_name).

tmp_name is basically generated by the web server to let PHP know where they've stored the file.

You have the following items available to you in each element of the $_FILES array:

  1. name (Original Name of the file)
  2. type (MIME Type of the file, ie. text/plain)
  3. tmp_name (Path to the uploaded files' temporary location)
  4. error (Error occurred when uploading, 0 when no error)
  5. size (Size of the uploaded file, in bytes)

From what I can see in your code, this will work perfectly fine and as discussed in comments, I think the issue lies in your HTML.

The tutorial that you linked to has an incorrect <form ..> tag definition. For file uploads, you're required to set the enctype attribute, below is what it should look like:

<form action="" method="post" enctype="multipart/form-data">

Upvotes: 5

Related Questions