0

I have uploads folder and inside there ,dynamically, I am creating another directory based on the image id.

I'm creating the directory but the image is not being saved in it! At the same time I want to store the path of the folder in the database.

if(isset($_POST['submit'])){

   $img_id=$_POST['img_id']; 

 /*  $imgpath= mkdir("uploads/".$img_id);
   $path1= "$imgpath/".$_FILES['c_photo']['name'][0];      
   move_uploaded_file($_FILES['c_photo']['name'][0], $path1);    
    $img_photo=$_FILES['c_photo']['name'][0]; 
*/

// This code is saving the image in uploads folder instead off $imgpath directory

    $imgpath=mkdir("uploads/".$img_id);
   $path1="uploads/".$_FILES['c_photo']['name'][0];  
   move_uploaded_file($_FILES['c_photo']['tmp_name'][0],$path1);


    $query1 ="INSERT INTO img_tbl(`img_id`,`img_photo`) VALUES ('$img_id', '$path1')";


    if ($conn->query($query1) === TRUE) {

        header("location:show_img.php?");

    }
    else {
    echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $conn->close(); 


}


?>

<div class="col-sm-12">                                        
    <div class="col-sm-3">
     <label>Image id</label>
    </div>
    <div class="col-sm-9">
       <input class="form-control" type="text" name="img_id""/>
    </div>
</div>
<div class="col-sm-12">
   <div class="col-sm-3">
      <label>Upload photo</label>
   </div>
 <div class="col-sm-9">
    <input class="form-control" type="file" name="c_photo[]" />
 </div>
</div>


<div class="col-sm-5">
  <input class="form-control" type="submit" name="submit" value="Save"/>
</div>

Please help me. I am new in php. Thanks in Advance

sudha
  • 1
  • 1
  • 5

3 Answers3

1

The move_uploaded_file() needs a file name and a destination. I guess you 2. parameter is not good. You can read more about it here: PHP move-uploaded-file

Try this:

$folderPath = "uploads/".$img_id;
$isimgpath= mkdir(folderPath); 

if(isimgpath){
    move_uploaded_file($_FILES['c_photo']['tmp_name'], $folderPath);
}

I guess your DB insert may work, but it fails before it and that's why it is not in. You can save image path to db. You create a "$path1" value, put it in the DB Insert. As i see it contains a image path with the name of it.

$query1 ="INSERT INTO img_tbl(`img_id`,`img_photo`) VALUES ('$img_id', ". $path1 .")";

Try to modify the input as well like this:

<input class="form-control" type="file" name="c_photo" />
DiabloSteve
  • 431
  • 2
  • 13
  • image are not moving inside the dynamically created folder..please help me out – sudha Feb 14 '18 at 09:20
  • I edited my text, check it. Try to echo the "$imgpath" and "$_FILES['c_photo']['tmp_name']" somewhere. It these values are null, then it is another reason for your issue. – DiabloSteve Feb 14 '18 at 09:29
  • Oh, yep. The mkdir return with true, or false (1 or 0). This is the problem for move_uploaded_file. I modified the example. – DiabloSteve Feb 14 '18 at 12:32
  • **folderpath** directory is created outside of uploads folder and inside the **uploads** one file with img_id name created(but its not folder..it is file) and image inside it. – sudha Feb 15 '18 at 05:16
  • Its very minor mistake ..its creating **file** not folder **inside the uploads folder**.Please give some time to resolve my issue – sudha Feb 15 '18 at 05:22
0

you have an issue with the uploading code it should be tmp_name which provide temporary store path from the server

move_uploaded_file($_FILES['c_photo']['tmp_name'][0], $path1);

Turn on your error reporting to display errors How do I get PHP errors to display?

Umar Majeed
  • 313
  • 3
  • 15
0

Assuming that the form has the correct enctype attribute ( multipart/form-data ) and you are uploading a single file as opposed to using a loop to upload multiple files then perhaps the following might help a little ( not tested though )

<?php

    if( isset( $_POST['submit'], $_POST['img_id'] ) && !empty( $_FILES['c_photo'] ) ){

        /* for convenience, convert to an object to use Object notation */
        $obj=(object)$_FILES['c_photo'];
        $tmp=$obj->tmp_name;
        $name=$obj->name;
        $error=$obj->error;

        $img_id=$_POST['img_id']; /* peculiar knowing the ID before inserting into the database?? */

        $savepath=__DIR__ . '/uploads/' . $img_id;

        mkdir( $savepath, 0644 );/* create directory & set directory perms */

        $path = $savepath.'/'.$name; 

        $result = is_uploaded_file( $tmp ) & move_uploaded_file( $tmp, $path );
        if( $result ){
            $query ="INSERT INTO img_tbl( `img_id`, `img_photo` ) VALUES ('$img_id', '$name')";
            $result=$conn->query( $query );

            if( $result ){
                header( "location:show_img.php?img_id=".$img.id );
            } else {
                echo "Error: ";
            }
        }

        $conn->close(); 


    }
?>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46