Reading a file without echoing in PHP? [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to read a file, but it keeps printing all of the data to the page without my asking it to do so.
The code is:
<?php
$htmlAppointmentPage = readfile('AppointmentPageTemplate.html');
?>
That's all of the code. For some reason it's echoing the contents of AppointmentPageTemplate.html.

You want file_get_contents() and not readfile().
From the Manual:
This function is similar to file(), except that file_get_contents()
returns the file in a string, starting at the specified offset up to
maxlen bytes. On failure, file_get_contents() will return FALSE.
file_get_contents() is the preferred way to read the contents of a
file into a string. It will use memory mapping techniques if supported
by your OS to enhance performance.
Please note that if your file is very large, it would take a lot of memory (= the file's size) to place it into a variable. Make sure you don't go over the limit set in php.ini.

readfile reads the contents of a file and outputs them to the page.
To get the contents of a file as string (and assign them to a variable), use file_get_contents

Related

call external PHP script with parameters [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
From a PHP page (http://example.com/test.php), I would like to call another PHP file located on another web site (http://newsite.example.net/version.php) and passing parameters (var1=123, var2=789). Version.php should do something and return true or false, so I can test the result in test.php (if ($result) ...)
Any short exemple how to proceed and what should be the content of the fileversion.php ?
Regards,
David
Executing a php script in such a way is equivalent to performing an http query. As such, the only result you can expect from the url you call is a http response (plain text, image, json - whatever mime type the page can output).
It can be done for example with file_get_contents() (provided your php configuration allows it) :
$contents = file_get_contents('http://somesite.com/somepage.php?someparameter=somevalue');
You could then process the resulting content text however you like. But you won't ever get direct boolean output. numbers (0 or 1) could be suitable replacements for your needs, however.
Edit:
The version.php might look like this :
<?php if($condition) { echo 1; } else { echo 0; }

Parse string to remove filename suffix (PHP) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
My PHP scripy receive a string from JS. And i need to parse it to ignore the resolution suffix the image come with, but preserve the filename.
More clearly:
I need to transform this string: starbucks-logo-291x300.jpg into starbucks-logo.jpg in PHP. But sometimes the filename may have a longer string name, like starbucks-logo-2921x3030.jpg and starbucks-logo-291x3200.jpg.The filename prefix may change, so i cant't make it search for "starbucks-logo". Sometimes the filename have no suffix, and come just as i need. And sometimes the file may come in another image extension, like png, gif, jpeg, and bmp.
I'm new to php, so if possible, be clear about the function i need to use, please <3
This can easily be done:
$result = preg_replace('~-\d+x\d+~', '', $filename);

How to get the size of a text file created with fwrite in php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a php page that creates txt files using fwrite which are then uploaded to a directory on the server. I also have a mysql database that the URL for the file is written to, is it possible to get the file size of the said file and add this to the database? If so, how?
after you save your file you know the path of the file try to get normally the filesize
$filename = 'directory_upload/somefile.txt';
$filesize = filesize($filename);
You could try the filesize() function.

fetching from text file or database? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have to display data consisting of approx. 1000 character.
Right now, I am using a text file and fetching it from there line by line till end of file and displaying it through AJAX.
But its quite slow.
I wanted to know if i store that data in database as text would it make difference ?
Yes. It does depend on how many lines there are tho. If there aren't many lines, it won't make much of a difference.
I would recommend (for something small) a txt file on the server and directly getting it directly with AJAX, without the addition step of php and sql.
You shouldn't need to read line by line. You can read the full string with:
file_get_contents('/path/to/file')
I cannot foresee storing the text in a single database row/field will improve efficiency at all.
actually storing text in database can slowdown performance, looks like your problem is in line-by-line reading, read about file_get_contents or file or the best way readfile

Load in random .html file with PHP? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a folder which contains several html files: 1.html, 2.html, 3.html, etc., etc. in sequential order.
I would like for PHP to randomly load in these files into a PHP webpage that I have. How can I go about doing this?
Also -- is PHP the most efficient way to do this? Would jQuery be better?
jquery could do it, but you'd have to send a list of the available files to the client beforehand, so it has a list to choose from. This would be required if you can't guaranteed there'll never be "holes" in the files, e.g. 1,2,4,5 (hey, where's 3?).
PHP can deal with the raw filesystem, and can always get the list of files, e.g.
<?php
$files = glob('*.html');
$random_file = $files[array_rand($files)];
include($random_file);
This will handle any .html file, regardless of holes in the numbering sequence, or even if they're numbered at all.

Categories