I have simple CI (CodeIgniter) code. what I want to do is: when I enter the pictures path to textarea, php downloaded each picture to my server and gave them filenames, but I have these errors:
Message: file_get_contents(http://geosmiley.ge/Upload/Product/42/1.jpg ) [function.file-get-contents]: failed to open stream: Invalid argument
Message: file_get_contents(http://geosmiley.ge/Upload/Product/42/2.jpg ) [function.file-get-contents]: failed to open stream: Invalid argument
Here is my CI code:
$image = explode("\n", $_POST['images']);
for ($i = 0; $i < count($image); $i++){
if (substr($image[$i], 0, strlen($this->host) - 1) != $this->host){
file_put_contents('images/'.$title_url.'_'.$i.'.jpg', file_get_contents($image[$i]));
$image[$i] = $this->host.'/images/'.$title_url.'_'.$i.'.jpg';
}
}
$poster = $image[0];
$images = implode("\n", $image);
What it does is simply downloads last file and for first and second files gives me that error
and please don't give me other advice like using cURL of fopen. I think that it is correct way because php downloads LAST FILE successfully. Please help me to solve my problem
From the manual:
A URL can be used as a filename with this function if the fopen
wrappers have been enabled.
You will either need to set allow_url_fopen to true or use cURL instead.
I don't know Why the problem happened but here is the code that fixed it, thanks to "NikiC" who found out that witespace " " was the problem, I fixed it.
This Code doesn't work
$image = explode("\n", $_POST['images']);
for ($i = 0; $i < count($image); $i++){
if (substr($image[$i], 0, strlen($this->host) - 1) != $this->host){
file_put_contents('images/'.$title_url.'_'.$i.'.jpg', file_get_contents($image[$i]));
$image[$i] = $this->host.'/images/'.$title_url.'_'.$i.'.jpg';
}
}
$poster = $image[0];
$images = implode("\n", $image);
This is working code
$image = explode("\n", $_POST['images']);
for ($i = 0; $i < count($image); $i++){
if (substr($image[$i], 0, strlen($this->host) - 1) != $this->host){
if ($i < count($image)-1){
$kk = substr($image[$i], 0, strlen($image[$i]) - 1);
} else {
$kk = $image[$i];
}
if ($this->download($kk, 'images/'.$title_url.'_'.$i.'.jpg')){
$image[$i] = $this->host.'/images/'.$title_url.'_'.$i.'.jpg';
}else{
echo($image[$i]);
}
}
}
//****************************************************
public function download($url, $path){
if (file_put_contents($path, file_get_contents($url))){return TRUE;}else{return FALSE;}
}
Wrap your URL in quotes; make it a string
Related
I have researched this for a few days now and finally found something that seemed to work, but I am getting the wrong result. I need to count the number of pages in a PDF file on a remote server. My code opens the PDF, but it's not finding the correct number of pages and I'm not sure why.
Here is my code so far:
$CI = &get_instance();
$CI->load->library('Awss3', null, 'S3');
$CI->load->library('Pdflib');
$data = $CI->S3->readFile('uploads/225572/filename.pdf', false, 'bucket-name');
$needle = 'Page';
$positions = array();
$lastPos = 0;
while (($lastPos = strpos($data, $needle, $lastPos))!==false) {
$positions[] = $lastPos;
$lastPos = $lastPos + strlen($needle);
}
echo count($positions);
foreach ($positions as $value) {
echo $value . '<br />';
}
$test = strpos($data, 'Page');
If I echo out the $data, I get lots of symbols, etc. and some words, but the $test comes out to 0 when it should be 16. Does it depend on the type of PDF or do I need to decode it or something like that?
Simplest of all is using ImageMagick
here is a sample code
$image = new Imagick();
$image->pingImage('myPdfFile.pdf');
echo $image->getNumberImages();
otherwise you can also use PDF libraries like MPDF or TCPDF for PHP
I'm trying to get an image from an E-mail which is sent to my mailserver and save the file to the server. I tried it with this code:
$no_of_occurences = 0;
$intStatic = 2;
$decode = imap_fetchbody($mbox, $val , null);
$no_of_occurences = substr_count($decode,"Content-Transfer-Encoding: base64");
$no_of_occurences--;
echo $no_of_occurences;
if($no_of_occurences > 0){
for($i = 0; $i < $no_of_occurences; $i++){
$strChange = strval($intStatic+$i);
$decode = imap_fetchbody($mbox, $val , $strChange);
$data = base64_decode($decode);
$x = 1;
$fName = './images/'.$ticketurl."_".$x. '.png';
while(file_exists($fName)){
$x++;
$fName = './images/'.$ticketurl."_".$x. '.png';
}
if($x==500){
$valid = false;
exit;
}
$file = $fName;
$success = file_put_contents($file, $data);
}
}
This code is working fine but only if you have one type of appendage in it. If you place one image inline the mail it is working. If you place one as an attachment, it also works. But if you got both (inline and as attachment) it's just getting one image and a second clean png file.
Any suggestions how to solve this behavior?
I would like to extract the first x lines of an html page
I know that I can extract the amount of characters with something like this:
file_get_contents('http://xx.xxx.158.239/fin.html' , NULL, NULL, 0, 125);
but what about lines ? like to extract the text from line 1 to line 4? is that possible ?
You can read the file using dedicated method calls instead of the one-for-all file_get_contents():
$fp = fopen('my/file/name', 'r');
for ($i = 0; $i < 4; $i++) {
if (feof($fp)) {
echo 'EOF reached';
break;
}
echo fgets($fp);
}
fclose($fp);
Here's a little code snippet that you might find useful:
$file = 'http://xx.xxx.158.239/fin.html'; // remote or local file
$lines = 3; // how many lines do you want?
if (file_exists($file)) {
$contents = #file_get_contents($file); // suppress errors, esp. for remote files
$head = implode("\n", array_slice(explode("\n", $contents), 0, $lines));
} else {
$head = 'File does not exist';
}
echo $head;
You can use file on a File- or URL-Handle created by fopen to load the content into an array and read only the relevant lines.
To get the content line by line in a loop, you can use fgets.
I use newrelic to keep track of anything on my website and I always get this error:
Error message: E_WARNING: fclose() expects parameter 1 to be resource, boolean given
Stack trace: in fclose called at /etc/snmp/bfd-stats.php (68)
This is how /etc/snmp/bfd-stats.php looks like
<?php
$a = 0;
$ptr = 0;
$any = 0;
$mx = 0;
$ns = 0;
$cname = 0;
$soa = 0;
$srv = 0;
$aaaa = 0;
$txt = 0;
$total = 0;
if(file_exists('/etc/snmp/bfd-log-pos.stat')) {
$lfh = fopen('/etc/snmp/bfd-log-pos.stat','r');
$string = fread($lfh,2087);
$res = explode(',',$string);
fclose($lfh);
}
else {
$res = array();
$res[0] = 0;
$res[1] = 0;
}
if(file_exists("/var/log/bfd_log.1")) {
$stats = stat('/var/log/bfd_log.1');
if($stats[10] > $res[0]) {
$res[0] = 0;
$res[1] = 0;
}
}
$fh = fopen('/var/log/bfd_log', 'r');
fseek($fh,$res[1]);
$blocks = 0;
if(!$fh) {
echo "Error! Couldn't open the file.";
} else {
while (!feof($fh)) {
$data = fgets($fh);
if(preg_match('/executed\sban/',$data)) {
$blocks++;
}
}
}
$lfh = fopen('/etc/snmp/bfd-log-pos.stat','w');
$timestamp = time();
$pos = ftell($fh);
fwrite($lfh,"$timestamp,$pos");
fclose($lfh);
if(!fclose($fh)) {
echo "Error! Couldn't close the file.";
}
print("bfd_blocks\n$blocks");
?>
On line 40: $fh = fopen('/var/log/bfd_log', 'r'); I looked at the directory /var/log and there is no file called bfd_log, I dont know if I have to create it by myself or it is automatically created.
Can anyone help me on fixing this error, Thanks in advance.
The error indicates that you are trying to pass a variable with a boolean value (true/false) to a function that needs a resource instead of a boolean value.
Please make sure that before you use resources from variables, the function that returns the resource has not run into trouble. Only on success perform the other functions that use this resource/variable.
$fh = fopen('/var/log/bfd_log', 'r');
// check fh before other functions use this variable
if (!$fh) {
echo "Error! Couldn't open the file.";
} else {
// perform task with resource $fh
fseek($fh, $res[1]);
[...]
$lfh = fopen('/etc/snmp/bfd-log-pos.stat', 'w');
// check before other code block is executed and use this variable
if( $lfh )
{
// perform task with resource $lfh
$pos = ftell($fh);
fwrite($lfh, "$timestamp,$pos");
fclose($lfh);
fclose($fh);
[...]
} else {
// lfh error
}
}
If you always check before using variables, you won't run into this error anymore.
I wrestled with this problem and could not find the answer until I separated my write check (put it first) from the actual file write code. So before I would open the file fopen/fwrite then do the is_writable check and then do the fclose and i would get this error.
To resolve I moved the is_writable and variable declaration before the fopen/fwrite and the error went away. Shown below (former php code position shown in comments) The first comment did help me realize this... Thank you.
$myfile = "/var/www/html/newfile.txt";
if (is_writable($myfile)) {
echo "The file is writable";
}
else {
echo "The file is not writable";
}
$txt = "$name, $email, $command, $searchtype, $keyword \n";
$myfile = fopen('/var/www/html/newfile.txt', 'w') or die("Unable to open file!");
fwrite($myfile, $txt);
// $myfile = "/var/www/html/newfile.txt";
// if (is_writable($myfile)) {
// echo "The file is writable";
// }
// else {
// echo "The file is not writable";
// }
fclose($myfile);
Try
$fh = fopen('/var/log/bfd_log', 'a+');
a+ mode will create the file if it does not exists
I am trying to code a file shredder in PHP, and I get these errors:
Warning: fopen(calculate): failed to open stream: Permission denied in C:\XAMPP\htdocs\shred.php on line 9
Warning: fwrite() expects parameter 1 to be resource, string given in C:\XAMPP\htdocs\shred.php on line 11
<?PHP
$files = glob("*");
foreach ($files as $files) {
$size = filesize($files);
$bytes = "1";
$writes = "1";
while ($writes <= "3") {
$data = fopen($files, "w");
while ($bytes <= $size) {
fwrite($files, "0");
$bytes = $bytes + 1;
}
fclose($data);
$writes = $writes + 1;
}
// unlink($files);
}
?>
I have no idea what to do at this point. The files aren't read only.
You should use $data as the first parameter of fwrite.
Make sure you have the appropriate permissions because that is the common problem in using
fopen()
you can see the the same errors with this one.