gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:05:56 26 4
gpt4 key购买 nike

我有一个 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 时,我希望将图片上传到创建的文件夹:

客户/品牌1/作业1/周/StoreCode/image.jpg

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

客户/Brand2/Job2/Week/StoreCode/image.jpg

图像计数的值从第一个 PHP 发布到另一个 PHP。我只是不知道如何使用这个值将 move_uploaded_file 移动到相应的文件夹。

最佳答案

上传图片的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;
?>

关于javascript - 使用PHP将图像保存到指定文件夹和子文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31715001/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com