1

I'm trying to upload an image to a directory, but there is an issue with the form. When the form is submitted it is redirected to the php file specified in the form action, but then it seems to be unaware of the form being submitted.

HTML

<html>
<form action="profileimg.php" method="post" enctype="multipart/form-data">
<label for="chooseimg" class="profile-pic-change"><i class="material-icons" style="color: rgb(255, 255, 255);">add_a_photo</i></label>
<input type="file" name="image" accept="image/gif, image/jpeg, image/png" id="chooseimg" value="Image" onchange="javascript:this.form.submit();" />
</form>
</html>

(I've also tried using a regular submit button; no difference.)

PHP

<?php
include 'connect.php';
$target_dir = "Users/";

$name = $_FILES["image"]["name"];
$name = md5($name);
$name = $name . uniqid($name);
$path = $_FILES["image"]["name"];
$ext = pathinfo($path, PATHINFO_EXTENSION);
$name = $name.".".$ext;

$target_file = $target_dir . $name;
if(isset($_POST['submit']))
{

move_uploaded_file($_FILES["image"]["tmp_name"], $target_file);
$SQL = "INSERT INTO Users (image) VALUES ('$target_file')";
$result = mysql_query($SQL);
if( $result )
{
    echo "<script type='text/javascript'>alert('success!')</script>";
}
else
{
    echo "<script type='text/javascript'>alert('failed!')</script>";
}
}
else
{
echo 'didnt submit';
}
?>

I added the didn't submit echo after getting neither of the alerts to see if that was the result, and that is all I get. It redirects to that page and just displays didn't submit. Any thoughts as to why that may be?

Thanks in advance.

Codedstuff
  • 179
  • 1
  • 3
  • 11
  • where's the input for this? `if(isset($_POST['submit']))` or the JS. and why aren't you checking for the real errors? – Funk Forty Niner Jan 06 '16 at 18:07
  • Consult these following links http://php.net/manual/en/function.mysql-error.php and http://php.net/manual/en/function.error-reporting.php and apply that to your code, then come back and tell what they are, if any. – Funk Forty Niner Jan 06 '16 at 18:08
  • input is the file, and submit should happen on file select, however I have also tried with a submit button. Neither worked. Have attempted to apply those error reporting functions to my code but it's not given me anything other than an internal server error – Codedstuff Jan 06 '16 at 18:27
  • also, could you please explain the similarity between my question and the one you've linked as a duplicate? I'm probably being a bit slow, but I don't see it – Codedstuff Jan 06 '16 at 18:29
  • given the answer below, they'll have to adjust it, now won't they? You need to show more code and as to "how" that conditional statement is related here. If you're getting an internal server error (as seen in a comment you left below) then check your logs and use error reporting. Your question doesn't support that error; not for what you posted anyway. – Funk Forty Niner Jan 06 '16 at 18:31
  • sorry, I'm even more lost than I was before haha. What is it they'll have to adjust? That is the error I'm getting regardless. There's something wrong with the post. I've tried with the javascript method which I would like to use, but I've also tried with a regular submit button and neither give the correct response. There's nothing missing from the form that I can see, hence my confusion. – Codedstuff Jan 06 '16 at 18:40
  • Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Jan 06 '16 at 18:43
  • Have done so, given nothing but a blank page. as for the js I got it from this question and should be all included http://stackoverflow.com/questions/7321855/how-do-i-auto-submit-an-upload-form-when-a-file-is-selected – Codedstuff Jan 06 '16 at 19:04
  • I'm now trying with `onChange="form.submit()"` (5th answer down) – Codedstuff Jan 06 '16 at 19:06

1 Answers1

0

You don't have submit key in $_POST array. Use regular submit button with name="submit"

<input type="submit" name="submit" />
Bart
  • 1,268
  • 2
  • 12
  • 14
  • Just tried that again, got an internal server error – Codedstuff Jan 06 '16 at 18:24
  • @Codedstuff The `if(isset($_POST['submit']))` is looking for a submit button here of the same name which isn't present in your question / if there is JS/Ajax, included it in your question. This being an addendum to my last comment up there. – Funk Forty Niner Jan 06 '16 at 18:43