0
我有一個php頁面的上傳表單,將csv文件放入數據庫。未能打開流:沒有這樣的文件或目錄PHP
我在提交我的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']))
我有嘗試重命名我的文件路徑多次,並沒有什麼似乎工作。最明顯的答案是,我的文件目錄路徑是錯誤的,但我把文件夾放到我的瀏覽器中,得到了正確的路徑。我甚至試圖在我的server/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 /測試或http://www.blakeloizides.co.za/csv/test
我的權限是755兩個文件夾。
您是否檢查了上傳文件夾的權限? – amburnside
經常遇到此錯誤,並快速排除故障,請按照下列步驟操作:https://stackoverflow.com/a/36577021/2873507 –