我正在使用图像上传脚本的filedrop.js。 我在这里资助了一个脚本:https://tutorialzine.com/2011/09/html5-file-upload-jquery-php
在Prject中是一个file_post.php,我想将其更改为将一些信息(如文件名)保存到数据库中。
这是我的post_file.php:
<?php
// If you want to ignore the uploaded files,
// set $demo_mode to true;
$demo_mode = false;
$upload_dir = 'uploads/tmp/';
$allowed_ext = array('jpg','jpeg','png','gif');
if(strtolower($_SERVER['REQUEST_METHOD']) != 'post'){
exit_status('Error! Wrong HTTP method!');
}
if(array_key_exists('pic',$_FILES) && $_FILES['pic']['error'] == 0 ){
$pic = $_FILES['pic'];
if(!in_array(get_extension($pic['name']),$allowed_ext)){
exit_status('Only '.implode(',',$allowed_ext).' files are allowed!');
}
if($demo_mode){
// File uploads are ignored. We only log them.
$line = implode(' ', array( date('r'), $_SERVER['REMOTE_ADDR'], $pic['size'], $pic['name']));
file_put_contents('log.txt', $line.PHP_EOL, FILE_APPEND);
exit_status('Uploads are ignored in demo mode.');
}
// Move the uploaded file from the temporary
// directory to the uploads folder:
if(move_uploaded_file($pic['tmp_name'], $upload_dir.$pic['name'])){
//My added code
包括(&#39; /var/www/html/board/SSI.php');
$userName = $context['user']['name'];
$content_id = $_COOKIE["contentid"];
$pic_name = $pic['name'];
$pic_code = $content_id;
$pic_path = $pic_name;
$db_host = "******";
$db_name = "******";
$db_user = "******";
$db_pass = "******";
$db = mysqli_connect("$db_host","$db_user","$db_pass","$db_name") or die("Error " . mysqli_error($db));
$stmt = $db->prepare("INSERT INTO `User_pics` (content_id, path, user_id, user_name) VALUES (?, ?, ?, ?)");
$stmt->bind_param('ssss', $pic_code,
$pic_path,
$context['user']['id'],
$context['user']['name']);
$stmt->execute();
$stmt->close();
//end of my added code
exit_status('File was uploaded successfuly!');
}
}
exit_status('Something went wrong with your upload!');
// Helper functions
function exit_status($str){
echo json_encode(array('status'=>$str));
exit;
}
function get_extension($file_name){
$ext = explode('.', $file_name);
$ext = array_pop($ext);
return strtolower($ext);
}
?>
在我添加mysqli部分之后,不再显示成功消息。 在图像上传中,进度条停止在大约50%。文件已上传,信息被保存到数据库中,但我没有成功响应,我需要处理下一步。请求帮助! 感谢。
答案 0 :(得分:0)
问题是包含在UTF-8中编码的SSI.php。 这就是Json编码响应中出错的原因。
SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符
我的洗礼是: 我为用户ID和用户名创建了一个cookie,因此我可以删除Include。在此之后,每件事都很有效。如果某人有相同的错误,只需创建一个cookie,您在上传表格后显示您的上传表格。
感谢社区,祝你度过愉快的一天;)