I want to save a same file double time at 2 pages according to my requirement. First I try with session but nothing happened. And now I prefer to work with cache but still nothing happen.
actionOne()
{
$uploadObj= UploadedFile::getInstance($model, 'file');
$FileName = $uploadObj->name;
$url = 'uploads/' . $FileName ;
$uploadObj->saveAs($url,false); //it's work
$cache = Yii::$app->cache;
$cache->add('TestKey',$uploadObj,5);
// other codes below to go to 'two'
}
actionTwo()
{
$cache = Yii::$app->cache;
$uploadObj=$cache->get('TestKey');
$uploadObj->saveAs('uploads/new.file'); // this not work and no err
}
| ---------------- | ---
| Yii version | 2.0.12?
| PHP version | 7.14
| Operating system | Mac
This is a PHP feature. Please visit PHP doc: POST method uploads
The file will be deleted from the temporary directory at the end of the request if it has not been moved away or renamed.
already read it. I don't know what I have to do with it. It's about getting info of uploaded file. I have checked the info of uploaded file with var_dump($uploadedobj).
I still need more exact way.
@KaungZawHtet The file $uploadObj->tempName deleted after actionOne() return. So, you don't save the deleted file in actionTwo(). This is a PHP feature.
Because actionOne() and actionTwo() are different requests.
Is there any good way that keep the uploaded file at storage within a time and delete it after that amount of time? Like cache concept that keep at storage instead of on RAM.
Suppose,
$cache = Yii::$app->cache;
$cache->add('TestKey',$uploadObj,5);
This code keep the cache on RAM for 5 seconds.After that, there will be automatically deleting. I want the way that work with storage instead of RAM.
You can call this "Automatic file deletion after a period". How can I get this?
If I know that way, I think I can relinquish the previous way.
@KaungZawHtet $cache store the $uploadObj object data, not store $uploadObj->tempName file (such as /tmp/Haj2Jsbw on linux) data.
You can get a "/tmp/Haj2Jsbw" string from the cached $uploadObj object data. But, the file /tmp/Haj2Jsbw is deleted on new request.
I will inform you back after reading some sources and making researches over this. For now, thx for your reply.
@KaungZawHtet
public function actionOne()
{
$uploadObj = UploadedFile::getInstance($model, 'file');
$FileName = $uploadObj->tempName;
$data = file_get_content($FileName);
$cache = Yii::$app->cache;
$cache->add('TestKey', $data, 5);
}
public function actionTwo()
{
$cache = Yii::$app->cache;
$data = $cache->get('TestKey');
file_put_content('uploads/new.file', $data);
}
@KaungZawHtet You could save base64 file data to session.
@larryli
wowwww!!! You made my day. Your code work!!!!!!!
Thx for your help. I now know more about how php handles uploaded files.
@FabrizioCaldarelli
Thx for your good suggestion. I can use your idea in near future.
Thank you for your question.
In order for this issue tracker to be effective, it should only contain bug reports and feature requests.
We advise you to use our community driven resources:
If you are confident that there is a bug in the framework, feel free to provide information on how to reproduce it. This issue will be closed for now.
_This is an automated comment, triggered by adding the label question._
Most helpful comment
@KaungZawHtet You could save base64 file data to session.