-1
<?php include('header.php');
include ('config.php');
if(isset($_POST['submit_image'])){
  $imgname=$_FILES["myimage"]["name"] ;
    $target="ProfileImages/";
    $filetarget=$target.$imgname;
    $tempname=$_FILES["myimage"]["tmp_name"];

    $result = move_uploaded_file($tempname,$filetarget);
    if($result){

        $id=$_SESSION['id'];
        $caption=$_POST['caption'];
        $q="INSERT into `images` (id,path,caption) VALUES ('$id','$filetarget','$caption')";
        $res=mysqli_query($con,$q);
        if($res)
        {
            $msg="Photo Uploaded Sucessfully..";
            $_SESSION['msg']=$msg;
            header('location:profile.php');
        }
    }
    else{

        $msg="Error Not Uploaded...Try Again";
            $_SESSION['msg']=$msg;
            header('location:profile.php');
        }
}
?>        
<form method="POST"  enctype="multipart/form-data">
 <h2><u>Select Image</u></h2><br><input type="file" name="myimage"><br>
<h2><u>Caption</u></h2><br>
    <textarea rows="4" cols="25" name="caption"></textarea>
 <input type="submit" name="submit_image" value="Upload">
</form>   

Heading

I was trying to upload pictures through this code, but after some time I checked it was not working...Can any of you help me how to fix this?

saksham7
  • 19
  • 5
  • What part of it is not working? What have you tried? Do the logs give anything? There isn't enough information to help adequately. – Karl Viiburg Mar 24 '18 at 21:34
  • can you add the `html` for the upload `form` ? – Taki Mar 24 '18 at 21:34
  • what done this code ? – Goms Mar 24 '18 at 21:35
  • @Taki Please take a look i've added HTML part now. – saksham7 Mar 24 '18 at 21:56
  • You are wide open to [SQL Injections](http://php.net/manual/en/security.database.sql-injection.php) and should really use [Prepared Statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead of concatenating your queries. Specially since you're not escaping the user inputs at all! – M. Eriksson Mar 24 '18 at 22:00
  • What is actually happening? "Error Not Uploaded...Try Again" or something else? – ino Mar 24 '18 at 22:14
  • Error not Uploaded...it is not moving the files, but the `$filetarget` is valid filename. – saksham7 Mar 24 '18 at 22:37

1 Answers1

0

Turn on the PHP Error Reporting!

Isolate the working script and try to find out where the issue is. Make sure if:

  1. the parser enters the first if
  2. is the $filetarget valid file name
  3. was the file move successful
  4. was the query successful
  5. is the redirection to valid page

If you do not have debugger, disable the header('location:profile.php'); to stay on the page and see errors and/or print testing messages such as echo "I am here on line ".__LINE__; to make sure the parser is in.

ino
  • 2,345
  • 1
  • 15
  • 27
  • No, it is not like that I have a separate table where `id` is the foreign key and I am inserting the multiple images for the single user who is logged in ...Before some time code was working fine...Don't know what happened now?? – saksham7 Mar 24 '18 at 21:54
  • @saksham7 - If you want us to have a chance of figure it out and be able to help you, you need to, at least, check your log files to see if you get any error messages. – M. Eriksson Mar 24 '18 at 21:59
  • Yes i have enabled it in php.ini – saksham7 Mar 24 '18 at 22:12
  • Just to be sure, enable it in the script as well. – ino Mar 24 '18 at 22:13
  • I have updated my original answer with couple of hints. Hope this will help you to find the direction to solve your `it was not working` issue. – ino Mar 24 '18 at 22:24
  • @ino yes, I have figured it out that function `move_uploaded_file()` is not working.. – saksham7 Mar 24 '18 at 22:35
  • Great! So you have found the problem and got it fixed. If not, tell us more. Otherwise do and accept the solution that is best for you to close the question - see [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – ino Mar 26 '18 at 06:09