Reputation: 15
Hello I need to save image name as name_01,name_02 but I tried more than 5 hours I can not out of this please check the code and help me out
if ($typeMessage == 'image') {
$responseMedia = $bot - > getMessageContent($idMessage);
if ($responseMedia - > isSucceeded()) {
// getRawBody() binary
$dataBinary = $responseMedia - > getRawBody(); // return binary
// get file type from header
$fileType = $responseMedia - > getHeader('Content-Type');
if (preg_match('/image/', $fileType)) {
list($fileType, $ext) = explode("/", $fileType);
$ext = ($ext == 'jpeg' || $ext == 'jpg') ? "jpg" : $ext;
if (!file_exists($fileFullSavePath)) {
$counter = 1;
$fileNameSave = 'lamsam_'.$counter.
".".$ext;
else if (file_exists($fileFullSavePath)) {
$counter++;
$fileNameSave = 'lamsam_'.$counter.
".".$ext;
}
}
}
$botDataFolder = 'LAMSAM PAPER/'; // main save file folder
$botDataUserFolder = $botDataFolder.$sourceType.$sourceId; // sub folder= sourceId
if (!file_exists($botDataUserFolder)) { // check if don't have folder sourceId
mkdir($botDataUserFolder, 0777, true);
}
// path
$fileFullSavePath = $botDataUserFolder.
'/'.$fileNameSave;
file_put_contents($fileFullSavePath, $dataBinary);
I expect the output
but the actual is only
Upvotes: 1
Views: 85
Reputation: 15
for($i = 0; $i <= $num_files;) {
$i++;
$fp = fopen('lamsam_' . $i .'.jpg', 'w');
fwrite($fp, $dataBinary);
fclose($fp);
$fp = $fileNameSave;
}
$num_file
is what I use for count file in folder
they show lamsam_1.jpg
it's work for 1st pic
but they do the same lamsam_1.jpg
again
and I have tried
for($i = 0; $i <= 10;)
they show lamsam_1.jpg
, lamsam_2.jpg
... until lamsam_10.jpg
, same image but 10 names
Upvotes: 0
Reputation: 1304
Firstly, we can't see any loop, so $counter
is unlikely to affect anything unless you are loop through it as seen below.
You can open a file (fopen()
), and then write (fwrite()
) the data to it. Once you've wrote the data to it, you need to close (fclose()
) the file:
$fp = fopen('file.txt', 'a+'); //Append mode
// $fp = fopen('file.txt', 'w'); //Write mode
fwrite($fp, $data);
fclose($fp);
fopen()
: https://www.php.net/manual/en/function.fopen.php
fwrite()
: https://php.net/manual/en/function.fwrite.php
fclose()
: https://www.php.net/manual/en/function.fclose.php
In a loop:
This will loop through all the images, and create the naming convention you need:
Start $i
from 1, so $i
should be less than or equal to $noOfFiles
to continue the loop
, obviously, you can use a different approach to achieve the same goal, but ideally, unless you're doing something clever with $_SESSION
's with the counter
, or some clever JavaScript with it, I can't see how you will ever get $counter
to increase.
for($i = 1; $i <= $noOfFiles; $i++) {
$fp = fopen('lamsam_' . $i .'.jpg', 'w');
fwrite($fp, $data[$i]);
fclose($fp);
}
lamsam_1.jpg
, lamsam_2.jpg
etc.
You will need to calculate the $noOfFiles
for the loop, and also have the data for your images in the $data
array.
EDIT IN REPLY TO UPDATE
You need to have all of the image file data, ie, each image, stored in $dataBinary
. So $dataBinary
needs to be an array:
$dataBinary = array(
$image1BinaryData,
$image2BinaryData,
$image3BinaryData,
$image4BinaryData,
$image5BinaryData,
$image6BinaryData,
$image7BinaryData,
$image8BinaryData,
$image9BinaryData
$image10BinaryData
);
So.. if you're getting your images using $responseMedia->getRawBody()
, then you either want it to come as an array (which if you're getting the same image, it seems it's only getting a single image)... or you want to also loop through whatever method you have available to get all the images you are after and add them into the array.
Then in your loop, using $i
as the index of the $dataBinary
array, you can also loop through the images you are adding to each file, rather than just creating 10 files with different names but the same image:
for($i = 0; $i <= $num_files;) {
$i++;
$fp = fopen('lamsam_' . $i .'.jpg', 'w');
fwrite($fp, $dataBinary[$i]);
fclose($fp);
$fp = $fileNameSave;
}
Upvotes: 1