i'm kinda new in php with javascript/jQuery... I'm developing a system that must handle uploads. Using fileupload.js everything works but... I can't retrieve file info and store at mysql like name,extension,size,url ... When I try something I got an error message:
SyntaxError: Unexpected token <
The code that starts my problems is:
<?php
$options = array(
'delete_type' => 'POST',
'db_host' => 'localhost:3306',
'db_user' => 'root',
'db_pass' => '123456',
'db_name' => 'house',
'db_table' => 'midias'
);
error_reporting(E_ALL | E_STRICT);
require('UploadHandler.php');
class CustomUploadHandler extends UploadHandler {
protected function initialize() {
$this->db = new mysqli(
$this->options['db_host'],
$this->options['db_user'],
$this->options['db_pass'],
$this->options['db_name']
);
parent::initialize();
$this->db->close();
}
protected function handle_form_data($file, $index) {
$file->title = @$_REQUEST['title'][$index];
$file->description = @$_REQUEST['description'][$index];
}
protected function handle_file_upload($uploaded_file, $name, $size, $type, $error,
$index = null, $content_range = null) {
$file = parent::handle_file_upload(
$uploaded_file, $name, $size, $type, $error, $index, $content_range
);
if (empty($file->error)) {
$sql = 'INSERT INTO `'.$this->options['db_table']
.'` (`name`, `size`, `type`, `title`, `description`)'
.' VALUES (?, ?, ?, ?, ?)';
$query = $this->db->prepare($sql);
$query->bind_param(
$file->name,
$file->size,
$file->type,
$file->title, // I will try change for $_SESSION[userID]
$file->description // I will try change for dateOfUpload
);
$query->execute();
$file->id = $this->db->insert_id;
}
return $file;
}
protected function set_additional_file_properties($file) {
parent::set_additional_file_properties($file);
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$sql = 'SELECT `id`, `type`, `title`, `description` FROM `'
.$this->options['db_table'].'` WHERE `name`=?';
$query = $this->db->prepare($sql);
$query->bind_param('s', $file->name);
$query->execute();
$query->bind_result(
$id,
$type,
$title,
$description
);
while ($query->fetch()) {
$file->id = $id;
$file->type = $type;
$file->title = $title;
$file->description = $description;
}
}
}
public function delete($print_response = true) {
$response = parent::delete(false);
foreach ($response as $name => $deleted) {
if ($deleted) {
$sql = 'DELETE FROM `'
.$this->options['db_table'].'` WHERE `name`=?';
$query = $this->db->prepare($sql);
$query->bind_param('s', $name);
$query->execute();
}
}
return $this->generate_response($response, $print_response);
}
}
$upload_handler = new CustomUploadHandler($options);
I just can't figure out whats happening, I hope somebody can give me light Thank you for attention, and sorry for the huge code ;s
Try this:
I change description by date_description; make sure you have that field in your database, and it's a datetime type.
session_start();
$sql = 'INSERT INTO `'.$this->options['db_table']
.'` (`name`, `size`, `type`, `title`, `date_description`)'
.' VALUES (?, ?, ?, ?, now())';
$query = $this->db->prepare($sql);
$query->bind_param(
$file->name,
$file->size,
$file->type,
'$_SESSION[userID]'
);
I was experiencing the same problem. I couln't replace $file->anything
with a specific value. I tried 3
, '3'
, "3"
... as int
or String
. All of those returned the same SyntaxError: Unexpected token
My solution
$myvar = "hvhjvcvv";
$sql = 'INSERT INTO `'.$this->options['db_table']
.'` (`randomField`, `name`, `size`, `type`, `title`, `description`)'
.' VALUES (?, ?, ?, ?, ?, ?)';
$query = $this->db->prepare($sql);
$query->bind_param('ssisss', $myvar, $file->name, $file->size, $file->type, $file->title, $file->description);
$query->execute();
$file->id = $this->db->insert_id;
Hope this help.
Also note that the bad DB connection will return the same error ;)