Upload File Server PHP

1

I have to upload 2 images. One is a product image and another is a QR. Also the code I want to insert in a MySQL database the URL of each image. I managed to get the URL of each image registered in the database and upload one of the two images, but the image that corresponds to the QR code is not only the first server, I do not receive any error message.

This is the table in MySql The product image is "product_image" and that of the QR code is product_qr

This is the user "interface" created with "modal" in PHP- Then I put the piece of code that corresponds to the function that connects to the database and that is responsible for uploading the files:

$type = explode('.', $_FILES['productImage']['name']);
$type = $type[count($type)-1];
$typeQR = explode('.', $_FILES['productQR']['name']);
$typeQR = $typeQR[count($typeQR)-1];


$url = '../assests/images/stock/'.uniqid(rand()).'.'.$type;
$urlQR = '../assests/images/stock/'.uniqid(rand()).'.'.$typeQR;

if(in_array($type, array('gif', 'jpg', 'jpeg', 'png', 'JPG', 'GIF', 'JPEG', 'PNG')) ||
in_array($typeQR, array('gif', 'jpg', 'jpeg', 'png', 'JPG', 'GIF', 'JPEG', 'PNG')) ) {
    if(is_uploaded_file($_FILES['productImage']['tmp_name']) ||
    is_uploaded_file($_FILES['productQR']['tmp_name']) ) {
        if(move_uploaded_file($_FILES['productImage']['tmp_name'], $url) ||
        move_uploaded_file($_FILES['productQR']['tmp_name'], $urlQR)) {

            $sql = "INSERT INTO product (product_name, product_image, brand_id, categories_id, quantity,
            rate, active, status, description, position_store, status_product, reference, product_qr)
            VALUES ('$productName', '$url', '$brandName', '$categoryName', '$quantity', '$rate',
             '$productStatus', 1, '$productDescription', '$positionStore', '$statusObject', '$reference', '$urlQR')";

This is the "modal" code:

<div class="modal-body" style="max-height:450px; overflow:auto;">

        <div id="add-product-messages"></div>

        <div class="form-group">
            <label for="productImage" class="col-sm-3 control-label">Imagen: </label>
            <label class="col-sm-1 control-label">: </label>
                <div class="col-sm-8">
                    <!-- the avatar markup -->
                        <div id="kv-avatar-errors-1" class="center-block" style="display:none;"></div>
                    <div class="kv-avatar center-block">
                        <input type="file" class="form-control" id="productImage" placeholder="Imagen del producto" name="productImage" class="file-loading" style="width:auto;"/>
                    </div>

                </div>
        </div>

            <div class="form-group">
            <label for="productQR" class="col-sm-3 control-label">QR: </label>
            <label class="col-sm-1 control-label">: </label>
                <div class="col-sm-8">
                    <!-- the avatar markup -->
                        <div id="kv-avatar-errors-1" class="center-block" style="display:none;"></div>
                    <div class="kv-avatar center-block">
                        <input type="file" class="form-control" id="productQR" placeholder="Imagen QR" name="productQR" class="file-loading" style="width:auto;"/>
                    </div>

                </div>
        </div>
        Y>

As I say, everything works fine, because the QR image does not upload to the server.

    
asked by Oscar C. 28.10.2018 в 23:43
source

2 answers

1

I was not uploading the two images (Files) because in the expression "in_array" I was using "o" - > "||". To work, you must declare "and" - > "& amp;" Therefore the code would be as follows:

    if(in_array($type, array('gif', 'jpg', 'jpeg', 'png', 'JPG', 'GIF', 'JPEG', 'PNG')) && in_array($typeQR, array('gif', 'jpg', 'jpeg', 'png', 'JPG', 'GIF', 'JPEG', 'PNG')) ) {
    if(is_uploaded_file($_FILES['productImage']['tmp_name']) && is_uploaded_file($_FILES['productQR']['tmp_name']) ) {          
        if(move_uploaded_file($_FILES['productImage']['tmp_name'], $url) && move_uploaded_file($_FILES['productQR']['tmp_name'], $urlQR)) {
    
answered by 31.10.2018 / 20:55
source
0

I recommend you use the arcaela framework, it's on Github link You have a fileManager () call in your classes; It allows you to upload files without so many complications, just call the class like this:

//Abres el Object
$files = new fileManager();

//No te preocupes si es $_FILES o es Base64 o es un link de una imagen,
//La funcion lo detecta y hace lo que deba hacer para crear la imagen.
$files = $files->addFiles($_FILES['nombre_del_input']);

//Indicas el fichero
$files = $files->setDir("/ruta/donde/subirás_el_archivo/");

//Inicias el upload
$files = $files->start();

It will return an array with 3 values, Wait, Error and Success, in the wait are the files that exceed to upload, others I think you understand. If you need to cancel the operation just call $ files- > abort ();

When the files are loaded correctly, they return an array each with the name, path and date. Already with those values I think you can work on the MySql.

It should be noted that this arc system is in development but I saw that in BitBucket they constantly update, only that they release the public very few times.

    
answered by 29.10.2018 в 01:06