0

我在 php 页面中有一个上传表单,它将 csv 文件放入数据库。

提交 csv 文件时出现错误:

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

这是错误的行:

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

在:

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

我现在多次尝试重命名文件路径,但似乎没有任何效果。最明显的答案是我的文件目录路径错误,但我将文件夹放入浏览器并获得了正确的路径。我什至尝试在我的服务器 /public_html/test 上使用一个文件夹,但这不起作用。

<?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>

在此处输入图像描述

我的 index.php 文件位于 public_html/csv 或http://www.blakeloizides.co.za/csv 我想将文件上传到 public_html/csv/test 或http://www.blakeloizides.co.za /csv/测试

我的两个文件夹的权限都是 755。

在此处输入图像描述

4

1 回答 1

0

我不得不将我的文件路径更改为 /home/blakeloi/public_html/csv/test 。那是我使用 /public_html/csv/test 的错误

于 2012-11-28T11:41:07.953 回答