I am trying to test a scraping setup targeting sites of my own using servers of my own, but when I do I get an error from WPRobot saying "Error: Inserting post failed. Cannot create attachment file in "/home/myapp/public_html/wp-content/uploads/2021/03/162153879_434009211024473_384527521975744737_n.jpg" Please set correct permissions." I typically get this notice if the source does not allow other sites to copy its content using curl requests. To get around this I setup a Puppeteer script to act as a proxy by taking screenshots of image files on the source website (in this case Instagram), but now I get the same error when trying to download the screenshots from my own IIS server.
I have added nothing to my server that is intended to prevent anyone from being able to curl image files from it. In fact, there are several sites that were hosted on the same server recently. WPRobot never had a problem downloading any images from those. I have changed nothing other than uploading a Node.js application to the default website directory after deleting the old websites for the purpose of using this server to test the app before my account with the host is suspended in a few days.
What would cause images on an IIS 10 server to not be accessible via curl on the default website? Here is an example image for which WPRobot claims correct permissions are not set http://85.17.219.113/images/2021/3/505648a2-3404-c3e6-618f0fa50fd3.jpg? Could this be due to using an IP address instead of a domain name? Could the length of the file name be an issue?
Also: the image is only 92kb in size. I mention this because I have had issues with WPRobot displaying the same error when trying to download large files, but this file is nowhere near large enough to trigger that.
UPDATE: This appears to be a problem with file_get_contents and/or file_put_contents in PHP and NOT curl. For instance, the following results in an error:
$page = file_get_contents('http://85.17.219.113/images/2021/3/505648a2-3404-c3e6-618f0fa50fd3.jpg');
try{
file_put_contents('test.jpg', file_get_contents($page));
echo 'file downloaded';
}catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
That code does not create a file and does not echo an error message either, but when I turned on error reporting I got this:
Fatal error: Uncaught ValueError: file_get_contents(): Argument #1 ($filename) must not contain any null bytes in C:\xampp\apps\wordpress\htdocs\sources\test.php:16 Stack trace: #0 C:\xampp\apps\wordpress\htdocs\sources\test.php(16): file_get_contents('\xFF\xD8\xFF\xE0\x00\x10JFIF\x00\x01\x01\x00\x00...') #1 {main} thrown in C:\xampp\apps\wordpress\htdocs\sources\test.php on line 16
I cannot explain this error. How can that URL translate to a filename with null bytes?
This downloads the image just fine:
$url = 'http://85.17.219.113/images/2021/3/505648a2-3404-c3e6-618f0fa50fd3.jpg';
// Image path
$img = 'test.jpg';
// Save image
$ch = curl_init($url);
$fp = fopen($img, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
Now I need to find the function in WPRobot that is used to cache images and save them to the file system. Does anyone know where that function is in WPRobot? It would be nice to know exactly how that plugin saves images.
The problem turned out to be WPRobot itself not updating the new feed URL. As a result it was still trying to use the one that included the original Instagram image file URLs. Usually this bug does not take so long to detect. Usually when I load the campaign options it will show the old URL if the update did not work and all you have to do is keep pasting the new URL and hitting the update button until it finally works. I had done this several times and reloading the page showed the new URL in the text area, but for some reason I loaded the page an hour later and the old one showed up.
This really is a horrible bug in WPRobot. When you have to change the URLs in 10-20 campaigns it can take an hour to do it because you keep having to do it over and over again until it finally works.
Related
I have seen some random errors when processing images that were uploaded to my site, where certain .png files will cause an error to be echo'd to the output buffer which will screw up the response back to the server.
The error is:
"Interlace handling should be turned on when using png_read_image"
I have seen this in some cases where i am manually processing uploaded files and now i just started seeing this on my Wordpress install as well. I have not tagged this question as Wordpress, as i doubt it has anything to do with wordpress, rather it must be some issue within PHP.
I originally saw the issue in PHP 5 but i have also seen it since i upgraded to PHP 7. I am now running PHP 7.1.9.
Googling so far has not returned any meaningful results, there do not seem to be any answers which are specific to PHP, most answers say that the user needs to enable interlacing on the source file BEFORE uploading, which would be unacceptable from the web server standpoint because i cannot control what users upload. The images still seem to work fine after upload, i just need a way to suppress this message so it doesn't cause erroneous errors to be shown to the user.
The error message does mention using png_read_image(), however i am not directly calling this function anywhere. perhaps it is calling it internally somewhere, but that makes it very difficult to debug. Best i can figure is that it is related to either imagepng() or imagecreatefrompng().
For now i am working around this in the front end, where i am stripping off the error text before trying to parse the response.
I am sorry if this is a little lacking in details but i really do not know what else to provide for now.
I have this message (PHP 7.2.3 + GD) :
libpng warning: Interlace handling should be turned on when using png_read_image
and some others
libpng warning: tRNS: invalid with alpha channel
libpng warning: iCCP: known incorrect sRGB profile
my code is merging png images from different sources (then different qualities) in one image, after working with Photoshop on some pictures, messages disappeared, however it seems there is a workaround, see explanations below:
<?php
// Create an image instance
$im = imagecreatefromgif('php.gif');
// Enable interlancing
imageinterlace($im, true);
// Save the interlaced image
imagegif($im, './php_interlaced.gif');
imagedestroy($im);
?>
https://www.php.net/manual/en/function.imageinterlace.php
imageinterlace() turns the interlace bit on or off.
And for clarification what interlace is:
The imageinterlace() function is an inbuilt function in PHP which is used to enable or disable interlace in an image. Interlacing (also known as interleaving) is a method of encoding a bitmap image such that a person who has partially received it sees a degraded copy of the entire image. One difference between interlaced and non-interlaced images on a website is that the former one is loaded in a low-quality version first and then it’s quality keeps improving as the website loads whereas a non-interlaced image is loaded in a fixed quality line by line from top to bottom when the website loads.
Source
The problem:
file_get_contents('http://google.com'); works fine (works for any external url)
file_get_contents('http://my.server.org/existing_file.php'); returns nothing, and var_dump($http_response_header); returns an empty array when loading any local url.
I get the same empty response using 127.0.0.1 and localhost in place of the domain name.
I've tried/checked:
I've tried using cURL instead as suggested here - I get the same result (works for external URLs, empty response for local).
allow_url_fopen=On and cURL is enabled in php.ini according to phpinfo() - this must be ok, as I can load external URLs without a hitch.
I cannot use include instead, as I need to load a URL with variables to pass to a PDF renderer.
Is there some other php.ini setting that needs to change? Or could it be some other problem? I think it is a configuration issue because my code works fine locally in MAMP, but not on the production server.
Why I am doing this at all:
It's all tied up in a Wordpress functions.php file - I'm basically loading a url like http://my.server.org/path/to/file/?pdf-template, which I then check against the path to load the correct rendering template, which then feeds the rendered php to DOMPDF to generate a PDF file on the fly. Ultimately it means the client can append /pdf/ to any url in their wordpress site and get a PDF version.
What's odd is that this used to work fine on their server, and the person in charge of the server at the client says nothing has changed with the configuration. They have changed webhost in the last month though, which probably has something to do with it.
Update
I managed to pull this from the error log
Notice: file_get_contents(): send of 2 bytes failed with errno=104 Connection reset by peer in /home/path/to/my/file.php on line 19
The problem also appears to be intermittent - sometimes file_get_contents works, sometimes it fails with the above error. There doesn't seem to be a pattern.
Could this be a firewall problem?
I used file_get_content() to read from the uploaded image temp file and store in database.
Everything is work fine except this warning. It show every page of my site. And there is no such page "footer.php". Is it the server problem or I need to change that function.
Warning:
file_get_contents(http://admanager.nethosting.com/getads/www.mogoke.5gbfree.com/):
failed to open stream: Connection timed out in
/usr/share/php/footer.php on line 11
The problem is another invocation of file_get_contents, which downloads advertisements from a remote server, but that download fails due to network problems or a firewall.
This looks very much like your code or server had a security vulnerability and someone uploaded code that includes ad banners on your website (but the money for the clicks of course goes to the crooks instead of you). Removing the malware (i.e. the added advertisement lines, probably heavily obfuscated) should suffice. See this security.stackexchange.com question for more details.
Well, this is a problem I have never seen before.
I am trying to stream an FTP file using PHP's fopen() and feof() in order to copy it from one server to my own. This works fine when using normal http:// URLs. However, when using the ftp:// protocol, I'm receiving the following error:
fopen(ftp://.../streaming/planted2.mp4) [0function.fopen0]: failed to open stream: FTP server reports 550 /streaming/planted2.mp4: not a plain file.
Bear in mind that I have confirmed the URL is correct.
If I pop it into my browser's search bar, it always loads correctly.
Following this error, any attempt to use feof() or fread() on the wrapper results in an error complaining that the respective function expects a resource, but that instead a boolean is being provided. This would not be the case if fopen() was not failing.
As the files are quite large (several gigabytes at times) streaming is mandatory. Also, due to the configuration of the server, I need a way to loop through each chunk in order to buffer some output. If I don't, the server holds up. It's a weird configuration on Rackspace's behalf. That's why I'm using feof().
So, without further ado, my question is this: What does the 550 error mean, and what is a "plain file"? If it is a problem with the configuration of the server I am attempting to download from, what are my options, given my limitations?
EDIT
I have determined this is a server issue. However, the problem is still unresolved.
I contacted my boss and our server administrator, and the server guy told me to test this out on a different Windows-based server instead of the Linux-based one I was playing with. My script works with the Windows server, so I have confirmed my script is not in error.
Unfortunately, my boss still wants me to figure out the problem, and find out why it's not working on the Linux box. I have absolutely no idea why, and don't know where to look. Any hints would be greatly appreciated!
I've just come across this issue when trying to get a file from a sco unix server.
Server File Location: /home/user/data/myfile.csv
I can put the below into any browser and it gets the file.
ftp://user:password#host/data/myfile.csv
However if I run the below, I get the same error as you
$f = fopen("ftp://user:password#host/data/myfile.csv", "r");
However, if I put the full path into fopen - it works fine.
$f = fopen("ftp://user:password#host/home/user/data/myfile.csv", "r");
I'm not sure if this will fix it for you, but works for me.
I'm currently trying to teach myself PHP by doing a small image-processing type project with Imagemagick. To start off with the basics, I wrote some simple code to read in an image and convert it to a PNG.
However, while I'm able to read from local image files, I am completely unable to read in images from URLs, as it crashes when I call readImageFile() on the url, and I get the following error:
Fatal error: Uncaught exception 'ImagickException' with message 'Invalid CRT parameters detected' in C:\xampp\htdocs\imagepractice\imagemagicktest.php:8 Stack trace: #0 C:\xampp\htdocs\imagepractice\imagemagicktest.php(8): Imagick->readimagefile(Resource id #3) #1 {main} thrown in C:\xampp\htdocs\imagepractice\imagemagicktest.php on line 8
I've spent the last hour Googling for a way to fix this, to no avail, and the only lead I have been able to find is Error in using readImage function (Imagick). However, unlike that issue, I am perfectly able to use readImage, and I'm even able to use readImageFile on local files, just not on image URLs.
From the only comment there, it seems that it may possibly be a bug specific to Windows, but I was wondering if anyone happens to be able to confirm/deny this and/or suggest a way to fix the CRT parameter error?
For reference, the code I wrote is below:
<?php
$im = new Imagick();
//$im->newPseudoImage(1000, 1000, "magick:rose"); //this works!
//$im->readImage("images\\wheels.jpg"); // this works!
$handle = fopen("http://www.google.com/images/srpr/logo3w.png", "rb");
$im->readImageFile($handle); //this line crashes!
fclose($handle);
$im->setImageFormat("png");
$type = $im->getFormat();
header("Content-type: $type");
echo $im->getImageBlob();
?>
In addition, I am running 64-bit Windows 7, and I am using XAMPP 1.7.7 (which uses PHP 5.3.8), and I initially installed Imagemagick 6.6.4 using these instructions. (Although I replaced the 6.6.4 version with Imagemagick 6.6.2 instead, as per the suggestion of a commenter here, which hasn't fixed anything.)
Thanks to some friendly folks at another coding forum, I finally figured out how to stop the getting the error and make this code work. Unfortunately, I'm still not sure what was causing the CRT Parameter error in the first place, but switching from fopen to file_get_contents solved the issue for me.
Working updated code:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
$im = new Imagick();
//$im->newPseudoImage(1000, 1000, "magick:rose"); //this works!
//$im->readImage("images\\wheels.jpg"); //this works!
$url = "http://www.google.com/images/srpr/logo3w.png";
$source = #file_get_contents($url);
if(!$source){
throw new Exception("failed to retrieve contents of $url");
}
$im->readImageBlob($source);
$im->setImageFormat("png");
$type = $im->getFormat();
header("Content-type: $type");
echo $im->getImageBlob();
?>
However, according to someone else at the forum I posted this on,
Use curl, some places have allow_url_fopen disabled in PHP. I do some development on my Windows 7 64-bit machine using XAMPP and curl works everytime no matter what I am doing.
so just to be safe, I'll probably change to using curl instead and seeing if that works, but at least I've solved my issue for now!