0

I'm making a script to upload files. I have some questions.

I'm using the dropzonejs library.

  1. 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.

  2. 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.

  1. 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?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Chazy Chaz
  • 1,781
  • 3
  • 29
  • 48

2 Answers2

2

Ok I got it :)

$tmp_name = $_FILES["file"]["tmp_name"];

$image = basename($_FILES['file']['name']);
$image = str_replace(' ', '|', $image);

$img_dir = $_SERVER['DOCUMENT_ROOT'] . '/images/user_uploads/' . $_SESSION['username'] . '/';

if (!file_exists($_SERVER['DOCUMENT_ROOT'] . '/images/user_uploads/' . $_SESSION['username'])) {
    mkdir($_SERVER['DOCUMENT_ROOT'] . '\\images\\user_uploads\\' . $_SESSION['username']);
}

move_uploaded_file($tmp_name, $img_dir . $image);

$img_path = $_SESSION['username'] . '/' . $image;
$up_time = get_current_time();
$up_ip = get_ip_address();

if ($stmt = $mysqli->prepare("INSERT INTO user_uploads (userID, image_name, image_path, up_time, up_ip) VALUES (?, ?, ?, ?, ?)")) {
    $stmt->bind_param('issss', $_SESSION['user_id'], $image, $img_path, $up_time, $up_ip);
    $stmt->execute();
}

Using this javascript I think it's only possible to return error messages from the javascript itself so creating functions or setting the messages will be ok:

  dictInvalidFileType: "No puedes subir este tipo de archivos.",
  dictResponseError: "Código de error {{statusCode}}.",

Hope it helps someone :)

Chazy Chaz
  • 1,781
  • 3
  • 29
  • 48
0
  1. Please provide your final query and its value
  2. You upload code in inside one file, so you can not return value by including file, you need to wrap that code in function or class method.
  3. Refer this: Create a folder if it doesn't already exist
Community
  • 1
  • 1
Avinash
  • 6,064
  • 15
  • 62
  • 95
  • What do you mean by final query and it's value? What will be send to MySQL? So, I have to make a function and send it the two variables: $_FILES['file']['name'] and $_FILES['file']['tempname'] right? – Chazy Chaz Oct 11 '14 at 20:08