Bama
Bama

Reputation: 577

Why isn't upload.php uploading the file to the Directory?

Thank you so much for taking the time to read this and answer, We have a php file that executes, it prints out the url as index.php?uploadyay just as we labeled it while writing the code in the header section. It Seems to execute Just fine. The only issue and a really big one is that it does not place the file into the server folder from file destination variable it does not seem to place the local file anywhere in the server. Our Code is as follows,

<form action="upload.php" method="POST" enctype="multipart/form-data">
        <input type="file" name="file">
        <button type="submit" name="submit">
            UPLOAD
        </button>
    </form>

<?php 
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
if (isset($_POST['submit'])) {
    $file = $_FILES['file'];

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

    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));

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

    if (in_array($fileActualExt, $allowed)) {
        if ($fileError === 0) {
            if ($fileSize < 200000000) {
                $fileNameNew = uniqid('', true).".".$fileActualExt;
                $fileDestination = '/public_html/styleuploads'.$fileNameNew;
                move_uploaded_file($fileTmpName, $fileDestination);
                header("Location: index.php?uploadyay");
                exit;
            } else echo "Opps, your file is too big! It needs to be smaller than 200 Megabytes";
        } else {
            echo "There was an error uploading your file";
        }
    } else {
        echo "You can not upload files of that type";
    }
}
?>

Upvotes: 1

Views: 53

Answers (1)

sunny bhadania
sunny bhadania

Reputation: 423

Please check Below code

<form action="upload.php" method="POST" enctype="multipart/form-data">
        <input type="file" name="file">
        <button type="submit" name="submit">
            UPLOAD
        </button>
    </form>

<?php 
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
if (isset($_POST['submit'])) {
    $file = $_FILES['file'];

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

    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));

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

    if (in_array($fileActualExt, $allowed)) {
        if ($fileError === 0) {
            if ($fileSize < 200000000) {
                $fileNameNew = uniqid('', true).".".$fileActualExt;
                $fileDestination = $_SERVER['DOCUMENT_ROOT'].'/public_html/';
                $moved = move_uploaded_file($fileTmpName, $fileDestination.'styleuploads'.$fileNameNew);
                header("Location: index.php?uploadyay");
                exit;
            } else echo "Opps, your file is too big! It needs to be smaller than 200 Megabytes";
        } else {
            echo "There was an error uploading your file";
        }
    } else {
        echo "You can not upload files of that type";
    }
}
?>  

Upvotes: 1

Related Questions