使用PHP将图像保存到指定文件夹和子文件夹?

时间:2015-07-30 04:04:02

标签: javascript php html ajax image

我有一个PHP,显示PHP中的图像编号。现在我想将基于图像计数的图像上传到不同的文件夹,这里是下面的PHP,显示图像计数和我想用来上传图像的其他PHP。

第一个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++;
}
?>

以下是用于上传图片的PHP。

<?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;
 ?>

下面是显示第一个PHP输出的页面的屏幕截图。 enter image description here

我想要做的是当图像计数为1时我想将图片上传到创建的文件夹:

客户端/ Brand1 /作业1 /周/商店代码/ image.jpg的

当图像数为2时,我希望将图片上传到创建的文件夹:

客户端/ Brand2 /作业2 /周/商店代码/ image.jpg的

图像计数的值从第一个PHP发布到另一个。我只是无法弄清楚如何将此值用于move_uploaded_file到相应的文件夹。

1 个答案:

答案 0 :(得分:0)

要上传的图像的Javascipt如下,然后是php上传文件。

<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>

这是将文件上传到指定文件夹和子文件夹的php。

<?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;
 ?>