0

I have an upload form in a php page , that puts a csv file into a database.

I'm getting an error upon submission of my csv file:

Warning: move_uploaded_file(/public_html/csv/test/books.csv) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/blakeloi/public_html/csv/index.php on line 19

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpZw2e2X' to '/public_html/csv/test/books.csv' in /home/blakeloi/public_html/csv/index.php on line 19

This is the line that is wrong:

$upload_path = '/Users/blakeloizides/Desktop';

In:

if (move_uploaded_file($_FILES['file']['tmp_name'], $upload_path.'/'.$_FILES['file']['name']))

I have tried renaming my file path many times now, and nothing seems to work. The most obvious answer would be that my file directory path is wrong , but I put the folder into my browser and got the correct path. I even tried to use a folder on my server /public_html/test, but this did not work.

<?php

$message = null;

$allowed_extensions = array('csv');

$upload_path = '/Users/blakeloizides/Desktop';

if (!empty($_FILES['file'])) {

if ($_FILES['file']['error'] == 0) {

    // check extension
    $file = explode(".", $_FILES['file']['name']);
    $extension = array_pop($file);

    if (in_array($extension, $allowed_extensions)) {

        if (move_uploaded_file($_FILES['file']['tmp_name'], $upload_path.'/'.$_FILES['file']['name'])) {

            if (($handle = fopen($upload_path.'/'.$_FILES['file']['name'], "r")) !== false) {

                $keys = array();
                $out = array();

                $insert = array();

                $line = 1;

                while (($row = fgetcsv($handle, 0, ',', '"')) !== FALSE) {

                    foreach($row as $key => $value) {
                        if ($line === 1) {
                            $keys[$key] = $value;
                        } else {
                            $out[$line][$key] = $value;

                        }
                    }

                    $line++;

                }

                fclose($handle);    

                if (!empty($keys) && !empty($out)) {

                    $db = new PDO('mysql:host=localhost;dbname=blakeloi_import-csv', 'blakeloi_root', 'password');
                    $db->exec("SET CHARACTER SET utf8");

                    foreach($out as $key => $value) {

                        $sql  = "INSERT INTO `books` (`";
                        $sql .= implode("`, `", $keys);
                        $sql .= "`) VALUES (";
                        $sql .= implode(", ", array_fill(0, count($keys), "?"));
                        $sql .= ")";
                        $statement = $db->prepare($sql);
                        $statement->execute($value);

                    }

                    $message = '<span class="green">File has been uploaded successfully</span>';

                }   

            }

        }

    }     else {
        $message = '<span class="red">Only .csv file format is allowed</span>';
    }

} else {
    $message = '<span class="red">There was a problem with your file</span>';
}

}

?>
    <!DOCTYPE HTML>
    <html lang="en">
    <head>
<meta charset="utf-8" />
<title>Upload CSV to MySQL</title>
<meta name="description" content="" />
<meta name="keywords" content="" />
<link href="css/core.css" rel="stylesheet" type="text/css" />
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>

<section id="wrapper">  

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

    <table cellpadding="0" cellspacing="0" border="0" class="table">
        <tr>
            <th><label for="file">Select file</label> <?php echo $message; ?></th>
        </tr>
        <tr>
            <td><input type="file" name="file" id="file" size="30" /></td>
        </tr>
        <tr>
            <td><input type="submit" id="btn" class="fl_l" value="Submit" /></td>
        </tr>
    </table>

</form>

</section>

</body>
</html>

enter image description here

My index.php file is sitting in public_html/csv or http://www.blakeloizides.co.za/csv and I want to upload the file to public_html/csv/test or http://www.blakeloizides.co.za/csv/test

My permissions are 755 for both folders.

enter image description here

j0k
  • 22,600
  • 28
  • 79
  • 90
Blake Loizides
  • 985
  • 2
  • 20
  • 43

1 Answers1

0

I had to change my file path to /home/blakeloi/public_html/csv/test . That was my error I was using /public_html/csv/test

Blake Loizides
  • 985
  • 2
  • 20
  • 43