Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法打开流:没有此类文件或目录_Php_Mysql_File Upload_Csv - Fatal编程技术网

Php 无法打开流:没有此类文件或目录

Php 无法打开流:没有此类文件或目录,php,mysql,file-upload,csv,Php,Mysql,File Upload,Csv,我在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:

我在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';
在:

我已经多次尝试重命名我的文件路径,但似乎没有任何效果。最明显的答案是,我的文件目录路径是错误的,但我将文件夹放入浏览器并获得了正确的路径。我甚至尝试使用我的服务器/public\u 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>

我不得不将文件路径更改为/home/blakeloi/public_html/csv/test。这是我的错误,我正在使用/public\u html/csv/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>