i have inserted SQL values on tables with PHP that before has readed a .txt file.
The thing is that I can not work with those values.
PHP reads on the file .txt '8,00' and inserts this value correctly on the data base but if i do a strlen() this returns a 6 and it has tu return a 4.
TXT: the text on .txt
if (in_array($fileActualExt, $allowed)){
if ($fileError == 0){
if ($fileSize > 5000){
echo "Tu archivo es muy grande!";
} else{
$fileNameNew = uniqid('', true).".".$fileActualExt;
$fileDestination = 'uploads/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
$file = fopen("uploads/$fileNameNew","r");
if ($file) {
if (($line = fgets($file)) !== false) {
$ram = $line;
}
if (($line = fgets($file)) !== false) {
$cpu = $line;
}
if (($line = fgets($file)) !== false) {
$so = $line;
}
if (($line = fgets($file)) !== false) {
$gpu = $line;
}
if ($i=1) {
$sql = "INSERT INTO comparar (ram,cpu,gpu,so) VALUES ('$ram','$cpu','$gpu','$so')";
$conn->query($sql);
}
session_start();
$session_array2 = array($ram,$cpu,$gpu,$so);
$_SESSION['nombre2'] = $session_array2;
fclose($file);
CodePudding user response:
Thanks for the clarification above. Seems that when you are extracting the text from the txt file you are also extracting the new line character \n.
Clean the value you want to insert with something like:
strlen(str_replace(array("\n", "\r\n", "\r"), '', $ram));
This should now return the correct length for the value are trying to work with.