0

Have got 3 database table as shown below:

Table 1:
id    username    password
D1    12345       12345
D2    123456      123456

Table 2:
username    code    address    chain
12345       123     ASD        WCL
12345       1234    QWE        PNS

Table 3:
code    jobs    week    client    brand
123     J1      WK01    C1        B1
123     J2      WK01    C1        B2
123     J3      Wk01    C2        B3

When the user logins using username and password, the code related to that specific username is displayed and other than that the jobs related to that specific code as well all together. the query used to display the output is as below:

$sql = "SELECT * FROM tbl_store INNER JOIN tbl_job ON tbl_store.store_code = tbl_job.store_code WHERE username = '$userid'";

There is an HTML form at the end of each block as show in figure below: enter image description here

I am able to get the preview of the image as well. rest of the codes is all below. PHP.

<form enctype="multipart/form-data" id="form" action="" method="post">
        <input type="file"  id="image" name="img" onChange="readURL(this);" />
        <img id="blah" src="#" alt="your image" /><br/><br/>
        <input type="button" value="upload" onclick="javascript:uploadInage();" />
    </form>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script>

function readURL(input) {      
        if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function (e) {
            //$('#blah').attr('src', e.target.result).width(300).height(340);
             $(input).next('img').attr('src', e.target.result).width(300).height(340);
        };
        reader.readAsDataURL(input.files[0]);
    }

  }
</script>


<script type="text/javascript">
        function uploadInage()
        {
        var file_data = $('#image').prop('files')[0];   
        var form_data = new FormData();                  
        form_data.append('file', file_data);
            $.ajax({
                url: "preview1.php",
                dataType: 'text',  // what to expect back from the PHP script, if anything
                cache: false,
                contentType: false,
                processData: false,
                data: form_data,                         
                type: 'post',
                success: function (result) {
                        alert(result)

                }
            });
                </script>

the code above is to preview the image and upload it to the server. file.php:

<?php

    $imagePath = "uploads/";
    $temp = explode(".", $_FILES["file"]["name"]);

    $extension = end($temp);

    $filename = $_FILES["file"]["tmp_name"];
    echo "$filename";
    $time = time();
    move_uploaded_file($filename, $imagePath . $time . '.' . $extension);    
   echo "File Uploade";
   exit;
 ?>

The problem I am facing is to store the image to the folder and subfolders: For example: I want the image to be uploaded to the folder and sub folder in this pattern:

Client/Brand/J1/Week/Code/Client_Brand_J1_Week_Code.jpg

This is the code that I want to add to the SESSION.

while($row = mysqli_fetch_array($result)){

$target = $upload.'/'.$row['Client'].'/'.$row['Brand'].'/'.$row['jobs'].'/'.$row['store_code'].'/';
         $_SESSION[] = $target; 
}

How to achieve this?

Puttaa
  • 164
  • 8

1 Answers1

0

it's in your call to move_uploaded_file. You have an $imagePath variable that will need to be manipulated with your content. You just need to use some string manipulation to build the path, then save that in your $imagePath variable.

swatkins
  • 13,530
  • 4
  • 46
  • 78
  • Whats if I store the values in the SESSION and use that variable to determine the '$imagepath'.. But is it possible to store more than one value in SESSION and if yes How to retrieve the values from that.. – Puttaa Jul 28 '15 at 05:08
  • you can store an array in the session. $_SESSION['user'] = array('key0' => $var0, 'key1' => $var1, 'key2' => $var2); // extraction : $_SESSION[user']['key2']; http://stackoverflow.com/questions/30772402/is-php-session-unchangeable-from-user-end/30772766#30772766 – Brian Jul 28 '15 at 05:13
  • @Brian Please see above i have updated a bit of code. Kindly let me know how to add it to the SESSION.. – Puttaa Jul 28 '15 at 06:44