Reputation: 341
Is there any way that I can add the content of multiple files then put the combined values into one file?
I am currently trying this:
$start_tr = include 'include_all_items/item_input_start_tr.php' ;
$img_start = include 'include_all_items/item_input_img_start.php' ;
$img_link = include 'include_all_items/item_input_img_link.php' ;
$img_end = include 'include_all_items/item_input_img_end.php' ;
$newcontent = "$start_tr
$link
$img_start
$_POST[img_link]
$img_end ";
$content = $newcontent.file_get_contents('../../include/item.computer.php');
file_put_contents('../../include/item.computer.php', $content);
Upvotes: 1
Views: 1813
Reputation: 50592
file_get_contents
and include
are two totally different tools. Be sure you make the distinction -- if you're adding more PHP script to the current script, you want include/require. If you're trying to get the contents of files into a variable, that's a whole different task.
Assuming you ARE trying to include/require PHP script, the solution to this is to either a) use an autoloader (preferred for classes) or b) place all your includes in a single file, and then include that file from your other scripts.
Single file method:
common.php
include 'include_all_items/item_input_start_tr.php' ;
include 'include_all_items/item_input_img_start.php' ;
include 'include_all_items/item_input_img_link.php' ;
include 'include_all_items/item_input_img_end.php' ;
myScript.php
include('common.php');
An autoloader is more complex, and I don't have enough details about your use case here to provide a relevant example. An example: https://gist.github.com/jwage/221634
Also, you're taking the return from calling include: $start_tr = include 'include_all_items/item_input_start_tr.php';
-- unless you're doing something in those included files, the return of include
is either false or 1. Per the manual:
Successful includes, unless overridden by the included file, return 1
Finally, be sure you mean include
rather than require
!
Documentation
include
- http://php.net/manual/en/function.include.phprequire
- http://php.net/manual/en/function.require.phpspl_autoload_register
- http://www.php.net/manual/en/function.spl-autoload-register.phpUpvotes: 0
Reputation: 46900
include
is for code, not for content
. In your case you have to use file_get_contents
so you can save the returned value. include
will not give you that value.
Handling Returns: include returns FALSE on failure and raises a warning. Successful includes, unless overridden by the included file, return 1.
$start_tr = file_get_contents('include_all_items/item_input_start_tr.php');
$img_start = file_get_contents('include_all_items/item_input_img_start.php');
$img_link = file_get_contents('include_all_items/item_input_img_link.php');
$img_end = file_get_contents('include_all_items/item_input_img_end.php');
Now you can use your variable like you did.
$newcontent = "$start_tr
$link
$img_start
$_POST[img_link]
$img_end ";
Upvotes: 1