Form does not execute PHP script [closed]

-1

Good afternoon from StackOverflow. I have an HTML page that captures a file and sends it to the PHP script for it to upload.

<!--The basic structure of the method begins in the the HTML page-->
<form method="post" action="receive_upload.php" enctype="multipart/form-data">
<!--At this point we define the PHP script that will run the upload-->
    <input type="file" name="take_file" /><br><br>
<!--The button "take_file" will catch the file that you want upload--> 
    <input type="submit" value="Send!" />
<!--The second input will lead the file to the PHP script-->
</form>

That up there is the HTML page, and here below PHP

<?php
if (isset($_POST["take_file"])){
// Folder where the file will be saved
$_UP['folder'] = 'uploads/';
// Maximum file size (in Bytes)
$_UP['size'] = 1024 * 1024 * 50; // 50Mb
// Array with the allowed extensions
$_UP['extensions'] = array('rar', 'jpg', 'png', 'gif', 'pdf', 'zip');
// Rename the file? (If true, the file will be saved as .jpg and a unique name)
$_UP['rename'] = false;
// Array with the PHP upload error types
$_UP['errors'][0] = 'There is no errors';
$_UP['errors'][1] = 'The file in upload it is bigger than the PHP limit';
$_UP['errors'][2] = 'The file exceeds the HTML specified size limit';
$_UP['errors'][3] = 'The upload of the file was partially made';
$_UP['errors'][4] = 'The file upload it was not done';
// Verify if there's any upload error. If yes, show a error mensage
if (isset($_FILES['file']['error']) != 0) {
  die("Could not upload, error:" . $_UP['errors'][isset($_FILES['file']['error'])]);
  exit; // For the script execution
}
// Verify the file extension
$tmp = explode('.', $_FILES['file']['name']);
$extension = strtolower(end($tmp));
if (array_search($extension, $_UP['extensions']) === false) {
  echo "Imcompatible format!";
  exit;
}
// Verify the file size
if ($_UP['size'] < $_FILES['file']['size']) {
  echo "The sended file is too much big, send files until 50Mb.";
  exit;
}
// Verify if it needs to rename the file
if ($_UP['rename'] == true) {
  // Create a current UNIX TIMESTAMP based name and with the .jpg extension
  $final_name = md5(time()).'.jpg';
} else {
  // Keep the original file name
  $final_name = isset($_FILES['file']['name']);
}

// Verify if it's possible to move the file to the chosen folder
if (move_uploaded_file(isset($_FILES['file']['tmp_name']), $_UP['folder'] . $final_name)) {
  // Upload sucessfully, display a mensage and a file link
  echo "Sucessfully upload!<br>";
  echo '<a href="' . $_UP['folder'] . $final_name . '">Click here to access the file</a>';
} else {
  // It wasn't possible to upload, probably the folder it's wrong
  echo "Couldn't send the file, try again";
}
}
else{
    echo "There's something wrong in the code :/";
}

When I boot the system, it is always returning me:

  

There's something wrong in the code: / "

What's wrong? Many thanks.

    
asked by anonymous 10.12.2016 / 17:09

1 answer

2

The variable $_POST["take_file"] does not exist because you set it to the input = file the correct would be this:

if (isset($_FILES["take_file"])){

You are also using the $_FILES['file'] variable in several places, but the expected value is $_FILES["take_file"]

These lines are also wrong, that's not how the isset function is used:

if (isset($_FILES['take_file']['error']) != 0) {
...
$final_name = isset($_FILES['take_file']['name']);
...
if (move_uploaded_file(isset($_FILES['take_file']['tmp_name']), $_UP['folder'] . $final_name)) {

isset checks for the existence of the variable and is not used to compare, do this:

if ($_FILES['take_file']['error'] != 0) {
...
$final_name = $_FILES['take_file']['name'];
....
if (move_uploaded_file($_FILES['take_file']['tmp_name'], $_UP['folder'] . $final_name)) {

Follow the adjusted code:

<?php
if (isset($_FILES["take_file"])) {
    // Folder where the file will be saved
    $_UP['folder']     = 'uploads/';

    // Maximum file size (in Bytes)
    $_UP['size']       = 1024 * 1024 * 50; // 50Mb

    // Array with the allowed extensions
    $_UP['extensions'] = array(
        'rar',
        'jpg',
        'png',
        'gif',
        'pdf',
        'zip'
    );

    // Rename the file? (If true, the file will be saved as .jpg and a unique name)
    $_UP['rename']     = false;

    // Array with the PHP upload error types
    $_UP['errors'][0]  = 'There is no errors';
    $_UP['errors'][1]  = 'The file in upload it is bigger than the PHP limit';
    $_UP['errors'][2]  = 'The file exceeds the HTML specified size limit';
    $_UP['errors'][3]  = 'The upload of the file was partially made';
    $_UP['errors'][4]  = 'The file upload it was not done';

    // Verify if there's any upload error. If yes, show a error mensage
    if ($_FILES['take_file']['error'] != 0) {
        die("Could not upload, error:" . $_UP['errors'][isset($_FILES['take_file']['error'])]);
        exit; // For the script execution
    }

    // Verify the file extension
    $tmp       = explode('.', $_FILES['take_file']['name']);
    $extension = strtolower(end($tmp));
    if (array_search($extension, $_UP['extensions']) === false) {
        echo "Imcompatible format!";
        exit;
    }

    // Verify the file size
    if ($_UP['size'] < $_FILES['take_file']['size']) {
        echo "The sended file is too much big, send files until 50Mb.";
        exit;
    }

    // Verify if it needs to rename the file
    if ($_UP['rename'] == true) {
        // Create a current UNIX TIMESTAMP based name and with the .jpg extension
        $final_name = md5(time()) . '.jpg';
    } else {
        // Keep the original file name
        $final_name = $_FILES['take_file']['name'];
    }

    // Verify if it's possible to move the file to the chosen folder
    if (move_uploaded_file(isset($_FILES['take_file']['tmp_name']), $_UP['folder'] . $final_name)) {
        // Upload sucessfully, display a mensage and a file link
        echo "Sucessfully upload!<br>";
        echo '<a href="' . $_UP['folder'] . $final_name . '">Click here to access the file</a>';
    } else {
        // It wasn't possible to upload, probably the folder it's wrong
        echo "Couldn't send the file, try again";
    }
} else {
    echo "There's something wrong in the code :/";
}
    
10.12.2016 / 17:41