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.