John Davidson

php - generate two files with different extensions with the same unique id

0 comments
Message:


I believe it's a matter of logic but I can't solve it.
I have a code that converts any video file to mp4, but at the moment I'm only converting to .gif. I wanted to know how I can generate a .mp4 and a .gif file with the same unique id.


I tried something like this


$finalFilePath = $targetDir . uniqid() . ".gif" ".mp4";

However, I was not successful.
It is only one file generator
In this case a uniqueid.gif file that is actually mp4.
and the intention is to generate both with the same unique id.


Thank you very much in advance.
Any help is welcome.


update more content to ask --->
Hello, using this code below I can generate the 2 files, but the problem is, two files are generated but both should have the same uniqid. however they are generated with different names, I'm having difficulties in creating a variable that stores only one uniqid this is my big problem.


 if(move_uploaded_file($videoData["tmp_name"], $tempFilePath)) {

$finalFilePath = $targetDir . uniqid() . ".gif" ;
$finalFilePath2 = $targetDir . uniqid() . ".mp4";


if(!$this->insertVideoData($videoUploadData, $finalFilePath)) {
echo "Insert query failed\n";
return false;
}

if(!$this->convertVideoToMp4($tempFilePath, $finalFilePath2)) {
echo "Upload failed\n";
return false;
}

Function Upload.


public function upload($videoUploadData) {

$targetDir = "uploads/videos/";
$videoData = $videoUploadData->videoDataArray;

$tempFilePath = $targetDir . uniqid() . basename($videoData["name"]);
$tempFilePath = str_replace(" ", "_", $tempFilePath);

$isValidData = $this->processData($videoData, $tempFilePath);

if(!$isValidData) {
return false;
}

if(move_uploaded_file($videoData["tmp_name"], $tempFilePath)) {
$finalFilePath = $targetDir . uniqid() . ".gif";

if(!$this->insertVideoData($videoUploadData, $finalFilePath)) {
echo "Insert query failed\n";
return false;
}


return true;

}
}



Function ConvertVideoToMp4


public function convertVideoToMp4($tempFilePath, $finalFilePath) {
$cmd = "$this->ffmpegPath -i $tempFilePath -pix_fmt rgb24 -r 4 -vf scale=540:-1 $finalFilePath 2>&1";

$outputLog = array();
exec($cmd, $outputLog, $returnCode);

if($returnCode != 0) {
//Command failed
foreach($outputLog as $line) {
echo $line . "<br>";
}
return false;
}

return true;
}




My Answer:
'.gif';
$finalFilePath2 = $targetDir . uniqid() . '.mp4';

This code will generate two unique file paths with the same unique id, one ending in .gif and the other ending in .mp4. You can then use these file paths to save your converted files.

Rate this post

3 of 5 based on 8188 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us