Select, Copy & Paste in PHP & CodeIgniter

We are building a website with CodeIgniter & have created the facility where a user can create their own advert. We are saving the adverts in an adverts folder rather than in an adverts category as no doubt other websites do. We want to create a link between the advert & selected categories of the users choice, but that is proving difficult to do.

In our system we have created a file containing ONLY the link, in the hope that somehow we can develop a system that will open that file, select, copy then close the file, then open a category file & paste. The paste must go to bottom & be appended, not overwrite, but that can be undertaken via fwrite. Its the select, copy & paste that is currently impossible.

From the Internet we have read that PHP can not do that, but maybe C++ can, however we are yet to find a script that suits our needs. Maybe we can create a C++ program into a User Library within CodeIgniter. However, we are not sure what would happen if a 2nd person clicked the same facility before the 1st person pasted.

Does anybody have any ideas on how our problem can be solved?
Somehow it's difficult to understand your problem.
Why shouldn't PHP be able to open, read and close a file and append the text to another file ?
CodeIgniter can Open, Close, & Append but wont Select, Copy & Paste.
Check out this sample code I'm not sure it does what you mean by "select copy paste" but it could be a starting point, it categorizes links from a main file into sub-category files.

Note that the code isn't compiled it could contain errors also read the comments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <fstream>
#include <utility>
#include <vector>
#include <ios>
#include <map>

//lets assume this file: advertlinks.txt is where links are stored.
//also assuming the links are stored in this format:
//category_of_link, delimiter_char, link_to_advert, new_line_char:

//Fashion   | https:\\www.thisfashionadvert 
//HomeStuff | https:\\www.thishomestuffadv
//tools     | https:\\www.thistoolsadv..
//Technology | https:\\www.techadvlinks...
//Fashion  | https:\\www.thisfashionadvert2
//Fashion  | https:\\www.thisfashionadvert3
//Beauty   | https:\\www.thisbeautyadvert1
//Technology | https:\\www.techadvlinks1
//...

std::pair<std::string,std::string> split_str(const std::string& str, char delim = '|')
{
    std::string category{},link{};
    int delim_pos = str.find(delim);

    category = std::string(str.begin(), str.begin() + delim_pos);
    link     = std::string(str.begin()+delim_pos+1, str.end());

    return {category,link};
}

//returns a pair of status flag(true if read was successful) and the map that contains the data from the file containing links;
std::pair<bool,std::map<std::string,std::vector<std::string>>>  read_main_adv_file(const std::string& file)
{
    std::ifstream file_stream{file};
    std::map<std::string,std::vector<std::string>> raw_links{};//category & link

    if(file_stream)
    {
        std::string data{};

        while(std::getline(file_stream,data,'\n'))
        {
            auto [category,link] = split_str(data);
            raw_links[category].push_back(link);
        }


        return {true,raw_links};
    }

    std::cout<<"failed to open: "+file+".txt, try again\n";
    return {false,raw_links};
}//file auto closed

//writes data to a category file after selection
bool write_to_category_file(const std::string& file_name,const std::vector<std::string>& link_data)
{
    std::ofstream write_stream{file_name,std::ios::app};

    if(write_stream)//if file opened successfully
    {
        for(const auto& link : link_data) //you should check if the links already exist in this file since you're appending, I didn't do it here
        {
            write_stream<<link<<"\n";
        }

        return true; //data written
    }
    return false;//write failed
}//file auto closed

void categorize_links()
{

    std::map<std::string,std::string> category_file_names{
        {"Fashion","fashion_links.txt"},
        {"Tools","Tools_links.txt"},
        {"Technology","Tech_links.txt"}
        };//file names stored statically.

    auto [status,link_data] = read_main_adv_file("advertlinks.txt");

    if(status == true)//successfully read the data
    {
        for(const auto& category : link_data)//loop over the map keys
        {
            auto file_name      = category_file_names[category];
            auto category_links = link_data[category];

            bool write_complete = write_to_category_file(file_name,category_links);

            if(write_complete)
            {
                std::cout<<"data written successfully to : " +category + " file";
            }

            else
                std::cout<<"failed to write data to : " + category + " file";//handle this error.
        }
    }

    else{
        //handle this error
    }

}





int main()
{
    categorize_links();
}
Last edited on
Sounds like all you want to do is read the contents of one file and append its contents to another.
There is no "selection" nor "copy/paste" involved in this trivial operation
Thanks @mbozzi but although I stand to be corrected I think the append involves overwrite. Overwrite will not be acceptable. Do you have any coding?
I think the append involves overwrite.

Please explain your reasoning.
Last edited on
Append and overwrite are the opposite. In append mode any data written to a file will be written at the end after the original content, therefore appended. In overwrite mode the data will be written at the beginning of the file and the old content will be deleted there overwritten.

Let me check if I understand correctly.
You have a file A that contains the filename / url of a file B that contains the real content. You want to read the content of file B and append it to file C.
PHP is totally able to do that. It has functions like file_get_content, fopen, fwrite, fclose.

BTW. Are you sure you can call C++ code from CodeIgniter?
Thanks @Thomas1965 - I wasnt thinking straight when I said append will overwrite.

I want file A to be read & append it to file C. File B is the advert & file A is the href link to the advert & file C is the category. I am familiar with fopen, fwrite, fclose & have made myself familiar with file_get_content & also file(). I will need to spend time to see if they will achieve what I need which will be my 1st choice.

My 2nd choice will be to use a Python script. CodeIgniter already uses Python.

My 3rd choice is to use C++ & as CodeIgniter does not use C++ & if I need to go down that track, Im hoping I can create a User Library within CodeIgniter which will contain C++.
Hey @Thomas1965 - I have fixed my coding & got the advert creation working good together with the user being able to find the adverts that have been created. Ive done the later primarily with HTML href, & a little PHP, but Im having difficulty in the user being able to select a particular advert file. Once I get the advert selected I will then try to manipulate file_get_content.

Have you any suggestion on how I can select an advert file?
Last edited on
Are the files stored on the same server?
In this case you just need to have the folder name and then you can get the filenames from the PHP functions opendir / readdir.

After that you need to display them on a the webpage - maybe in a select element or in a bunch of <p> tags inside a html form
Thanks @Thomas1965 - Not on server, only localhost.

I have the adverts stored in an adverts folder for example adverts/$user/type/$product.php

I then have a href links to the advert which the user can easily find & open. Once the advert file is found I want the user to effectively be able to select & copy the link & paste it into category file(s).
Topic archived. No new replies allowed.