简体   繁体   中英

Save Image to the Designated Folder and Subfolder using PHP?

I have a PHP that show the number of the image in the PHP. Now i want to upload the image based on the Image count to the different folders here is the PHP below to show the Image count and the other PHP that I want to use to upload the image.

First PHP:

<?php  
include('includes/config.php');
 $upload = 'uploads/';

 session_start();
$_SESSION['userid'];

    $sql = "SELECT * FROM tbl_job WHERE username = '$userid' ";

    $result = mysqli_query($conn,$sql);
        $rowcount=mysqli_num_rows($result);
    $job = array();
    $client = array();
    $brand = array();
    $week = array();
    $imgCnt = 1;
    $x = 1;

    echo "LIST OF JOBS ".'<br/>'.'<br/>'; 
    while($row = mysqli_fetch_array($result)){
        echo "Image Count".'<br/>';
         echo "$imgCnt".'<br/>';
        echo "Number".'<br/>';
        echo $x.'<br/>';
        echo "Jobs".'<br/>';
         echo $row['jobs'].'<br/>'.'<br/>'; 
         echo "Clients".'<br/>';
         echo $row['Client'].'<br/>'.'<br/>'; 
         echo "Brands".'<br/>';
         echo $row['Brand'].'<br/>'.'<br/>';
         echo "WEEK".'<br/>';
         echo $row['week'].'<br/>'.'<br/>';
?>
<form style = '<?php echo $imgCnt; ?>' id='uploadForm-<?php echo $imgCnt; ?>' action = '' method = 'POST'>
        <input type="file"  class="image<?php echo $imgCnt; ?>" name="img" onChange="readURL(this);" />
        <img id="blah" src="#" alt="your image" /><br/><br/>
        <input type='button' id = '<?php echo $imgCnt; ?>' class='uploadPicture<?php echo $imgCnt; ?> btn btn-primary' value = 'Upload'>
        <!-- <input type="button" value="upload" class="uploadPicture"  id="upload_btn<?php echo $imgCnt; ?>"/> -->
</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">

       $('.uploadPicture<?php echo $imgCnt; ?>').unbind().click( function(e) { 

        var file_data = $('.image<?php echo $imgCnt; ?>').prop('files')[0];   
        var form_data = new FormData();                  
        form_data.append('file', file_data);
        var edit_id = $(this).attr("id");
        $.ajax({
                url: "file.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>
<?php
echo "------------------------------------------------------------------------------------------------------".'<br/>';
$x = $x + 1;
$imgCnt++;
}
?>

Below is the PHP that is used to upload the image.

<?php
include('includes/config.php');
$_SESSION['target'];
$upload = 'uploads/';

$img = $_POST['$imgCnt'];

$sql = "SELECT * FROM tbl_job WHERE username = '$userid'";

$result = mysqli_query($conn,$sql);
        $rowcount=mysqli_num_rows($result);

        while($row = mysqli_fetch_array($result)){
            if($img = 2){
            $target = $upload.'/'.$row['Client'].'/'.$row['Brand'].'/'.$row['jobs'].'/'.$row['week'].'/'.$row['store_code'].'/';
         //$_SESSION['target'] = $target; 
         if(!file_exists($target))
         {
             mkdir($target,0777,true);
         }       
         $imagePath = $upload.'/'.$row['Client'].'/'.$row['Brand'].'/'.$row['jobs'].'/'.$row['week'].'/'.$row['store_code'].'/';


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

    $extension = end($temp);

    $filename = $_FILES["file"]["tmp_name"];
    echo "$filename".'<br/>';
    $time = time();
    move_uploaded_file($filename, $imagePath . $time . '.' . $extension);    
   echo "File Uploaded".'<br/>';

   echo "$img";

        }

        }
   exit;
 ?>

Attached below is the screenshot of the page that shows the output on the first PHP. 在此处输入图片说明

What I want to do is when the Image Count is 1 i want the picture to be uploaded to the folder created as:

Client/Brand1/Job1/Week/StoreCode/image.jpg

When the image count is 2, i want the picture to be uploaded to the folder created as:

Client/Brand2/Job2/Week/StoreCode/image.jpg

The value of the Image count is posted from first PHP to the other. I just cannot make out how to use this value to move_uploaded_file to the respective folder.

Javascipt for the image to be uploaded is as follow and followed by it is the php to upload the file.

<script type="text/javascript">
       $('.uploadPicture<?php echo $imgCnt; ?>').unbind().click( function(e) { 
        var form = $('.form<?php echo $imgCnt; ?>')[0];
        var file_data = $('.image<?php echo $imgCnt; ?>').prop('files')[0];
        var file_path = '<?php echo $testpath; ?>';

        var form_data = new FormData(form);                  
        form_data.append('file', file_data);
        form_data.append('filepath', file_path);
        var edit_id = $(this).attr("id");
        $.ajax({
                url: "file.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>

Here is the php to upload the file to the designated folders and subfolders.

<?php
include('includes/config.php');
 $upload = 'uploads/';


    $imagePath = $upload.'/'.$_POST['filepath'];
    $temp = explode(".", $_FILES["file"]["name"]);

    $extension = end($temp);

    $filename[$i] = $_FILES["file"]["tmp_name"];
    echo "$filename[$i]";
    $time = time();
    move_uploaded_file($filename[$i], $imagePath . $time . '.' . $extension);    


echo "File Uploaded";
   exit;
 ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM