保存图像到指定的文件夹和子文件夹使用PHP? [英] Save Image to the Designated Folder and Subfolder using PHP?

查看:168
本文介绍了保存图像到指定的文件夹和子文件夹使用PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个展现在PHP中的图像的数量PHP。现在我要上传基于图像数这里的不同的文件夹图像下方的PHP来证明我想用来上传的图片图像计数和其他PHP。

第一个PHP:

 < PHP
包括(包括/ config.php文件);
 $上传='上传/';

 在session_start();
$ _SESSION ['用户ID'];

    $ SQL =SELECT * FROM tbl_job其中username ='$用户id';

    $结果= mysqli_query($康恩,$ SQL);
        $行数= mysqli_num_rows($结果);
    $就业=阵列();
    $客户端=阵列();
    $品牌=阵列();
    $周=阵列();
    $ imgCnt = 1;
    $ X = 1;

    回声LIST乔布斯'< BR />'。'< BR />';
    而($行= mysqli_fetch_array($结果)){
        回声图像计数。< BR />';
         回声$ imgCnt。< BR />';
        回声号。< BR />';
        回声$ X'< BR />。
        回声乔布斯。< BR />';
         回声$行['作业']'< BR />'。'< BR />';
         回声客户。< BR />';
         回声$行['客户'。'< BR />'。'< BR />';
         回声品牌。< BR />';
         回声$行['品牌'。'< BR />'。'< BR />';
         回声周刊。< BR />';
         回声$行['周'。'< BR />'。'< BR />';
?>
<形式风格='<?PHP的echo $ imgCnt; ?> ID ='uploadForm-<?PHP的echo $ imgCnt; ?>行动=''方法='POST'>
        <输入类型=文件级=图片< PHP的echo $ imgCnt;​​>中NAME =IMG的onChange =readURL(本); />
        < IMG ID =嗒嗒SRC =#ALT =你的形象/>< BR />< BR />
        <输入类型='按钮'ID ='<?PHP的echo $ imgCnt; ?>类='uploadPicture<?PHP的echo $ imgCnt; ?> BTN BTN-主'值='上传'>
        <! - <输入类型=按钮值=上传级=uploadPictureID =upload_btn< PHP的echo $ imgCnt;​​>有/> - >
< /形式GT;
&所述;脚本的src =htt​​ps://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js>&所述; /脚本>
<脚本>
功能readURL(输入){
        如果(input.files&安培;&安培; input.files [0]){
        VAR读卡器=新的FileReader();
        reader.onload =功能(E){
            //$('#blah').attr('src',e.target.result).WIDTH(300).height(340);
             $(输入)的.next('IMG')ATTR('src'中,e.target.result).WIDTH(300).height(340)。
        };
        reader.readAsDataURL(input.files [0]);
    }

  }
< / SCRIPT>
<脚本类型=文/ JavaScript的>

       $('。uploadPicture<?PHP的echo $ imgCnt;​​?>')解除()点击(函数(E){。

        VAR file_data = $('。图片< PHP的echo $ imgCnt;​​?>')。支撑('文件')[0​​];
        VAR form_data =新FORMDATA();
        form_data.append('文件',file_data);
        VAR edit_id = $(本).attr(ID);
        $阿贾克斯({
                网址:为file.php,
                数据类型:'文字',//什么期望从PHP脚本回来,如果有什么
                缓存:假的,
                的contentType:假的,
                过程数据:假的,
                数据:form_data,
                类型:'后',
                成功:函数(结果){
                        警报(结果)

                }
            });
        });

    < / SCRIPT>
< PHP
回声"------------------------------------------------------------------------------------------------------".'<br/>';
$ X = $ X + 1;
$ imgCnt ++;
}
?&GT;
 

下面是用于上载的图像的PHP的。

 &LT; PHP
包括(包括/ config.php文件);
$ _SESSION ['目标'];
$上传='上传/';

$ IMG = $ _ POST ['$ imgCnt'];

$ SQL =SELECT * FROM tbl_job其中username ='$用户id';

$结果= mysqli_query($康恩,$ SQL);
        $行数= mysqli_num_rows($结果);

        而($行= mysqli_fetch_array($结果)){
            如果($的img = 2){
            $目标= $upload.'/'.$row['Client'].'/'.$row['Brand'].'/'.$row['jobs'].'/'.$row['week'].'/'.$row['store_$c$c'].'/';
         // $ _ SESSION ['目标'] = $的目标;
         如果(!file_exists($目标))
         {
             MKDIR($的目标,0777,真正的);
         }
         $的ImagePath = $upload.'/'.$row['Client'].'/'.$row['Brand'].'/'.$row['jobs'].'/'.$row['week'].'/'.$row['store_$c$c'].'/';


    $ TEMP =爆炸(,$ _FILES [文件] [名称]。);

    $延长=端($ TEMP);

    $文件名= $ _FILES [文件] [tmp_name的值];
    回声$文件名。&LT; BR /&GT;';
    $时间=时间();
    move_uploaded_file($文件名,$的ImagePath $时间$扩展。'。');
   echo文件上传。&LT; BR /&GT;';

   回声$ IMG;

        }

        }
   出口;
 ?&GT;
 

下面附带的是,示出了在第一个PHP的输出页面的屏幕截图。

在图像计数为1我要的照片上传到创建的文件夹,我想要做的就是

客户端/ Brand1 /作业1 /周/存储code / image.jpg的

在图像数为2,我想要的图片上传到创建的文件夹:

客户端/ Brand2 / JOB2 /周/存储code / image.jpg的

图片数,它是从发布第一个PHP其他。我真搞不懂怎么使用这个值来move_uploaded_file到相应的文件夹。

解决方案

javascipt的对要上载的图像作为跟踪并随后它是上传文件的PHP。

 &LT;脚本类型=文/ JavaScript的&GT;
       $('。uploadPicture&LT;?PHP的echo $ imgCnt;​​?&GT;')解除()点击(函数(E){。
        变种形式= $('形式LT;?PHP的echo $ imgCnt;​​?&GT;')[0];
        VAR file_data = $('。图片&LT; PHP的echo $ imgCnt;​​?&GT;')。支撑('文件')[0​​];
        VAR FILE_PATH ='&LT;?PHP的echo $ testpath; ?&GT;';

        VAR form_data =新FORMDATA(表);
        form_data.append('文件',file_data);
        form_data.append(文件路径,FILE_PATH);
        VAR edit_id = $(本).attr(ID);
        $阿贾克斯({
                网址:为file.php,
                数据类型:'文字',//什么期望从PHP脚本回来,如果有什么
                缓存:假的,
                的contentType:假的,
                过程数据:假的,
                数据:form_data,
                类型:'后',
                成功:函数(结果){
                        警报(结果)

                }
            });
        });
    &LT; / SCRIPT&GT;
 

下面是PHP的文件上传到指定的文件夹和子文件夹。

 &LT; PHP
包括(包括/ config.php文件);
 $上传='上传/';


    $的ImagePath = $上传'/'$ _ POST ['文件路径']。
    $ TEMP =爆炸(,$ _FILES [文件] [名称]。);

    $延长=端($ TEMP);

    $文件名[$ i] = $ _FILES [文件] [tmp_name的值];
    回声$文件名[$ i];
    $时间=时间();
    move_uploaded_file($文件名[$ i],$的ImagePath $时间$扩展。'。');


echo文件上传;
   出口;
 ?&GT;
 

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

这篇关于保存图像到指定的文件夹和子文件夹使用PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆