0

I want to create a form to submit an image file in the front-end and send it to the wordpress upload directory and database using php ,but my php says the file is a image but dont upload it :

<div id="recibo">
<form action="" method="post" enctype="multipart/form-data">
<p>upload do recibo:
<input type="file" name="pictures[]" />
<input type="submit" value="Enviar" />
</p>
</form>
</div> 

php:

<?php
$target_dir = "wp-content/uploads/recibos";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
?> 
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
I-am Sam
  • 268
  • 2
  • 13
  • try this for upload a file http://www.trofeosolution.com/index.php/blog/trofeo/1/upload-file-image-php-html – Sundar Jan 07 '16 at 13:23
  • the file i was sending was 15kb but still i get The file exceed the memory limit. File Size - 14.5107421875kb – I-am Sam Jan 07 '16 at 13:27
  • Have you changed the file upload limit in your php.ini? – Jay Blanchard Jan 07 '16 at 13:47
  • 1
    @I-amSam Your question about sending the url to database did not contain any SQL code to support the question, therefore your question could have been voted to close as being too broad or unclear. ***Stack pro tip:*** When posting a question, you need to try something first and seeing that you seem to be using Wordpress, you should use the `wordpress` and `database` tags along with any database code you may have tried and possibly failed. I for one do not know Wordpress. There is surely a forum for it out there, you just need to Google it. Wordpress uses its own database syntax. – Funk Forty Niner Jan 07 '16 at 14:17
  • i can use regular sql insert commands , im tying to do that and if i faild to get it to work ill try a new question ! tks again :) – I-am Sam Jan 07 '16 at 14:23

1 Answers1

0

There are a few things wrong here.

Firstly, the file input's name attribute is name="pictures[]" yet you are using $_FILES["fileToUpload"], those must match.

So rename it: name="fileToUpload[]"

Sidenote: Using brackets implies an array to upload multiple files; you don't need it here since the rest of your code doesn't support multiple files uploading.

Change it to name="fileToUpload"

Then your folder path:

$target_dir = "wp-content/uploads/recibos";

You have no directory seperator after recibos

This would translate to recibosFILE.JPG rather than recibos/FILE.JPG.

Change it to:

$target_dir = "wp-content/uploads/recibos/";

Make sure the folder you are uploading to has the correct permissions to write to it.

Another thing: I can't see how if(isset($_POST["submit"])) {...} would work here, since your submit button does not have the name attribute to match it with.

<input type="submit" value="Enviar" /> to <input name="submit" type="submit" value="Enviar" />

Special note:

If you do want to upload multiple files, then you will need to find yourself another script to do that and this isn't what your question is about.

See also:

to increase upload size

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • i changed my code and it works ..but now i need to send it to database (please check my edited question – I-am Sam Jan 07 '16 at 13:57
  • 1
    @I-amSam Great, but you can't go changing your question like that. That's a totally different animal here. If you have a question about database, post a new question. – Funk Forty Niner Jan 07 '16 at 13:59
  • @I-amSam I did a rollback to another revision. Again, if you have another code problem, ask a new question. Don't modify your original post, the one that I originally answered to. – Funk Forty Niner Jan 07 '16 at 14:00
  • @I-amSam You're welcome and *Welcome to Stack*. Feel free to post a new question and I'm sure that either I or someone else will be more than happy to have a look at it, *cheers* – Funk Forty Niner Jan 07 '16 at 14:07