Adding a generated .srt into a HTML5 video - javascript

I am using PHP to generate a .srt file to add into a HTML5 Video, but is not working and its showing this message on console:
Resource interpreted as TextTrack but transferred with MIME type text/plain: "../subtitles/Test%20Edit.srt".
I am using this JQuery script to make the video work http://www.storiesinflight.com/js_videosub/#download, works fine with the example, but not with my .srt file.
I am creating the .srt file with this code:
$folder = 'subtitles/';
$filename = $this->get_title() . '.srt';
$fp = fopen($folder.$filename,'w');
$i = 1;
$Query = mysql_query("") or die(mysql_error());
while ($a = mysql_fetch_array($Query)) {
$subtitle = new Subtitle($a['idSubtitle']);
$text .= $i . chr(13) . chr(10) . $subtitle->get_start() .
',000 --> ' . $subtitle->get_end() . ',000' . chr(13) . chr(10) .
$subtitle->get_text() . chr(13) . chr(10) . chr(13) . chr(10);
$i++;
}
fwrite($fp,$text);
fclose($fp);
It is generating this file:
1
00:00:01,000 --> 00:00:10,000
Test
2
00:00:12,000 --> 00:00:15,000
Test 2

As Charlotte Dunois said, you're not setting the right MIME type.
There are multiple solutions to this. One would be to load the .srt from a PHP page, that then changes the MIME type using header() before any content is served. Another, usually more practical, solution would be to make your web server figure out the correct MIME type by itself, setting the Content-Type header.
If you're using Apache, look into mod_mime, otherwise look up the documentation for your specific web server.

Related

Cannot allocate memory, couldn't create child process : Do I need to queue the PHP-image requests?

Update 3aug 2017:
I've tried out a simple test. I made a PHP-file with the following content:
<?php
echo "<img src='http://www.hependos.fi/testgetimage.php?file=2016-11-02_1478082625_zKW3xUPF.jpg'>";
echo "<img src='SIMILARLINKWITHANOTHERFILENAME'>";
echo "<img src='SIMILARLINKWITHANOTHERFILENAME'>";
echo "<img src='SIMILARLINKWITHANOTHERFILENAME'>";
echo "<img src='SIMILARLINKWITHANOTHERFILENAME'>";
echo "<img src='SIMILARLINKWITHANOTHERFILENAME'>";
echo "<br><br>";
echo "<img src='http://www.hependos.fi/Ikonit/test/2016-11- 02_1478082625_zKW3xUPF.jpg'>";
echo "<img src='SIMILARLINKWITHANOTHERFILENAME'>";
echo "<img src='SIMILARLINKWITHANOTHERFILENAME'>";
echo "<img src='SIMILARLINKWITHANOTHERFILENAME'>";
echo "<img src='SIMILARLINKWITHANOTHERFILENAME'>";
echo "<img src='SIMILARLINKWITHANOTHERFILENAME'>";
?>
and the testgetimage.php-file contains the following:
<?php
$KuvaReferenssi = basename(urldecode($_GET["file"]));
$fileDir = "Ikonit/test/";
$file = $fileDir . $KuvaReferenssi;
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
?>
The images with a fixed source display correctly each time, but with the php-file-as-source files once again do not show up correctly, on average 2/5 images appear correctly.
The images are the smallest I could find on my server with a single file has a size of 4.4kb.
PHP error says:
Fatal error: Allowed memory size of 2097152 bytes exhausted (tried to allocate 2312704 bytes) in /home/hependos/public_html/testgetimage.php on line 57
Cpanel's error log states the same as mentioned below.
What am I doing wrong here?
Thanks in advance!
Original post
I'm having trouble displaying my images on my web-pages. The images are being fetched by a php-script.
The images on the pages have the source of a .php-file which fetches an image based on a filereference. website.c om/getImage.php?file=filename
And then the part of the php-file that actually sends the image to the browser has the content of:
if (file_exists($fileDir . $file)){
$contents = file_get_contents($fileDir . $file);
echo $contents;}
Of course the filename and directory is fetched from a database and used as the variable $file there.
The actual problem is that the servers Virtual memory is capped really easily (fetching 6 images, each sized around 200kb), resulting in HTTP500.
Cpanels error log states (for each HTTP500):
[Sun Jun 25 15:31:00.131534 2017] [:error] [pid 398316:tid 140190395291392] (12)Cannot allocate memory: [client 85.76.42.74:50544] couldn't create child process: /usr/sbin/suphp for /home/username/public_html/getImage.php, referer: http://www.website.com/index.php
This is really an issue as some images don't load at all (there is no specific order in which they decide to appear or not to appear).
I've tried to contact my service-provider but they keep saying "Myes, the server's capacity is topped out.".
This can't be the case as there is hardly any traffic other than myself and a few others on my server. And the requests I send are very small (text-based article and then these images).
But the thing is, am I missing something on the image-fetch? Am I supposed to clean-up some variables or free-up memory for use after each fetch?
I don't use any libraries or anything, I've written everything myself.
I'll be happy to reply to any further questions in order to get this thing going, its really frustrating. :(
Edit 28.06 15:06
I replaced the those 3 lines and tried out the readfile()-function like this:
echo readfile($fileDir . $row['KuvaNimi']);
but no difference. Out of 6 images, 5 never show up.
After 6 months of reviewing, testing and struggling with my code, it wasn't my code in the first place. My service provider had a deprecated option left on in Linux's LVE-options. They didn't specify too much more, but that was the issue causing all these symptoms here.
Thanks everyone who sacrified your thoughts to this, its finally up and running as it should be. :)

PHP user profile page

Im trying to make a website with users on it, and I'm trying to create a script so that whenever a new user registers it will automatically create a user folder and profile for them but when I try to register it doesn't create the files, could someone please help me with this, Thanks
<?php
include "inc/header.php";
$newfolder = $username;
if (!mkdir($newfolder, 0777, true)) {
die('Failed to create folders...');
}
$pagename = $username;
$newFileName = './u/'.$username.'/'.$pagename.".php";
$newFileContent = '<?php echo "something..."; ?>';
?>
To make a directory/file
if (!file_exists("parent_folder/$username")) {
//Create a file with read write execute PERMISSIONS ENABLED
//Please check : your parent folder also must have 0777 permissions to avoid any kind of read write error
mkdir("parent_folder/$username", 0777);
//now u have to create a FILE with .php
//now this file_put_contents is VERY importnant !
$pagename = $username ;
$newFileName = './parent_folder/$username/'.$pagename.".php";
$newFileContent = '<?php echo "something..."; ?>';
if (file_put_contents($newFileName, $newFileContent) !== false) {
//notify file is created
echo "File created (" . basename($newFileName) . ")";
} else {
//notify u have error
echo "Cannot create file (" . basename($newFileName) . ")";
}
//now create ur .php file in user folder
}
else {
echo "Your parent folder does not exist"
}
Now the possible error and some tips
1) Most of people do fopen("filename_with_PATH", "w")
and expect that file will be generated in PATH folder !
Some times it might fall wrong (depends on version)
fopen is meant to create a file in the directory where your php resides
2) check ur php permission in php.ini if u dont give php permission to write,remote access then u might get some errors (it will be displayed that u have error in my script)
3)For more info and tinkering file_put_contents
Hope this will be helpful for you ..

Recursively generate script tags for all JavaScript files within a directory with PHP

I'm building a complex app with lots of JavaScript files in lots of sub-directories. I know I want to include them all (it won't affect performance), but I don't want to manually create a script tag for each. Given that all of my files are children of a "/js" directory, how could I dynamically generate the script tags for each with PHP? Something like this:
// first somehow recursively get all .js files, then:
foreach($files as $file) {
echo '<script src="' . $file->path . '"></script>';
}
Most elegant way is to use SPL in my opinion.
$dirIterator = new RecursiveDirectoryIterator("/path/to/js");
$iterator = new RecursiveIteratorIterator(
$dirIterator,
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $file) {
if($file->getExtension() == 'js') {
// You probably have to adjust the full path according to your DOC_ROOT
$url = $file->getPathname();
echo '<script src="' . $url . '"></script>';
}
}
Have a look at http://php.net/manual/en/class.splfileinfo.php to see what else you can do with $file .

write a file on local disk from web app [duplicate]

I am trying to create and save a file to the root directory of my site, but I don't know where its creating the file as I cannot see any. And, I need the file to be overwritten every time, if possible.
Here is my code:
$content = "some text here";
$fp = fopen("myText.txt","wb");
fwrite($fp,$content);
fclose($fp);
How can I set it to save on the root?
It's creating the file in the same directory as your script. Try this instead.
$content = "some text here";
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/myText.txt","wb");
fwrite($fp,$content);
fclose($fp);
If you are running PHP on Apache then you can use the enviroment variable called DOCUMENT_ROOT. This means that the path is dynamic, and can be moved between servers without messing about with the code.
<?php
$fileLocation = getenv("DOCUMENT_ROOT") . "/myfile.txt";
$file = fopen($fileLocation,"w");
$content = "Your text here";
fwrite($file,$content);
fclose($file);
?>
This question has been asked years ago but here is a modern approach using PHP5 or newer versions.
$filename = 'myfile.txt'
if(!file_put_contents($filename, 'Some text here')){
// overwriting the file failed (permission problem maybe), debug or log here
}
If the file doesn't exist in that directory it will be created, otherwise it will be overwritten unless FILE_APPEND flag is set.
file_put_contents is a built in function that has been available since PHP5.
Documentation for file_put_contents
fopen() will open a resource in the same directory as the file executing the command. In other words, if you're just running the file ~/test.php, your script will create ~/myText.txt.
This can get a little confusing if you're using any URL rewriting (such as in an MVC framework) as it will likely create the new file in whatever the directory contains the root index.php file.
Also, you must have correct permissions set and may want to test before writing to the file. The following would help you debug:
$fp = fopen("myText.txt","wb");
if( $fp == false ){
//do debugging or logging here
}else{
fwrite($fp,$content);
fclose($fp);
}

Http content loaded in https connection

My Webapp is running with a https connection / ssl certificate. I need to show pictures to the user. I get the links to the picture by an API request and link them afterwards. Sadly, the pictures address is http, so the browser shows that there are unsecure parts on the site, which mustn't be...
I could download the pictures and link to the proper picture afterwards, but I think this might be a little timeconsuming and not the best way to handle this.
Does somebody know a better solution for this? I'm using php, jquery and javascript.
You'll have to write a proxy on your server and display all the images through it. Basically your URLs should be like:
$url = 'http://ecx.images-amazon.com/images/I/51MU5VilKpL._SL75_.jpg';
$url = urlencode($url);
echo '<img src="/proxy.php?from=' . $url . '">';
and the proxy.php:
$cache = '/path/to/cache';
$url = $_GET['from'];
$hash = md5($url);
$file = $cache . DIRECTORY_SEPARATOR . $hash;
if (!file_exists($file)) {
$data = file_get_contents($url);
file_put_contents($file, $data);
}
header('Content-Type: image/jpeg');
readfile($file);
Ok, I've got a streaming example for you. You need to adapt it to your needs, of course.
Suppose you make a php file on your server named mws.php with this content:
if (isset($_GET['image']))
{
header('Content-type: image/jpeg');
header('Content-transfer-encoding: binary');
echo file_get_contents($_GET['image']);
}
Look for any image on the web, for instance:
http://freebigpictures.com/wp-content/uploads/2009/09/mountain-stream.jpg
now you can show that image, as if it was located on your own secure server with this url:
https://<your server>/mws.php?image=http://freebigpictures.com/wp-content/uploads/2009/09/mountain-stream.jpg
It would of course be better to store the image locally if you need it more than once, and you have to include the correct code to get it from Amazon MWS ListMatchingProducts, but this is the basic idea.
Please don't forget to secure your script against abuse.

Categories

Resources