Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a php-script which saves a pdf from a eclipse birt report to pdf.
I'm using get file content as imput.
The birt report pdf takes some time to create.
I think this is the problem.
In the following, the script:
<?php
$rname = 'reportname';
$wname = $rname . '_' . date('d.m.Y') . '.pdf';
$pdf = file_get_contents("http://xxx.xxx.xxx.x:8080/Birt/run?__report=" . $rname . ".rptdesign&sample=my+parameter&__format=pdf");
file_put_contents('/tmp/report' . $wname, $pdf);
?>
What is the problem?
Thanks for your help :)
Try to set a request timeout for file_get_contents
file_get_contents('http://www.example.com/', false, stream_context_create(Array("http" => Array("method" => "GET",
"timeout" => 600,
))));
Also check the default timeout
echo ini_get("default_socket_timeout");
file_put_contents() will return a numeric value (representing bytes written) if it succeeded with writing, and will return false when the write was not successful. If the write is not successful, the web server might not have permission to write to the directory. Make sure the directory is writable.
$result = file_put_contents('/tmp/report' . $wname, $pdf);
if( is_numeric($result) && $result > 0 ) {
// write was successful
} else {
// write was NOT successful
}
Relevant PHP doc: http://php.net/manual/en/function.file-put-contents.php
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a problem while trying to open a file and display its contents using php
My file called hello.txt
Here is my PHP code
<?php
$filename = 'hello.txt';
$filePath = 'c:\\Users\\bheng\\Desktop\\'.$filename;
if(file_exists($filePath)) {
echo "File Found.";
$handle = fopen($filePath, "rb");
$fileContents = fread($handle, filesize($filePath));
fclose($handle);
if(!empty($fileContents)) {
echo "<pre>".$fileContents."</pre>";
}
}
else {
echo "File Not Found.";
}
?>
I got this from
http://php.net/manual/en/function.fread.php
I keep getting error:
fread(): Length parameter must be greater than 0
Can someone help me please?
Although there are good answers here about using file_get_contents() instead, I'll try to explain wht this is not actually working, and how to make it work without changing the method.
filesize() function uses cache. You probably executed this code while still having the file empty.
Use the clearstatcache function each time the file change, or before testing its size :
clearstatcache();
$fileContents = fread($handle, filesize($filePath));
Also obviously make sure that your file is not empty ! Test it :
clearstatcache();
if(file_exists($filePath) && filesize($filePath)) {
// code
}
It needn't be that hard, and it certainly doesn't require you to read a file in binary mode:
if (file_exists($filePath))//call realpath on $filePath BTW
{
echo '<pre>', file_get_contents($filePath), '</pre>';
}
All in all, you really don't want to be doing this kind of stuff too much, though
If you need to read the entire file's content, there is a shortcut function
http://php.net/manual/en/function.file-get-contents.php
So you don't need to bother creating a file handler and closing it afterwards.
$fileContents = file_get_contents($filePath);
using file_get_contents method of php
echo file_get_contents("text.txt");
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Hello I cannot get this to work and am looking for some help.
Here is my current code:
$accepted_file_mime_types = array('image/gif','image/jpg','image/jpeg','image/png','application/pdf','application/zip','application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/msword','text/plain','audio/wav','audio/mp3','audio/mp4');
$file_extension = strtolower(strrchr($_FILES["userpro_file"]["name"], "."));
if( !in_array($file_extension, array( '.gif','.jpg','.jpeg','.png','.pdf','.txt','.zip','.doc','.docx','.wav','.mp3','.mp4' ) ) || !in_array($fileinfo,$accepted_file_mime_types) ){
// .. Do stuff
}
here is the code that I have tried, however still gives me the error of invalid file type:
$accepted_file_mime_types = array('image/gif','image/jpg','image/jpeg','image/png','application/pdf','application/zip','application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/msword','text/plain','audio/wav','audio/mp3','audio/mp4', 'text/x-vcard');
$file_extension = strtolower(strrchr($_FILES["userpro_file"]["name"], "."));
if( !in_array($file_extension, array( '.gif','.jpg','.jpeg','.png','.pdf','.txt','.zip','.doc','.docx','.wav','.mp3','.mp4','.vcf' ) ) || !in_array($fileinfo,$accepted_file_mime_types) ){
// do stuff
}
Any help would be greatly appreciated.
Thanks.
vCard files have mimetypes of
text/vcard
You'd need to add that as one of the accepted_file_mime_types. If it's not that, it could be any of the following mimetypes which are now deprecated.
text/x-vcard
text/directory;profile=vCard
text/directory
Edit - if it still does not work, you will need to output the mimetype when you upload the file to see what needs to be accepted.
Try a MIME type of text/vcard. text/x-vcard is deprecated.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I try to copy some files from one directory on server to another, but it does not work.
Here is my code:
system('cp /var/www/site1/images/' . $row['imageUrl']. ' /var/www/site2/content/upload/content/item/mid/' . $row['imageUrl']);
$file = '/var/www/site1/images/'.$row['imageUpl'];
$newfile = '/var/www/site2/content/upload/content/item/mid/'.$row['imageUpl'];
if (copy($file, $newfile)) {
echo "success";
}
else
{
echo "failed";
}
Look at the copy() function here: http://php.net/manual/ru/function.copy.php
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am using a cross domain request.A request has been made to php file in a server via ajax from another server.From php side Here, I need to create a file and write some contents in that file.My request is reaching perfectly.But I am not able to create a file.Please help
NOTE : ITS A CROSS DOMAIN REQUEST
<?php
$filename = "lin.txt";
$data2 = "lin IS HERE";
$newFile= fopen($filename, 'w');
chmod($newFile, 777);
fwrite($newFile, $data2);
fclose($newFile);
?>
Thanks in advance
In am not sure what are you going to do, because you haven't told us your code. I think this will help you:
<?php
$file = 'demo.txt';
$content = "Example\n";
file_put_contents($file, $content);
?>
The file is demo.txt (it is at the same location with the PHP document.
$content is the text which will be put on your txt file.
Hope it helps!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a website, where the user can write PHP/HTML code and save it to his computer. Everything is fine until the user types a slash (/).
The file saves into the client computer, but instead of saving the client code, it exports an PHP error (the file in the computer has a PHP code of an error). The file-saving code is the following:
$content = $_REQUEST['code']; //Get the code
$file = "file.php";
file_put_contents($file, $content); //Writes the content into a file
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename=$file');
readfile(dirname(dirname($con)) . '/'.$file);
The error only happens when the client uses slashes.
Any idea on why is this happening? )-:
EDIT:
This is one of those errors:
The code that i tried to export was the following:
/:
The thing that worries me, is that if I type exactly the same characters in different order (:/) then they export to my computer with no errors.
I see only one error - problem with readfile(\/105.2.php) in line 24 - so I tested it.
$file = 'file.php';
$con = '/:';
readfile(dirname(dirname($con)) . '/'.$file);
It gives me incorrect path \/file.php as in error message.
If I use $con=':/' it gives me correct path ./file.php
I only don't know what $con is. Maybe you have dirname(dirname($content)) in your oryginal code and $content = $_REQUEST['code']; => dirname(dirname($_REQUEST['code'])) => dirname(dirname("/:")) => "\"