我正在寻找为用户显示错误的最佳方法,我正在使用类文件()和配置文件页面。类File()将处理以下事项:-检查文件扩展名-检查文件是否已经存在-爆炸并创建链接文件&&插入到数据库的链接-上传文件到项目文件夹
代码正常工作,除了错误显示,例如,如果扩展名错误,它将显示最后一个错误,而不是停止执行并显示扩展名错误。
如果文件已经存在于数据库&&文件夹目录下的php函数move_uploaded_file将显示错误"Warning: failed to open stream: permission denied"
谢谢你的帮助
class File {
private $file = array();
private $file_up = '';
private $pdo = '';
private $error = '';
private $regex_file = '/^[a-z0-9][a-z0-9]{4,20}.jpg|.jpeg|.png|.gif|.txt|.doc|.docx|.pdf|.xlsx|.xlm|.xls|.pub|.one|.pptx$i/';
/**
* [__construct connection ]
* @param [int] $id [Unique user_id retrieved from database (from include/header.inc)]
* Construct database connection using PDO
*
*/
public function __construct($id)
{
$this->pdo = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$sql = 'SELECT `file_name`, `file_extention` FROM `users`'
.'JOIN `file`'
.'ON users.`user_id` = file.`user_id`'
.'WHERE users.`user_id` = :id';
$stmt = $this->pdo->prepare($sql);
$stmt->execute(array(':id' => $id));
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$this->file[] = $row['file_name'] . '.' . $row['file_extention'];
}
}
/**
* [displayFile extention]
* @return [link] [description]
*/
public function displayFile()
{
$output = '';
$link = $this->file;
foreach($link as $row) {
$output .= '<table class="table table-bordered table-hover"';
$output .= '<thead><tr>';
$output .= '<th><a href="../file_user/'.$row.'><i class="fa fa-folder"></i>  '.$row.'</a><br /></th>';
$output .= '</tr></thead>';
$output .= '</table>';
}
return $output;
}
public function checkFile($file)
{
$this->file_up = strip_tags($file);
if(!preg_match($this->regex_file, $this->file_up)){
$this->error = '<b style="color:red">Extention ou nom de fichier incorrect</b>';
}
foreach($this->file as $row){
if($this->file_up == $row){
$this->error = '<b style="color:red">Fichier ou nom du fichier deja existant</b>';
}
}
if($this->error){
return $this->error;
}else{
$this->file_up = explode(".", $this->file_up);// array
return $this->file_up;
}
}
public function getError()
{
if($this->error !== ''){
return $this->error;
}
}
public function uploadFile($array, $id)
{
if(is_array($array)){
array_push($array, $id);
$sql = 'INSERT INTO `file`(`file_name`, `file_extention`, `user_id`) VALUES (?, ?, ?)';
$con = $this->pdo->prepare($sql);
$con->execute($array);
}else{
$this->error = '<b style="color:red">Fichier ne peut etre telecharger</b>';
}
}
public function mvFile($size, $name, $tmp_name)
{
$to = 'file_user/';
if($size <= 2000000){
move_uploaded_file($tmp_name, $to.$name);
$this->error = '<b style="color:green">Fichier téléchargé avec succes</b>';
}else{
$this->error = '<b style="color:red">Un probleme est survenue veuillez recommencer</b>';
}
}
}
和testing.php来测试它:
require_once 'class/file.inc';
$error = '';
$id = 2;
$file = new File($id);
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$type = $_FILES['file']['type'];
$tmp_name = $_FILES['file']['tmp_name'];
if(isset($_FILES['file'])){
$file_up = $_FILES['file']['name'];
$file_up = $file->checkFile($file_up);
$file->uploadFile($file_up, $id);
//$file->mvFile($size, $name, $_FILES['file']['tmp_name']);
if($file->getError()){
$error = $file->getError();
}
} // end isset
if($error){
echo $error;
}
两个独立的问题:
-
如果你在你的checkFile()方法中检测到任何错误,并且你只想得到第一个错误(从我的角度来看这是奇怪的;您可以在数组中收集错误,您应该直接返回该错误,而不是继续执行方法的其余部分。此外,我会考虑协调返回值。你的方法应该返回错误还是文件名?
-
你关于move_uploaded_file()的问题似乎与权限有关。检查您的源(tmp上载)目录和目标r/w权限。