我有一个csv上传脚本,可以在我的PC上完美运行,其他PC也已经过测试。我有一个客户端在他们的Mac上尝试这个脚本,它返回一个错误:错误没有选择文件。
上传文件夹的权限为755。
真的需要找出原因。您可以在下面看到我的脚本和表单。
define("UPLOAD_DIR", "/home/path/public_html/uploads/".$company."/");
$message = null;
$allowed_extensions = array('csv');
if(!empty($_FILES['file'])){
if($_FILES['file']['error'] === 0){
$file = explode(".", $_FILES['file']['name']);
$extension = array_pop($file);
// ensure a safe filename
$name = preg_replace("/[^A-Z0-9._-]/i", "_", $_FILES['file']["name"]);
// don't overwrite an existing file
$i = 0;
$parts = pathinfo($name);
while (file_exists(UPLOAD_DIR . $name)) {
$i++;
$name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
}
if(in_array($extension, $allowed_extensions)){
if(move_uploaded_file($_FILES['file']['tmp_name'],
UPLOAD_DIR . $name)){
if(($handle = fopen(UPLOAD_DIR . $name, "r")) !== false){
$keys = array();
$out = 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)){
foreach($out as $key => $value){
$sql = "INSERT INTO ".$company."(id,prefix,fullname,last_name,business,address1,address2,address3,town,county,postcode,email,website,telephone1,telephone2,telephone3,landing_page,assigned_to,question1,question2,question3,question4,question5,question6,date,status,leadtype,file,gclid,leadid,tostep,ip,client_xp)VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$date = date("Y-m-d H:i:s");
$status = "New Lead";
$client_xp = 1;
$statement = $db->prepare($sql);
$statement->bindParam(1, $value[0], PDO::PARAM_STR);
$statement->bindParam(2, $value[1], PDO::PARAM_STR);
$statement->bindParam(3, $value[2], PDO::PARAM_STR);
$statement->bindParam(4, $value[3], PDO::PARAM_STR);
$statement->bindParam(5, $value[4], PDO::PARAM_STR);
$statement->bindParam(6, $value[5], PDO::PARAM_STR);
$statement->bindParam(7, $value[6], PDO::PARAM_STR);
$statement->bindParam(8, $value[7], PDO::PARAM_STR);
$statement->bindParam(9, $value[8], PDO::PARAM_STR);
$statement->bindParam(10, $value[9], PDO::PARAM_STR);
$statement->bindParam(11, $value[10], PDO::PARAM_STR);
$statement->bindParam(12, $value[11], PDO::PARAM_STR);
$statement->bindParam(13, $value[12], PDO::PARAM_STR);
$statement->bindParam(14, $value[13], PDO::PARAM_STR);
$statement->bindParam(15, $value[14], PDO::PARAM_STR);
$statement->bindParam(16, $value[15], PDO::PARAM_STR);
$statement->bindParam(17, $value[16], PDO::PARAM_STR);
$statement->bindParam(18, $value[17], PDO::PARAM_STR);
$statement->bindParam(19, $value[18], PDO::PARAM_STR);
$statement->bindParam(20, $value[19], PDO::PARAM_STR);
$statement->bindParam(21, $value[20], PDO::PARAM_STR);
$statement->bindParam(22, $value[21], PDO::PARAM_STR);
$statement->bindParam(23, $value[22], PDO::PARAM_STR);
$statement->bindParam(24, $value[23], PDO::PARAM_STR);
$statement->bindParam(25, $date, PDO::PARAM_STR);
$statement->bindParam(26, $status, PDO::PARAM_STR);
$statement->bindParam(27, $value[26], PDO::PARAM_STR);
$statement->bindParam(28, $value[27], PDO::PARAM_STR);
$statement->bindParam(29, $value[28], PDO::PARAM_STR);
$statement->bindParam(30, $value[29], PDO::PARAM_STR);
$statement->bindParam(31, $value[30], PDO::PARAM_STR);
$statement->bindParam(32, $value[31], PDO::PARAM_STR);
$statement->bindParam(33, $client_xp, PDO::PARAM_STR);
$statement->execute();
}
$message = '
<div class="row">
<div class="col-lg-6">
<div class="alert alert-success">
Success. Import complete. <a href="/import/"><i class="fa fa-times pull-right"></i></a>
</div>
</div>
</div>';
}
}
}
} else {
$message = '
<div class="row">
<div class="col-lg-6">
<div class="alert alert-warning">
Only CSV\'s allowed <a href="/import/"><i class="fa fa-times pull-right"></i></a>
</div>
</div>
</div>';
}
} else {
$message = '
<div class="row">
<div class="col-lg-6">
<div class="alert alert-warning">
There was a problem with your upload. Please try again. <a href="/import/"><i class="fa fa-times pull-right"></i></a>
</div>
</div>
</div>';
}
}
<form enctype="multipart/form-data" method="POST" action="/import/">
<div class="form-body pal">
<div class="row">
<div class="col-md-12">
<div class="form-group">
CSV file to import:<br /><br /><input type="file" name="file" id="file">
</div>
</div>
</div>
<input type="submit" name="submit" class="btn btn-blue" value="Upload">
</div>
</form>
答案 0 :(得分:0)
好的,所以通过将第一行代码更改为此
,这最终成为一个非常简单的解决方案/admin
在使用公司变量之前,倒置的逗号正在关闭字符串,使上传路径无效。