大家好的一天,如果名称和文件扩展名与其中一个已保存的记录匹配,我想阻止上传文件。我的代码可以使用,但如何避免重复输入?
"My sample array"
Array
(
[upload-file] => Array
(
[name] => Penguins.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp\tmp\phpC87.tmp
[error] => 0
[size] => 777835
)
)
case 'upload-file':
$arr = [
":userid" => $_SESSION['loggedIn_PH'][0]['user_id'],
":filename" => $_FILES['upload-file']['name'],
":filelink" => $_FILES['upload-file']['tmp_name']
];
$allowed = array('xls','xlsx');
$filename = $_FILES['upload-file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed) ) {
$response_code = -1;
}else{
$response_code = 1;
$folder = time();
mkdir("path/".$folder);
$file = "path".DIRECTORY_SEPARATOR.$folder.DIRECTORY_SEPARATOR.$_FILES['upload-file']['name'];
move_uploaded_file($_FILES['upload-file']['tmp_name'], $file);
$query = "INSERT INTO file_rec_tbl ( `file_name`, `file_datetime`,`file_link`, `user_id` )
VALUES (:filename, '".date('Y-m-d H:i:s')."',:filelink,:userid)";
$stmt = $con -> prepare( $query );
$stmt -> execute( $arr );
}
exit(json_encode(array('r_code' => $response_code)));
break;
答案 0 :(得分:1)
<?php
$file='file-to-check.ext';
if (file_exists($file)) {
echo "exists";
} else {
echo "not exist";
}
?>
对我有用 - 显然$ file也可以包含一个路径 - 这个脚本只检查当前文件夹
答案 1 :(得分:0)
简单的代码可以这样:
$file_name = 'path/to/file_name.ext';
if (file_exist($file_name)) {
return true;
} else {
move_uploaded_file($_FILES['upload-file']['tmp_name'], $file_name);
insert into database;
return true;
}
此外,您不应该将两个文件与文件名进行比较。
答案 2 :(得分:0)
T1 = table(['M';'F';'MM'])
Dimensions of matrices being concatenated are not consistent.