/    Sign up×
Community /Pin to ProfileBookmark

file_get_contents question

hi;
usually when you want to open a file, you use the following code or fopen to grab a file:

$yourdata = file_get_contents(“abc.txt”);

now what if I want to have a choice, instead of going to code page
and change e.g. abc.txt to zzz.txt,

how can I have a browse button that when you click on it, it gives you the option to browse to your hard drive and select one file or multiple of files you want to bring to process with your script.

thanks for help.

to post a comment
PHP

17 Comments(s)

Copy linkTweet thisAlerts:
@hastxAug 06.2007 — Im not sure I understand, but if you want have a browse button to select a file to work with, you have to use a form and upload the file before you can work with it.
Copy linkTweet thisAlerts:
@learner2006authorAug 06.2007 — thanks for reply,

yes, I have to use form, but i don't know to use them together in my script,

here is the form that I have:

[CODE]
<form method="POST" enctype="multipart/form-data" action=" ">
<input type="file" size="20" name="filename">
</form>
[/CODE]

I don't know what I should have in action of form,

here is a top part of my code in same page:

[code=php]
$yourdata = file_get_contents("abc.txt");

$var1 = preg_split("/[s]+/", $yourdata);

[/code]


so instead of having abc.txt I want to have the option to download a file.

I don't know how to code that instead of having file_get_contents get the file from the folder that file has been downloaded.

I am not sure if I should have the form right in the parenthese where abc.txt is or not,
Copy linkTweet thisAlerts:
@kvirriAug 06.2007 — You can't use file_get_contents on a client's file. The client has to upload the file to the server. You'll see that file in the $_FILES superglobal, next index beeing the name of the file input tag.

Make that form submit to a php file that does a
<i>
</i>print_r ($_FILES);


Next, copy the file to a temporary directory, and then you can open it with file_get_contents ?
Copy linkTweet thisAlerts:
@learner2006authorAug 07.2007 — thanks for reply kvirri ;

I decided to load files in a folder by myself,

Now I want to make the script read files from the folder,

as you see the file_get_contents can read only the given text file,

how can I make it to read one or more files that are located in a folder.

appreciate any help.
Copy linkTweet thisAlerts:
@hastxAug 07.2007 — First index the folder
[code=php]
<?
$path = "./files";//relative path to dir with files
$show = array( '.txt', '.php', '.TXT', '.PHP' ); //Type of files to show
$dh = @opendir( $path );
$file_list = "<h3>Files:</h3><br>n";
while( false !== ( $file = readdir( $dh ) ) ){
$ext=substr($file,-4,4);
if(in_array( $ext, $show )){

$file_list .= "<a class=file href="javascript:void(open('get_contents.php?filename=$file','','top=50,left=50'));">$file</a><br>n";
}
}

closedir( $dh );
?>
<html>
<head>
<title>
Index of <?=$path?>:
</title>
</head>
<body>
<?
echo"<h1>Files in Directory: "$path"</h1>";
echo"$file_list";
?>
</body></html>
[/code]


Then create a second page called "get_contents.php" and insert your code to read the files. put it in the same dir as the first file
[code=php]
<html>
<head>
<title>
</title>
</head>
<body>
<?
$path = "./files/"; //point to the dir containing your files
$filename=$_GET['filename'];
$file= file_get_contents($path.$filename);
echo "<pre>$file</pre>"
?>
</body></html>
[/code]
Copy linkTweet thisAlerts:
@learner2006authorAug 07.2007 — thanks for reply,

that is a great script, thank you very much,

it works all well and shows the files inside folder,

I am trying get my script read one sample text file inside the folder,

but I can not get it to work,

on the other page where there is this script:
[code=php]

<?
$path = "./files/"; //point to the dir containing your files
$filename=$_GET['filename'];
$file= file_get_contents($path.$filename);
echo "<pre>$file</pre>"
?>
[/code]


I try to get and do a word count process,

here is the part of script that I have:
[code=php]
$words = file_get_contents("abc.txt");
$var1 = preg_split("/[s]+/", $words);

[/code]


so now instead of having abc.txt in there I should be able to read file out of the folder.
Copy linkTweet thisAlerts:
@learner2006authorAug 07.2007 — I have tried this , added two lines at the end, only part of the code, but I think is deadly wrong,

I don't get error, but as a result the written file is empty,

( the script that I work on eventually after counting words write a text file in a folder which works fine when given a single file name in file_get_content


[code=php]
<?
$path = "./files";//relative path to dir with files
$show = array( '.txt', '.php', '.TXT', '.PHP' ); //Type of files to show
$dh = @opendir( $path );
$file_list = "<h3>Files:</h3><br>n";
while( false !== ( $file = readdir( $dh ) ) ){
$ext=substr($file,-4,4);
if(in_array( $ext, $show )){

$file_list .= "<a class=file href="javascript:void(open('get_contents.php?filename=$file','','top=50,left=50'));">$file</a><br>n";
}
}

closedir( $dh );
?>
<html>
<head>
<title>
Index of <?=$path?>:
</title>
</head>
<body>
<?
echo"<h1>Files in Directory: "$path"</h1>";
echo"$file_list";

$words = file_get_contents($file);
$var1 = preg_split("/[s]+/", $words);
// this two lines are only part of the script for word counting

?>
</body></html>


[/code]
Copy linkTweet thisAlerts:
@learner2006authorAug 07.2007 — OK,

I have tried to make the script that [B]hastx[/B] has provided to work, and could not get anywhere, if anyone has an idea please help me out, I want it to handle read and write multiple files,

anyway I am now working on another script :

here is some info

I have managed to have a script that does work when a single file name

has been given. has added some codes and the result of the code is given below.

I want to make it work with multiple files both for reading and writing, and I need help to make it work, I am not good with php, anyway here is what it does when script runs:

read a text file from a folder,

counting words of the text file, then insert some non English words/characters from another file(csv format) into the text (after a certain number of words)

then make a new file out of the result,

(in this example is called new-text.txt.)

Now I want to make the script read all text files inside the folder one by one,

( i will put text files in the folder, no need for the script to do that).

and then do the insertion one by one

and then write new text files one by one,

and then give a name to the files (possibly a variation of the file names that has been used), should be recognized which file belongs to which one,

e.g. aaa.txt becomes aaa_1.txt and so

or even rewrite the files.

the code shows only the single file writing.

here is what I have now:

[code=php]

$fileLocation = 'zz'; // this the folder that contain text files
$fileNames = array();
$dh = opendir($fileLocation) or die('error opening dir');
while (false !== ($file = readdir($dh)))
{
if ( trim($file, '.') != '' )
{
echo "file name : ".$file."<br>";

function isDir($dir) {
$cwd = getcwd();
$returnValue = false;
if (@chdir($dir)) {
chdir($cwd);
$returnValue = true;
}
return $returnValue;
}

$var2 = file_get_contents("domedata.csv");
$var2 = explode('%%', $var2);


$allwords = file_get_contents("$file"); // this should read files from folder
$var1 = preg_split("/[s]+/", $allwords);
$i = 0;
$j = 1;
$numofwords = 30;
while($i < sizeof($var1)){
if($j == $numofwords+1){
$randnum = rand(0, count($var2)-1);
array_splice($var1, $i, 0, array($var2[$randnum]));
$j = 0;
}
$j++;
$i++;
}
$var2 = implode(" ", $var1);

$fp = fopen( "new-text.txt", "w" ); // this write a single file
fwrite( $fp, $var2 );



}
}
closedir($dh);


[/code]

the echo part is not necessary.

I am greatfull for some help,

thanks
Copy linkTweet thisAlerts:
@learner2006authorAug 09.2007 — no one can help on this?

perhaps it is very complicated.
Copy linkTweet thisAlerts:
@hastxAug 09.2007 — ....

read a text file from a folder,

counting words of the text file, then insert some non English words/characters from another file(csv format) into the text (after a certain number of words)

then make a new file out of the result,

(in this example is called new-text.txt.)

Now I want to make the script read all text files inside the folder one by one,

( i will put text files in the folder, no need for the script to do that).

and then do the insertion one by one

and then write new text files one by one,

and then give a name to the files (possibly a variation of the file names that has been used), should be recognized which file belongs to which one,

e.g. aaa.txt becomes aaa_1.txt and so

or even rewrite the files.

...[/QUOTE]


I dont think people understood what the goal was, let me see if i understand:

You have some text files, and a CSV file in a directory

You want to
[LIST=1]
  • [*]open a text file

  • [*]read from a CSV

  • [*]Insert a word from the CSV (after every 5th word in the text file)

  • [*]Save the modified file as a revision

  • [/LIST]


    Do you use the same csv file on every text file?

    Does the CSV file need to be in specific format...or can it just be a text file containing the words you want to insert?
    Copy linkTweet thisAlerts:
    @learner2006authorAug 09.2007 — I dont think people understood what the goal was, let me see if i understand:

    You have some text files, and a CSV file in a directory

    You want to
    [LIST=1]
  • [*]open a text file

  • [*]read from a CSV

  • [*]Insert a word from the CSV (after every 5th word in the text file)

  • [*]Save the modified file as a revision

  • [/LIST]


    Do you use the same csv file on every text file?

    Does the CSV file need to be in specific format...or can it just be a text file containing the words you want to insert?[/QUOTE]


    thank you for reply,

    Yes that is what i want,


    the script read both files, read csv file to just split some data that are between %% character it could be one word or a line of text, and there is more than just one word or data as we speak, so every time that one of these data in inserted into text is different data.

    the script also read text file ( content), count words until it reaches a certain number say 30 words, then it inserts one of the data from csv file which is between %% characters.

    then the result will be a text file that has one or two or more.. data inserted in between the content which now being saved as a new file called new-text.txt in my example.

    as I explain, I want to make it work as read and write couple of files,

    the only thing I should do is put my text files ( say 10 files)in the folder, the csv file there already, then run the script, the script should create 10 new text files in the folder or rewrite the same text files in the folder.

    thanks
    Copy linkTweet thisAlerts:
    @hastxAug 09.2007 — I think this is what you are looking for. At least the bones of it, you can polish it if you like.

    [LIST]
  • [*]it will unzip to a folder called "insert_test".

  • [*]All files will go to the same folder.

  • [*]The original files are named with a ".txt" extension

  • [*]The new edited files will be appended with a "_edit" and have a ".text" extension. They can't have a ".txt" extension while using the same folder.

  • [*]The only variable you have to set is the interval of words to insert.

  • [/LIST]


    the file to run is "insert_test/insert.php"

    [upl-file uuid=a85c0290-e00b-457b-9274-12e22260f3fd size=1kB]insert_test.zip[/upl-file]
    Copy linkTweet thisAlerts:
    @learner2006authorAug 09.2007 — Thanks a lot for your help,

    I am really greatful, I never think the way you did to solve the problem

    It is working but there are two problems with it,

    1-when the interval is let's say 10,

    and the text file is long ,

    and let's say there 5 line of data in .dat file,

    now when the insertion is done, you will see after fifth data there is

    nothing in the content, just a character (),

    I want it to repeat itself until the end of text file. even shuffle the data,

    this way all the text file does not have same data from the beginning.

    2- it seems there will be problem when data in .dat file is more than a line, it will be mess, the script i had solved this problem by inserting data which was in between %%,

    I am trying now to solve this problem, if you have a fast solution appreciate it,

    otherwise I am very greatful for the output.

    thanks
    Copy linkTweet thisAlerts:
    @hastxAug 10.2007 — yes the script was written so that one line in the dat file = one insertion string. Every new line will be looked at as a separate insertion string.
    Copy linkTweet thisAlerts:
    @learner2006authorAug 11.2007 — I can not make the repeating part works,

    in this part, I am trying to see if I can make changes on the last part of the Concise Conditional, it print nothing :""; when the condition is not ture, and so it does not, How can I replace it with continue to the print from the beginning when the data in in .dat file reaches the end,

    or perhaps I should change the Concise Conditional and use another function.


    any Idea?

    [code=php]

    $temp .= ($insert_count != 0) ? "".trim($inserts[$insert_count])." " : "";
    [/code]



    Thanks.
    Copy linkTweet thisAlerts:
    @hastxAug 11.2007 — The only thing that line does, is prevent an insert from being placed at the "0" position (or the very beginning) of the text file.

    without it, you would get your first insert before the text file begins, and then start counting.

    I think you are looking for a different function, but if you want to print something at the beginning of the file, you just enter it.
    [code=php]
    $temp .= ($insert_count != 0) ? "".trim($inserts[$insert_count])." " : "text inserted at beginning of file";
    [/code]
    Copy linkTweet thisAlerts:
    @learner2006authorAug 11.2007 — thanks for reply and explanation of the function,

    as I am not good with php, at least not knowing all the functions, I can not come up with another solution or function, I tried another function but it takes me be back to where I was that the process writes only to one file,

    does anyone has a solution for this script?

    I just want the script keep inserting the data until the end of content.

    the problem of having more than one line data is solved and I made it shuffle now there is no problem on that part.

    appreciate any help
    ×

    Success!

    Help @learner2006 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.8,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...