What filepath to use with ffmpeg_php? - php

I'm using ffmpeg-php to extract thumbnails from user uploaded videos for a site I'm building. Previously the videos were stored in a subdirectory of the directory containing the relevant php files and it worked fine. However I have now altered my directory structure and can't work out what filepath to supply ffmpeg_movie() with. The relevant files are:
/app/classes/class_lib.php
location of the class that calls ffmpeg_movie() and extracts/saves the thumbnail.
/app/upload.php
the php file that requires the above class, instantiates it and calls the relevant method.
And the videos are stored in:
/videos/encodes/
All those paths are relative to the sites public root (public_html). I have trieda number of different paths but keep getting a "cannot open movie file [Attempted video path]". I've tried paths relative to the site root /video/encodes/movie.mp4, relative to the executed php file ../video/encodes/movie.mp4, relative to the php class file ../../video/encodes/movie.mp4 and even the server root /srv/www/sitename.com/public_html/video/encodes/movie.mp4. No luck with any of them.
Any other ideas?
Server is Apache running on Ubuntu and directory permissions haven't changed since it was previously working (the encodes folder is globally readable)

I've ended up using the full url http://somesite.com/video/encodes/movie.mp4. It actually works, but it feels like the wrong way to do it. Unfortunately nothing else seems to work.
Still, if you're having the same problem then this is at least a working solution.

Related

php move_uploaded_file always ends up in webroot

Whenever I use move_uploaded_file to my an uploaded file, the file always ends up in my web root. What am I doing wrong? Should the path be relative to my web root, or should it be an absolute path on my file system?
Ultimately what I'm trying to do, it have a folder for php to upload/dowload files. I don't want web bots and anyone else just to be able to access the files, i want only authenticated people using my website to be able to download the files. So this is how I have my file structure laid out:
/var/www/website/public_html
and
/var/www/website/files
and my move_uploaded_file command is like this:
move_uploaded_file($_FILES['txtFileSelector']['tmp_name'], "/var/www/website/files/".$_FILES['txtFileSelector']['name']);
but no matter what i've tried, the file always ends up in
/var/www/website/public_html
I've even tried sending the file in other sub folders of public_html but still no luck.
ah-ha! Destination path is relative!
So the solution for me is:
echo move_uploaded_file($_FILES['txtFileSelector']['tmp_name'], "../files/".$_FILES['txtFileSelector']['name']
Because of the relative pathing, use .../ to go up from the web root, and then move it to the desired storage folder.
CORRECTION
Absolute path or relative path either will work. It was a combination of folder permissions (www-data needs to either be owner or group member with read/write permissions) and me being an idiot and discovering a programming bug. My code was in a php class and the uploading was function. In my constructor I had a bug in my code. When doing OO there's a big difference between
$upload_dir = "/path/to/upload";
and
$this->upload_dir = "/path/to/upload";

Referring to the root directory

Before I start, this question may have been asked before, but I either don't know what to type to find it specifically and this is a slightly more specific case.
I'm developing a website with many dynamic ties, some of which is at the beginning of every php file there is the line require("global.php"); which does what the name states, among others such as the css file and whatever else is found on the root level. Problem is however, upon entering down into another directory, the link to this file is broken.
Traditionally what I've done (which is an absolutely stupid beginner workaround) is create a variable just before the require called $up which contained the string '../' which recurred respectively depending on how many directories deep the file is from the root. So the line would then appear require($up."global.php");.
I've realised how stupid this is and have tried to find ways around this. One of which is using the variable $_SERVER['DOCUMENT_ROOT'] to print out the root directory but the reason why this won't work 100% is because a) I'm developing the website on a local machine where the 'root' of the website in development is located in a subdirectory on the server and b) it returns the entire path based upon the location on the drive starting from D:\ and working its location that way, which I don't particularly like to work with when it comes to developing websites and would rather remain within the hosted directories of the web server if possible.
So, based on what I've explained, is there an easy way to get the root location of a website (regardless if the project is in a subridectory or the real root of the web server) within a short string that is extremely easy to append to the begining of every file reference in every php file regardless of it's directory level?
Thanks! <3
Suppose you have the following directory:
/srv/website/index.php
/srv/website/lib/functions.php
You could then do this:
define("MY_ROOT", $_SERVER['DOCUMENT_ROOT']);
And use it in every (included) file relatively:
require (MY_ROOT . "lib/functions.php" );

Find filepath to public_html directory or it's equivalent using PHP

I'm creating a .php file that will be uploaded to the root directory of a server. I need that .php file to then figure out the path to the public_html folder or it's equivalent.
I need to do this because I want my .php file to be able to be uploaded to the root and used on any hosting account. Because many hosting companies use different file paths to the public_html folder or even call it something different, I'm trying to figure out how to detect it.
Preferable there is a server variable or easy test to do this. If not, the public_html folder will always contain a particular file so maybe I could search for this particular file and get the path that way. I'm just worried about a filename search being heavy on memory.
The .php file that is being executed is located inside the ROOT directory and needs to locate the public_html folder.
Like this: /home/user/file.php
needs to detect
/home/user/public_html/ or /home/user/var/www/ or /home/user/website.com/html/ etc.
The challenge with this is that a server can have very many public_html's so outside of the context of a request there is no real way to find out what that is.
One thing that you might be able to do to get this information from a php script (if you know the url to get to the host) is to create a php file called docroot.php that looks like this.
<?php
if ($_SERVER["REMOTE_ADDR"] == '127.0.0.1'){
echo $_SERVER["DOCUMENT_ROOT"];
}
Then within your file.php your would do something like
$docRoot = trim(file_get_contents("http://www.mydomain.com/docroot.php"));
This makes the assumption that the server can resolve to itself via the local interface by name.
I found this website which provided me with the only good solution I have found after scouring the web...
$root = preg_replace("!${_SERVER['SCRIPT_NAME']}$!", "", $_SERVER['SCRIPT_FILENAME']);
The way this works is by getting the full path of the file and then removing the relative path of the file from the full path.

play wav file in server when file is in different directory

i'm trying to make an web site where the user can listen to different wav files.
I made a php script to get all .wav files that i want and i keep their path on an array.
Then im doing this to play the audio:
echo "<a href='teste.wav'>Play sample 1 </a>";
echo "<a href='" .$audios[$id][1] ."'>Play sample 2 </a>";
?>
<script src="http://mediaplayer.yahoo.com/js></script>
On the first case i got the audio on same folder as the script and it works fine. On the second case i have it in a completely different directory and it never finds the file.
I know that the sample 2 will ref to something directly under the current directory where the script is being executed, but i tried to make the $audios[$id][1] like http://home/.../file.wav and still doesn't work.
Any idea how to fix this?
/home/jorge/VOCE/Recordings/-1458206716/Tiago#Aug_31_2012_Tiago_ID_1204811836_pr‌​ebaseline.wav
Do not use absolute paths. These are invalid outside your local file system. You have to use relative paths so browser can build request pointing to reachable file, so it should be more like this:
VOCE/Recordings/-1458206716/Tiago#Aug_31_2012_Tiago_ID_1204811836_pr‌​ebaseline.wav
(assuming your project publicly visible folders is parent to VOCE)
but i tried to make the $audios[$id][1] like http://home/.../file.wav
and still doesnt work.
That would work ONLY if your browser is running physically on the same machine your server is running and filesystem is accessible to you.
EDIT
How to do this right - some assumptions (if real system differs, just make adjustments) Your filesystem structure is as follow
document-root/
scripts/
test.php
recordings/
audio.wav
index.html
document-root is location on your disk which your httpd serves all files from. It is irrelevant what it really is. If I do http://yourdomain/index.html then it shall show content of index.html. Your scripts are in scripts/ folder and your audio files are in recordings. Then relative path from test.php to audio.wav is ../recordings/audio.wav. If you really need to use absolute path (not recommended) then it shall be http://yourdomain/recordings/audio.wav. Choose what's simpliest for you.

How to access a file with PHP in a virtual directory on IIS 7

Here is my problem: I have a website running on IIS 7 + PHP 5.3. There is a virtual directory in the website hierarchy called "vd" which contains flash files.
myApplication
- test.php
- vd
- animation1.swf
- animation2.swf
- ...
So, it's easy to reach the swf files with a browser, you just have to put the directory name in the url: http://www.mySite.com/vd/animation1.swf
However, I would like to use the getimagesize() function on my animations in a PHP script. But in this case, php can't find the file:
<?php
// test.php
var_dump(getimagesize('vd/animation1.swf')); // false
?>
It seems to make sense because anyway '/' aren't even the right directory separator for windows. But I just can't figure out how to make getimagesize works through a virtual directory, I tried a lot of stuff without success (using the DIRECTORY_SEPARATOR constant, using realpath() function ...).
Of course, I could use the real path in my script but it would be easier for me to be able to do that throught the virtual path.
Any help would be greaty appreciated,
Thanks!
PHP is not going through the web server but rather the OS to get the file. So, no web server virtual-to-real translations will take place. You will need to use the real path of the file. You could make a web request on the file and deduce the size in that manner but that is very inefficient. It makes much more sense to just do it right through the OS.
Simply create a variable/define/class constant or whatever you prefer to store the real path of the pertinent directory. Then just append animation1.swf to that in your code.

Categories