I'm making a script to upload files. I have some questions.
I'm using the dropzonejs library.
I need to save the uploaded file info into the database. I made a prepared statement but it's not working: nothing is stored in the table.
Table info: userID (int 11), image_name (varchar 50), image_path (varchar 60), up_time (varchar 60), up_ip (varchar 45).
Where can I see the MySQL errors? I already checked the .err file from appdata and there was nothing there.
How can I return an
$error_msg
from the upload page to the form page? I'll have to move the first check (file exists) from the upload page to the dropzone.js file but I'm not sure how to do this. Maybe there's already an option for this, but I searched and found nothing.
HTML:
<link rel="stylesheet" href="/css/dropzone.css"/>
<script type="text/JavaScript" src="/js/dropzone.js"></script>
<form action="/upload"
class="dropzone"
id="my-awesome-dropzone"></form>
<?php
if (isset($error_msg)) {
echo '<p class="error">' . $error_msg . '</p>';
}
?>
My upload.php script:
require_once('/includes/functions.php');
$image = basename($_FILES['file']['name']);
$image = str_replace(' ','|',$image);
$img_dir = $_SERVER['DOCUMENT_ROOT'] . '/images/user_uploads/' . $_SESSION['username'] . '/';
if (file_exists($img_dir . $image)) {
return $error_msg = $img_dir . $_FILES["file"]["name"] . " ya existe. ";
} elseif (move_uploaded_file($_FILES["file"]["tmp_name"],
$img_dir . $image)) {
$img_path = $img_dir . $image;
$up_time = get_current_time();
$up_ip = get_ip_address();
if ($insert_stmt = $mysqli->prepare("INSERT INTO user_uploads (userID, image_name, image_path, up_time, up_ip) VALUES (?, ?, ?, ?, ?)")) {
$insert_stmt->bind_param('issss', $_SESSION['user_id'], $_FILES["file"]["name"], $img_path, $up_time, $up_ip);
if (! $insert_stmt->execute()) {
return $error_msg = '¡Error al subir la imagen! (db_error)';
}
}
} else return $error_msg = '¡Error al subir la imagen!';
This works only if I use $_SERVER['DOCUMENT_ROOT']
as the image directory.
- If the folder does not exist (each user will have their own folder for their uploads), how can I tell the script to automatically create it?
I'm using XAMPP in Windows, but once it's finished I'll upload it to Linux hosting, so maybe I'll have to change the paths or something. So what is the difference between these two operating systems?