0

So i'm printing an array that i'm getting but i can't figure out how to access the stuff, im an complete noob by the way.

Thing i want to unpack:

code:

<?php
    $file = $_FILES['files'];

    $fileName = $_FILES['files']['name'];
    $fileTmpName = $_FILES['files']['tmp_name'];
    $fileSize = $_FILES['files']['size'];
    $fileError = $_FILES['files']['error'];
    $fileType = $_FILES['files']['type'];

    print_r(in_array("screenshot.jpg", $fileName)."woooo");

    print_r($file);
    $fileExt = explode(".", $fileName);
    $fileActualExt = strtolower(end($fileExt));


    $allowed = array('jpg', "jpeg", "png", "pdf");

    if (in_array($fileActualExt, $allowed)){
        if ($fileError === 0) {
            if($fileSize < 1000000) {
                $fileNameNew =  uniqid('', true).".".$fileActualExt;
                $fileDestination = 'uploads/'.$fileNameNew;

                move_uploaded_file($fileTmpName, $fileDestination);

            } else {
                echo 'You can not have a file bigger than 1 GigaByte!';
            }
        }else {
            print_r($fileError);
            echo 'There was an error uploading your file!';
        }
    }else {
        echo "You cannot upload files of this type!";
}

but i do not know how to print the name that first name that im getting.

1 Answer 1

1

You have a multiple file selector in your form, so all the elements of $_FILES are arrays, to allow for uploading multiple files. So you need to loop over them.

<?php
$file = $_FILES['files'];

foreach ($_FILES['files']['name'] as $i => $fileName) {
    $fileTmpName = $_FILES['files']['tmp_name'][$i];
    $fileSize = $_FILES['files']['size'][$i];
    $fileError = $_FILES['files']['error'][$i];
    $fileType = $_FILES['files']['type'][$i];

    print_r(in_array("screenshot.jpg", $fileName)."woooo");

    print_r($file);
    $fileExt = explode(".", $fileName);
    $fileActualExt = strtolower(end($fileExt));

    $allowed = array('jpg', "jpeg", "png", "pdf");

    if (in_array($fileActualExt, $allowed)){
        if ($fileError === 0) {
            if($fileSize < 1000000) {
                $fileNameNew =  uniqid('', true).".".$fileActualExt;
                $fileDestination = 'uploads/'.$fileNameNew;

                move_uploaded_file($fileTmpName, $fileDestination);

            } else {
                echo 'You can not have a file bigger than 1 GigaByte!';
            }
        }else {
            print_r($fileError);
            echo 'There was an error uploading your file!';
        }
    }else {
        echo "You cannot upload files of this type!";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.