C file execution

should be able to, it looks to be just ascii text.

So you’d connect to the database, read the data, and use file_put_contents() to write the data to a file.

Ok thanks! ^^

Hello, another problem. When i compile my program using terminal, a txt file will be generated to store the output and it worked, but when i exc the c file in php, the txt file is not generated. why is that?

p/s: i put all my file inside public_html

what are the rights of the folder that needs to contain the output? Can www-data write to it? As that is what your C program will be running under so you may need to allow the folder to be writeable by that user

I only know that when i use terminal to execute it, it will work.
“Can www-data write to it?”, how to check this?
“what are the rights of the folder that needs to contain the output?” , u mean the folder permission?

Yep, find out what permission is on the folder.
ls -la foldername

Next try to give the folder write access to everyone
chmod 0777 foldername

Then run your script in apache again and see if it generates the file for output. If it does, you have a permission issue. If it doesn’t, something else is going on and I’m not sure what.

SUCCESS! thanks man.^^,

How if that .txt file which already have the content of all the number below and i wanted to put it back to the mysql database? i tried but cant. i declare it as a longtext in database. is it right?

UPDATE: SOLVED.^^,

keep in mind, it may be better for you to create a new group and give both your username and www-data access to that group (see the usermod command we referenced earlier). Once both of you are in that group you could do the following:

addgroup newgroupname
use the usermod command we referenced earlier using the newgroupname for both your username and www-data
chown www-data.newgroupname foldername
restart apache

Then you should be able to leave the folder permission at 0755 (or 0775 if your user still needs permission)
chmod 0755 foldername

Ok sure i will. i have a post for you at #28

You would need to use file_get_contents() and then write an update/insert statement (whichever is applicable). The type of column you use would be based on how much data you need to store and the type of data. If it is ascii characters and will be fairly lengthly, then text/longtext is just fine

Hello there, i’ve met a new problem, in my .txt file i have this:

\xf5\x00\x00\x00\x1c\x0b\x23\xda\x01\x0f\x96\x17\x61\x12

but when i insert it into the database from “$file = file_get_contents(‘./results_eigenvalue.txt’);”, it worked, but it appear to be like:

xf5x00x00x00x1cx0bx23xdax01x0fx96x17x61x12

The slash are gone. how to fix this??

UPDATE: i found out that it is because the magic quotes so i use this command “set_magic_quotes_runtime(true);” in my php file then it can display the “\” symbol. Is it a good method?

Second problem is after i get my data from database and tried to put it into a .txt file,it does not working. below is my code:

<?php

$con = mysql_connect(“localhost”,“root”,“880702”);
if (!$con)
{
die('Could not connect: ’ . mysql_error());
}

mysql_select_db(“FYP”, $con);

$sql=“SELECT * FROM STUDENT_DATA”;

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
{
$name = $row[‘Name’] ;
$id = $row[‘IDNumber’];
$eigenvalue = $row[‘Eigenvalue’];
$chk = $row[‘CHK’];
}

mysql_close($con);

echo $name.“<br>”;
echo $id.“<br>”;
echo $eigenvalue.“<br>”;
echo $chk.“<br>”;

$file = ‘eigen_from_database.txt’;
// Open the file to get existing content
$current = file_get_contents($eigenvalue);
// Write the contents back to the file
file_put_contents($file, $current);

echo (“SUCCESS!!”);

?>

after taking from database i did echo all to make sure i get the data, although i get the correct data but still unable to transfer it into .txt file.

UPDATE: Problem solved! i use below command:

$myFile = “eigen_from_database.txt”;
$fh = fopen($myFile, ‘w’) or die(“can’t open file”);
fwrite($fh, $eigenvalue);
fclose($fh);

Your issue was this

$current = file_get_contents($eigenvalue);
// Write the contents back to the file
file_put_contents($file, $current);

You don’t need file_get_contents in this situation, you already had the value. You simply needed

file_put_contents($file, $eigenvalue);

Hmm… I typically leave magic_quotes turned off and use $value = addslashes($value); (this may be a good question in a new thread to get various opinions on which is better practice)

Hello there,i have a php code which will execute two C file. The way it run is like this, after i exec the matching_fingerprint, it took about 3 sec. then it will output a result in a .txt file. Second execution which is “check_matching_result” will use that particular .txt file to give result which is “OK” or “GG”. The thing is, when i run it in terminal. it can detect correctly even if i switch finger, but when i run the same file in php using the code below, it will either echo JUST “fingerprint matched” or JUST “error”. why is that happening?

<?

exec(‘./matching_fingerprint’);

sleep(5);

$matching_status = exec(‘./check_matching_result’);

if ($matching_status == “OK”)
{
echo (“FINGERPRINT MATCHED! THANK YOU!”.“<br>”);
}
else
{
echo (“ERROR: FINGERPRINT NOT MATCHED! TRY AGAIN!”.“<br>”);
}

?>

Kind of hard to answer that… but I’d start off pointing exec will only return the LAST line outputted by the script/program you call. So if a prior line contains the data you seek, then you won’t get the expected response you want.

You may want to use exec’s second parameter (output) so you can get ALL of the output and can find the data you seek.
http://us1.php.net/manual/en/function.exec.php

Maybe i didn’t clear up my buffer inside my C code,but the problem why it respond well when executing it in terminal rather than in php. Hurm…I need time to figure this thing out. Anyway, thanks man.

I’d capture the entire output and output that in var_dump from within your web application (using the second parameter of the exec statement). Something may be running differently and giving you unexpected data back.

i did this “$matching_status = exec(‘./check_matching_result’); var_dump($matching_status);” and i did really received the correct data thou. Still finding the problem why.