PHP getimagesize function fails - php

I'm working with wordpress locally and for some reason when I call getimagesize() from page.php to retrieve the sizes it does not work. I get the warning:
getimagesize(http://localhost:8080/wordpress/wp-content/themes/minimize/_/images/img-2.jpg )
[function.getimagesize]: failed to open stream: HTTP request failed! HTTP/1.0 404
Not Found in C:\wamp\www\wordpress\wp-content\themes\minimize\page.php on line 13
The actual code that is calling it is:
$items = get_post_meta($post->ID, 'test');
foreach($items as $item) {
$img_pth = 'http://localhost:8080/wordpress/wp-content/themes/HTML5-Reset-Wordpress-Theme/_/images/'.$item;
list($width, $height) = getimagesize($img_pth);
echo '<img src="'.$img_pth.'" height="'.$height.'" width="'.$width.'">';
}
I'm new to working with PHP so I wouldn't be surprised if it is something very obvious with my code, but I think what I'm trying to do is pretty easy and straight forward. I've tried changing localhost to 127.0.0.1 as I saw in another thread but to no avail. The images are being rendered, so the image paths are correct, but the function is failing. Thanks for any help I can get...

I had the same problem.
Use relative path to the file instead.

You are using wordpress, so it's probably one of two:
php configuration (php.ini)
one of the plugins you're using
Please see my full answer at this link

Related

Counting the pages in a PDF using PHP

I am writing a test to count the number of pages in PDFs stored in a folder on a server. I have been able to get it to work on my local machine, I am unable to get it to work on a remote file.
This is my code that works on local files:
require_once 'C:\..\application\libraries\fpdi\fpdf.php';
require_once 'C:\..\application\libraries\fpdi\fpdi.php';
$pathToFile = 'C:\Users\..\Desktop\filename.pdf';
$pdf = new FPDI();
$pageCount = $pdf->setSourceFile($pathToFile);
echo $pageCount;
But if I change the $pathToFile to a link on a remote server, I get an error message.
I tried this:
$pdfname = 'http://../filename.pdf';
$pdftext = file_get_contents($pdfname);
$num = preg_match_all('/\/Page\W/', $pdftext, $dummy);
echo 'Num: ' . $num;
But again, when I use a local file, it works fine, but the remote file gives me an error (failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden).
From searching online, it seems like that is a common error and I've seen code to use curl, but it makes no sense to me and I can't get it to work either. I saw code to use pdfinfo, but the link in that post is going to another site.
I don't want to have to download anything, so using something like Imagick is not an option either.
All I'm looking for is a simple page number from a file on a remote server. Any help would be much appreciated.

Resolve relative urls of youtube using PHP

This question is asked before but non of the answers worked for me.
I use the following code to directly copy a file from a remote server to my server,
<?php
set_time_limit(0); //Unlimited max execution time
$remote_file_url = $_GET['url'];
$ext = pathinfo($remote_file_url, PATHINFO_EXTENSION);
$name = basename($remote_file_url);
if(isset($ext)){
$local_file = 'download/'.$name.'.'.$ext;
}
else
$local_file = 'download/'.$name;
$copy = copy( $remote_file_url, "1.mp4" );
if( !$copy ) {
echo "Doh! failed to copy $file...\n";
}
else{
echo "WOOT! success to copy $file...\n";
}
?>
It works well but it doesn't copy the files I get from Youtube. I use 1-Click Youtube Video Downloader extension for Firefox which gives me direct link to youtube videos. I can use these direct links in browser and Internet Download Manager as well.
For example the direct url of
https://www.youtube.com/watch?v=xPXrJwQ5lqQ
is
https://r6---sn-ab5l6nzy.googlevideo.com/videoplayback?ipbits=0&requiressl=yes&sparams=dur,ei,expire,id,initcwndbps,ip,ipbits,ipbypass,itag,lmt,mime,mip,mm,mn,ms,mv,pl,ratebypass,requiressl,source&ei=3DNOWfq4CImGc9rxvcgO&signature=3D188D073D872381433A45462E84928383D10D02.4E0AF7D777E76AA19A576D42983A81F4E62EF84D&lmt=1472135086539955&mime=video%2Fmp4&ratebypass=yes&id=o-ABaoUEn3pBt5SLXdWXlrzCdteMLfLPizrRTPoakDoLSX&expire=1498318908&source=youtube&dur=119.211&itag=22&pl=20&ip=162.217.31.128&key=cms1&redirect_counter=1&req_id=ce038b9993a9a3ee&cms_redirect=yes&ipbypass=yes&mip=159.203.89.210&mm=31&mn=sn-ab5l6nzy&ms=au&mt=1498297234&mv=m
The problem is my code can't copy this file to my server. I would like to know of there is any way to resolve such urls?
The error is
failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in /home/...
thanks in advance.
Well, I have no idea why that happened. (Would it be expired?I hope not) I just managed to try another link for the above video (copy the link using right click) in your code as the $remote_file_url and it worked as expected
How did I get that link?
I've used the underlined library : YouTube-Downloader to the 1-Click Youtube Video Downloader extension (it is inherently used by that extension ) this way you will have more control over the process. Then after hosting the files in your web server. Simply run the index.php and when you use it, you'll get something like :
Then you can automate this last part to suit your needs.
That doesn't mean that all videos could be smoothly downloaded with this method. Because of the used videos that have signatures issue or that are recently uploaded issue and here's the list of issues of Youtube-Downloader
For that There is a fix that is somewhat involved: youtube-dl-php, it is based on a sound principle : there is a very good command line utility to download YouTube videos called youtube-dl : here is the download page
Basically, you'll just call it using php. Then, notice that you'll need its path installed in order for the following to work
After you install Composer, go to your web project folder
and run composer require norkunas/youtube-dl-php as explained in the Github page
When running its example, I've get an error
proc_open() 267 CreateProcess failed
I've never dealt with Symphony before and I've found it particularly interesting to play with YoutubeDl.php and redefine the $arguments passed to createProcess and commenting out much of the less useful configuration options to get rid of that error, give it more time to run with
ini_set('max_execution_time', 300);
And yikes it was downloaded.
You don't have to follow this unless you couldn't figure out a better way. It is just supposed to give you an idea of where lies the problem if you havn'et figure it out. And if you have that problem in the first place.
private function createProcess(array $arguments = [])
{
array_unshift($arguments, $this->binPath ?: 'youtube-dl');
$process = new Process("youtube-dl https://www.youtube.com/watch?v=nDMwW41AlSI");
/*$process->setEnv(['LANG' => 'en_US.UTF-8']);
$process->setTimeout($this->timeout);
$process->setOptions($this->processOptions);
if ($this->moveWithPhp) {
$cwd = sys_get_temp_dir();
} else {
$cwd = $this->downloadPath ?: sys_get_temp_dir();
}
$process->setWorkingDirectory($cwd);*/
return $process;
}
Or you can just write your own code that calls youtube-dl, good luck!

file_get_contents on local wamp server not working

I simply cant wrap my head around why this simple code won't work on my local wamp server where i have other sites running.
<?php
$file = file_get_contents('C:\Users\Computer\input.txt', true);
echo $file;
?>
The file is where it should be.
I get the following error:
Warning: file_get_contents(C:\Users\Computer\input.txt): failed to open stream: No such file or directory in C:\Users\Computer\Dropbox\htdocs\ny\www\php\day2\code.php on line 3
But the file is exactly where it should be.
The file has all possible permissions to true (read, write ect.) in windows except for "special permissions".
What is not working correctly?
Regards,
Patrick
The solution was the extension. It was not correctly parsed. This was checked by using #VolkerK suggesting to check
Does the output of var_export(glob('C:\Users\Computer*')); contain
input.txt? Or is it false/null?
Thanks to #VolkerK for the help solving the problem.
Since I incorrectly said that it might be because of backslash escaping, I guess I need to redeem by saying something useful :)
So try:
$file = 'C:\Users\Computer\input.txt';
if(is_file($file))
{
$string = file_get_contents($file);
}
else
{
die("Uh oh! Seems like $file has got problems!");
}
Also, you are passing the second argument as true for searching the include path. Since your filepath is absolute, I suspect if you need this argument to be passed at all.

file_get_contents() failed to open stream: No such file or directory

I am writing some php script to update the code on my sites. In order to do that I have written the following lines which checks for the update version and bring the name from where I use to distribute my updates ans then creates the link of that name. I have done something like this.
$filename = "http://www.hf-live.com/codeupdate/Get_Files_Name.php";
$contents = file_get_contents($filename);
echo $contents;
I am getting this error
failed to open stream: No such file or directory.
Even though the file is present still I am getting the same error. I have turned on my allow_url_fopen to on. The above code is working from my local host but not from the server. I have to update a major bug fix and I am stuck.. Please someone help me soon..
remove extra .php from url
$filename = "http://www.hf-live.com/codeupdate/Get_Files_Name.php";
You will get error if you are doing this way...
$filename = "marynyhof.com/Info.php"; // will give you error, which exactly you are getting
use full url like this
$filename = "http://www.marynyhof.com/Info.php"; //will work

PHP - Failed to open stream: no such file or directory - for existing file

I'm writing a PHP application and in my code i want to create create and return images to the browser. However, sometimes i'm getting some weird results where the image cannot be created since the file does not seem to exist.
Here is a sample error message I get and the code in a nutshell. I do know that the image exists, but still the method sometimes fails, and sometimes it succeeds, even for the same file.
The error:
Warning: imagecreatefrompng(path/to/image.png) [function.imagecreatefrompng]: failed to open stream: No such file or directory in file test.php on line 301
The code:
if (file_exists($filename)) {
$image = imagecreatefrompng($filename);
}
I would greatly appreciate any hints or tips of what might be wrong and how I can improve the code to be more stabile.
I suggest you use is_readable
if (is_readable($filename)) {
$image = imagecreatefrompng($filename);
}
The file may "exist" but is the file accessible? what does file_exists actually do?
if it opens the file and then closes it make sure that the file is actualy closed and not locked before imagecreatedfrompng fires.
it would be a good idea to try catching the error in a loop and make 4 or 5 attempts before handing back a controlled error.
maybe try is_readable() or is_writable() instead?
Have you considered checking for the correct permissions? If the file cannot be read, but the directory can, you would get file_exists(...) = true, but would not be able to open a handle to the file.
Use is_readable() to check whatever you have permission to access that file.
You can try GD :
IF($img = #GETIMAGESIZE("testimage.gif")){
ECHO "image exists";
}ELSE{
ECHO "image does not exist";
}
bro check for white spaces in your filepath. I recently had this issue while i was tring to include a file from a module i was creating for an app. Other modules included well when called but one didnt. It turned out that there was a white space in the filepath. I suggest u try php trim() function. If this works holla.

Categories