Problem uploading files

Asked

Viewed 400 times

1

I have a code pad to do upload but every time I try to do the upload appears a message saying "Please send files with the following extensions: jpg, png or gif". I am trying to do the upload file docx and I’ve specified that:

$_UP['extensoes'] = array('docx', 'xlsx', 'pdf', 'jpg', 'png', 'gif', 'jpeg' );

Why the message appears even though I have "released" other extensions?

$extensao = @strtolower(end(explode('.', $_FILES['arquivoJuridico']['name'])));
if (array_search($extensao, $_UP['extensoes']) === false) {
   echo "Por favor, envie arquivos com as seguintes extensões: jpg, png ou gif";
}

1 answer

1

I took a test and it worked:

$_UP['extensoes'] = array('docx', 'xlsx', 'pdf', 'jpg', 'png', 'gif', 'jpeg' );
$extensao = @strtolower(end(explode('.', "nome.doc")));
if (array_search($extensao, $_UP['extensoes']) === false) {
   echo "Por favor, envie arquivos com as seguintes extensões: jpg, png ou gif";
}
$extensao = @strtolower(end(explode('.', "nome.docx")));
if (array_search($extensao, $_UP['extensoes']) === false) {
   echo "Por favor, envie arquivos com as seguintes extensões: jpg, png ou gif";
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Other than this you have some problem that it is not possible to identify by what you posted in the question. One possibility is that the file name is not coming in correctly $_FILES['arquivoJuridico']['name'].

Do not compare directly with true or false in a place waiting for a boolean expression.

Another thing, when you put one @ to run something, you probably know it gives an error. The @ just hides the error, does not make the execution correct.

  • It’s just that if I don’t put @ the system fails for no reason! I took this code from a website and it already came with this direct comparison with false.

  • Does not exist for no reason in programming. There is an error there. And it is probably this error that is causing the next error. The @ He just switched places and made it harder to find the cause. That’s what I said, unless you have a very strong reason and only those who have a lot of experience know how to identify a very strong reason, the @ error omission never should be used. Print the contents of $_FILES['arquivoJuridico']['name'] preferably with guards in the string: echo "|" . $_FILES['arquivoJuridico']['name'] . "|"; to see if there is a filename there. And post the error.

  • 1

    $extension = @strtolower(end(explode('.', $_FILES['fileJuridic']['name'])); if (array_search($extension, $_UP['spellers']) === false) { echo "Please send files with the following extensions: jpg, png or gif"; } When shooting @: Strict standards: Only variables should be passed by Reference in C: Program Files (x86) Vertrigoserv www marco php reclamatorias zanellaP1.php on line 700 Strict standards: Only variables should be passed by Reference in C: Program Files (x86) Vertrigoserv www marco php reclamatorias zanellaP1.php on line 700

  • Edit your question to put additional information.

  • @Gustavosevero, The error of @ is easy to arrange, just you take the return of explode and play in a variable and then call strtolower() e end(). Like this: $arr = explode('.', $_FILES['arquivoJuridico']['name']);$extensao = strtolower(end($arr)); more details on this question

  • So? $extension = strtolower(end(explodes('. ', $_FILES['archivedJuridic']['name'])); $arr = explodes('. ', $_FILES['fileJuridic']['name']); $extension = strtolower(end($arr));

  • @I’m trying to reproduce the mistake and I can’t. http://ideone.com/IqWiW6. It is exactly your code the only thing I have done differently is initialize the $FILES on hand since I cannot receive the data from outside and even if I could, it would be difficult to reproduce exactly the same situation. I believe your problem is not of code but of environment.

  • @bigown, you can simulate the Strict Standards... in ideon adding 2 lines at the beginning, know there it seems that it has some setting to not display warnings, example with warnings

  • Simulating the Strict Standards?

  • 2

    I discovered my problem... I was forgetting the "enctype="Multipart/form-data"

  • 2

    As I was saying the problem is not in this presented code.

Show 6 more comments

Browser other questions tagged

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