$_FILES does not work

Asked

Viewed 381 times

3

EDIT: Even if you put the ENCTYPE tag in the form, the problem persists. I’m trying to use the $_FILES for the first time, and I’m having a hard time. It’s like he’s getting nothing. Look at the code I’m using.

HTML:

<form id="formtiles" method="post" enctype="multipart/form-data" action="scripts/cadastro.php" >
    <label for="logo"><b>Logotipo: </b></label>
    <input name="logo" type="file" id="logo" data-mini="true"/>
    <input type="submit" form="formtiles" value="Gravar"  id="enviadadoscliente" data-inline="true" data-icon="check"/>
</form>

PHP:

$location = 'public_html/teste/grid/img';
if (isset($_FILES['logo'])) {
    $name = $_FILES['logo']['name'];
    $tmp_name = $_FILES['logo']['tmp_name'];

    $error = $_FILES['logo']['error'];
    if ($error !== UPLOAD_ERR_OK) {
        echo 'Erro ao fazer o upload:', $error;
    } elseif (move_uploaded_file($tmp_name, $location . $name)) {
        echo 'Uploaded';    
    }
} 

Thanks in advance for the help.

  • 3

    For $_FILES to work, in principle, its form must contain: <form enctype= "Multipart/form-data">

1 answer

6


Tag form: entype="multipart/form-data"

EDIT:

Probably the target folder of the image (public_html/teste/grid/img) is wrong, you should access it respecting the hierarchy of folders example, your php file is in the folder: public_html/teste/php

Then your way to the briefcase img must be:

../grid/img/

I am the file php, I’m in the briefcase php, and my path is a folder behind mine, so I have to go back a folder (with ../) to see the folder grid and consequently the img.

Tip:

I suggest using the function time() to generate image names, because if the user uploads two images with the same name may occur an error, change the variable $name for:

$name = time().".jpg";

Download link from the example: MEGA NZ

  • Good afternoon, actually I put this tag also in the form, but it was right after my post. It still didn’t work. Do you have any other suggestion?

  • He’s probably not found the folder public_html/teste/grid/img you should do as HTML and CSS paths, go back to folders with ../ until reaching a degree that can access the img folder. I will put in the answer.

  • That’s not it either. The problem is that $_FILES is not getting anything. It doesn’t even get into if (isset($_FILES['logo']))

  • Error appears?

  • Unfortunately nothing appears. Only $_FILES value comes empty.

  • It gives a verified in the paths, certainly the error is this, I made a test here, just copied and pasted the code and changed the path according to my folder structure... Check whether the submit of form is actually going to the archive cadastro.php. Then check the path of the variable $location (Now that’s a little squiggly)

  • the form file is in which folder? Another problem I noticed was the absence of / at the end of the variable $location so it is not writing the file inside the folder but a file called img+nome-da-imagem in the briefcase grid

  • Indeed the path was wrong yes. But the error persists. Even all Posts work and receive values, only with $_FILES does not work.

  • I put in the reply a download link of an example perfectly equal to your code, here it worked all ok.. If it doesn’t work there, the problem is the software used to simulate the server. If it works, analyze the code and adapt it to yours. Sometimes he can display a Warning saying that it cannot receive file per parameter, but it is only one Warning, nothing obstructing the functioning of the code.

  • 1

    Leonardo, I figured out what the problem is. The point is that I’m using Jquery Mobile, and it embeds ajax into the Formulars. This generates error in uploading files, so to work I had to put the data-ajax="false" tag in the form. But I only discovered this after analyzing the code Voce attached and comparing it with mine. Thank you very much for the support!

Show 5 more comments

Browser other questions tagged

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