i am trying to upload a video and convert it in php using zend framework and i have a bit of a problem with my logic.
i have a directory, locally, that needs to hold the uploaded files:
C:/xampp/htdocs/zend/videos/
fist i need to convert the video then move it in that directory.
For conversion im using something like this:
exec("ffmpeg -i video.avi -ar 22050 -ab 32 -f flv -s 320x240 video.flv", $out);
here is a part of my form:
$file = new Zend_Form_Element_File('file');
$file->setLabel('File')
->setRequired(true)
//->setDestination('/var/www/tmp') use in real life
->setDestination('C:/xampp/htdocs/zend/tmp')
->addValidator('Size',
false,
array('min' => '10kB', 'max' => '100MB'));
when i upload the file it goes to that directory just fine.
Do i need to convert the file in the tmp
directory and then move it to my other main one, then delete the original one?
Isn't there a way to hold the original file in a temp directory temporary until it gets converted and then it will automatically delete itself?
I am trying also to use Zend_File_Transfer_Adapter_Http
but im a bit confuse on what is the difference in between setDestination
and the target
from the Rename
filter and if there is a need to use it.
i home someone could bring some light into this issue, maybe some best practices.
Thanks
I am trying also to use Zend_File_Transfer_Adapter_Http but im a bit confuse on what is the difference in between setDestination and the target from the Rename filter and if there is a need to use it.
setDestination()
is deprecated, and the Rename
filter should be used instead. If you have a Rename
filter set, it will override the destination. Otherwise, there isn't much of a difference. The main difference is that the Rename
filter can rename the file, where setDestination
keeps the original file name as it was on the user's PC and only moves it to the destination. This could lead to existing files being overwritten or someone smashing another user's file if you don't carefully name them.
Do i need to convert the file in the tmp directory and then move it to my other main one, then delete the original one?
Personally, I wouldn't try to convert the file as it is uploaded. While the conversion may be quick in many cases, what happens if a relatively large video was uploaded and it takes an additional 4 minutes to encode the video? You don't want the user hanging there waiting wondering what is happening while the conversion takes place.
I would upload the files, copy them to a temporary location where they will be converted and then moved to the original location. This adds complexity of course.
If you want to convert the videos on the fly as they are uploaded, I may not even bother using Zend_File_Transfer_Adapter_Http
at all. Instead, use the $_FILES
superglobal to access $_FILES['videofile']['tmp_name']
and have ffmpeg
convert that file and just write the output to the final destination, or a temp destination and then move it when conversion completes. Files uploaded to PHP are deleted automatically when the request terminates unless you call move_uploaded_file
or manually do something to write the temporary file to a permanent location.
Isn't there a way to hold the original file in a temp directory temporary until it gets converted and then it will automatically delete itself?
Yes, if you go without Zend_File_Transfer
. The uploaded file is automatically in a temporary directory until the PHP request terminates. If you do the conversion on the fly, the original temp file is automatically deleted when PHP finishes.
Hopefully that clears things up a bit for you.