How do i convert videos uploaded by user to .swf? - php

I am creating a website which enables to upload videos. But we know that user can upload any kind of video but the browser can't play them. So, I thought that I somehow can convert them into .swf and play them using a flash player. I tried to use ffmpeg-php but it didn't worked. my code was:
shell_exec('ffmpeg -i in.mp4 out.swf');
It does not show any error neither it returns out.swf.
Please! help me.

Things to check:
Does the in.mp4 file actually exist on the server and is it in the
correct temporary folder?
Check that file size limits (upload_max_filesize PHP setting) are
not being hit.
Does the ffmpeg command work properly from a shell promt as the
user that the webserver is running as? (this test should be
performed in the folder where you are processing the file) Use the
verbose logging options to check for errors
Capture all the output from the shell_exec and check for errors.
Try using system or exec where you can also capture the return
value of ffmpeg which can then be used for error checking
Finally, I would specify actual directory names for the input and output files. This will narrow down problems as you know exactly what folder to look in for results. You can also adjust permissions if required

Related

Getting images onto server from separate web host

i have GoDaddy shared webspace with FTP access that contains a folder with images. These images are changing every day.
Im looking for advice on what i need to do to get these images onto my server in the workplace, maybe every hour or so. The server doesnt have IIS installed is there any other way to do this?
Am i able to do a PHP script that can put all the images onto the server using the ip or something?
I had the same issue as I had two images on a remote server which I needed to copy to my local server at a predefined time each day and this is the code I was able to come up with...
try {
if(#copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) {
} else {
$errors = error_get_last();
throw new Exception($errors['type'] . " - " . $errors['message']);
};
} catch(Exception $e) {
//An Error Occurred Downloading The Requested Image
//Place exception handling code here
};
I then created a cron job which ran this file on a daily basis but you would be able to run is as frequently as you needed to based on how frequently the source image changes on the source server.
A few points to explain the code...
As the code is run in the background I use the # symbol to silence visual references to any errors which occur with the copy command as they won't be viewable anyway. Instead if copy results in an error it returns false and triggers throwing a new exception which takes the error type and error message and throws a new exception for it which can then be handled in the catch block and can be actioned however you need such as making a local log entry, sending an error email, whatever you need.
As the second paramater in the copy command you will see that the path results in the filename being named based on the current date. This name can be anything but needs to be unique in the destination for it to work. If you plan on overwritting the image each time the copy is done then you can statically code the name as the same name but if you want to maintain a history then you would need to come up with a file naming solution on your local server to ensure that the files don't get overwritten each time the cron runs.
The first parameter of the copy statement has been written on the assumption that the source image file is named the same each time, if the name changes the you will need to identify how the naming is achieved and code a variable to build that name and insert it as the source filename.
This code does not alter the format of the source image file so to ensure no corruption occurs and the image can still be shown after the copy you need to ensure that the source image file and the local copy of the image file have the same file extensions, so if the source image file is a .gif file then you need to make sure the file extension in the second copy parameter is also set to .gif
I'll try to answer here instead of continuing the comment-spam ;)
you got your webspace with FTP access. let's just call it webspace;
then you got your server at your workplace. let's just call it workplace;
after all you need one server (also can be webspace for example) where you are able to run PHP. let's call it php-server;
step 1
at the workplace setup a FTP server, for example FileZilla. you setup your FTP server so that you can connect to it. so make an account and set it up so you can access the folder(s) where you want to save your images. also make sure you got access from outside your workplace - firewall settings etc.
step 2
if you can run your PHP scripts on your webspace this would be the easiest way. you can directly access the image files, establish a connection to your FTP server at workplace and upload the files.
if the PHP server is somewhere else, you have to establish a connection from the php-server to your webspace; download your files to your php-server; upload your files to the FTP server at your workplace.
will be a bit of work as you can see, but should be possible to do.
Create a .bat file that uses the ftp command line functions to get the data you want from the server. Save that .bat file somewhere and create a Scheduled Task to run the script every hour.
You can even store the actual sequence of ftp commands in a separate file (e.g. ftpcmd.dat) and call them from the script
ftp -n -s:ftpcmd.dat SERVERNAME.COM
Example ftp command file:
user MyUserName
Password
bin
cd \your\path\images
mget * C:\Temp\
quit

PHP exec not giving the same result as cmd

exec ("C:/Lame/sox \"C:/1/2.wav\" -t wav \"C:/1/2.rev\" reverse");
Using that code to use an audio post processing tool to reverse a sound file. There is an output but the file is about 1/5th the size it should be and I am unable to play it. Basically it makes a file but its not the one I would have gotten if I did this in the command prompt:
C:/Lame/sox "C:/1/2.wav" -t wav "C:/1/2.rev" reverse
With that, I get the result I want and I am able to play the rev file.
Anyone have any idea why this is happening?
Found the problem. It was a permission problem.
All the other post processing command works because it writes in that folder. Reverse makes a temporary file in another folder which the current user didn't have write access in which why it made a small file since it tried to later read from a file that didn't exist.

determine if PDF file is openable and not corrupt

I am wandering if anybody has a reliable way of determine whether a PDF document is actually a PDF document, and that it isn't corrupted.
I generate reports on my system and I want to be certain that the data returned by another system contains an openable PDF document (and that the data is not corrupt).
At the moment, I am basically looking at string length (the PDF gets stored into a variable, not a physical file).
Any recommendations to do this in PHP would be great.
If you just want to make sure the file is a PDF file, without checking that it is a completely intact pdf file with no issues, you can read the first 5 bytes of the file and for a PDF file they will be exactly equal to the string "%PDF-"
This is how the file program in linux identifies PDF files.
But if you want to make absolutely sure there are no errors anywhere in the file, you can run a program that processes the entire file, and see if that program returns success.
In linux you can use ghostscript ("gs") to render the PDF document to any format.
Or you can install acrobat reader, and run acroread as a command line program to convert it to postscript:
acroread -print -toPostScript [your_file.pdf]
To do either of these you will need to use the system PHP function. To check of the program ran successfully, you need to pass a variable in the second parameter to system that will receive the return status.
You can use pdfinfo, centos installation command:
yum install poppler-utils
... and use pdfinfo command. The PHP code is as follows:
if(!exec("pdfinfo test.pdf")){
echo "file is corrupted"
}

Is there a way to check whether a file is completely uploaded using PHP?

I have a directory on a remote machine in which my clients are uploading (via different tools and protocols, from WebDav to FTP) files. I also have a PHP script that returns the directory structure. Now, the problem is, if a client uploads a large file, and I make a request during the uploading time, the PHP script will return the file even if it's not completely uploaded. Is there a way to check whether a file is completely uploaded using PHP?
Setup your remote server to move uploaded files to another directory, and only query the directory files are moved to for files.
AFAIK, there is no way (at least cross-machine) to tell if a file is still being uploaded, without doing something like:
Query the file's length
Wait a few seconds
Query the file's length
If it's the same, its possibly completed
Most UNIX/Linux/BSD-like operating systems has a command called lsof (lsof stands for "list open files") which outputs a list of all currently open files in the system. You can run that command to see if any process is still working with the file. If not, your upload has finished. In this example, awk is used to filter so only files will show that are open with write or read/write file handlers:
if (shell_exec("lsof | awk '\$4 ~ /.*[uw]/' | grep " . $uploaded_file_name) == '') {
/* No file handles open for this file, so upload is finished. */
}
I'm not very familiar with Windows servers, but this thread might help you to do the same on a Windows machine (if that is what you have): How can I determine whether a specific file is open in Windows?
I would think that some operating systems include a ".part" file when downloading a file, so there may be a way to check for the existence of such a file. Otherwise, I agree with Brian's answer. If you were using the script on the same system it is simple enough to tell using move_uploaded_file()'s return if it was being uploaded by a PHP script, but it does become a challenge pulling from a remote directory that can be added to with different protocols.

Detect with php if files is being uploaded, or is open

I have a PHP script that opens a local directory in order to copy and process some files. But these files may be incomplete, because they are being uploaded by a slow FTP process, and I do not want to copy or process any files which have not been completely uploaded yet.
Is is possible in PHP to find out if a file is still being copied (that is, read from), or written to?
I need my script to process only those files that have been completely uploaded.
The ftp process now, upload files in parallel, and it take more than 1 second for each filesize to change, so this trick is not working for me now, any other method suggests
Do you have script control over the FTP process? If so, have the script that's doing the uploading upload a [FILENAME].complete file (blank text file) after the primary upload completes, so the processing script knows that the file is complete if there's a matching *.complete file there also.
+1 to #MidnightLightning for his excellent suggestion. If you don't have control over the process you have a couple of options:
If you know what the final size of the file should be then use filesize() to compare the current size to the known size. Keep checking until they match.
If you don't know what the final size should be it gets a little trickier. You could use filesize() to check the size of the file, wait a second or two and check it again. If the size hasn't changed then the upload should be complete. The problem with the second method is if your file upload stalls for whatever reason it could give you a false positive. So the time to wait is key.
You don't specify what kind of OS you're on, but if it's a Unix-type box, you should have fuser and/or lsof available. fuser will report on who's using a particular file, and lsof will list all open files (including sockets, fifos, .so's, etc...). Either of those could most likely be used to monitor your directory.
On the windows end, there's a few free tools from Sysinternals that do the same thing. handle might do the trick

Categories