Form does not execute PHP script

Asked

Viewed 60 times

-1

Good afternoon folks 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>

The one at the top is the HTML page, and down here is 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 run the system, it’s always returning to me:

There’s Something Wrong in the code :/"

What is wrong? Thank you very much.

  • From what I understand, this error message There's something wrong in the code :/" is only shown when the field take_file is not sent. take_file to the archive receive_upload.php. At the beginning of receive_upload.php of a die(var_dump($_POST)). and see what brings in the variables.

1 answer

2


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

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

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

These lines are also wrong, this is not how to use the function isset:

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 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 :/";
}
  • Thank you very much indeed man. You helped me a lot, pity that some denied the question. Forgive me I’m not "'fluent" in PHP, I’m starting in object orientation yet, actually this upload code is there on github.com/zenasalves and I needed to fix it to bring my project together. Thanks!

  • 1

    @Zenas object orientation has nothing to do, OOP should be used if you already understand the language, what you need to learn is if, while, variables, switch, array, constants and identar the code. OOP is something useful, but it is dispensable for simple things, and OOP is nothing without the basics, otherwise do not invent the things in your head or read tutorials in any blog, just follow the documentation http://php.net/docs.php

  • in fact, I think I should practice the basis more, if you could refer me to some project on github or some project of yours, we could work fairly, I don’t know, I like collective programming.

  • 1

    @Zenas first learn the basics I have indicated, then I can refer you to several open-source projects, I myself participate in some. But on the level that this better understand what each thing is first. I sent a pullrequest in your repository.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.