PHP replace a row in csv works fine on my localhost but does not replace the row when uploaded to cpanel?

Viewed 75

Hello I am relatively new to PHP and I was trying to replace a row in a csv file, i didnt find an optimal solution so I concocted script (a work around) which suits my needs for the time being till I grasp a better understanding of PHP

I tested it on my localhost using XAMPP and everything was working fine , it was replacing the row as intended but when i uploaded the files to my cpanel it stopped replacing and instead it just goes the normal route and write the row on new line.

this is my code :

$fileName = 'Usecase.csv'; //This is the CSV file 
$tempName = 'temp.csv';     
$inFile = fopen($fileName, 'r');
$outFile =  fopen($tempName,'w');
while (($line = fgetcsv($inFile)) !== FALSE) 
{
    if(($line[0] == "$fin") )   //Here I am checking the value of the variable to see if same value exists in the array then i am replacing the array which will be later written into the csv file 
    {
        $line = explode (",", "$tempstr10"); 
        $asd=$asd+1; //this is the variable that i defined and assigned value 0 in the top most section, this is used later in the code 
    } 
    fputcsv($outFile, $line );
}
fclose($inFile);
fclose($outFile);
unlink($fileName);

rename($tempName, $fileName);
    if( $asd==0  && filesize("Usecase.csv")>0) // here its checking if the value is 0 , if value is 0 then that means the above code didnt execute which means the value wasnt present in the file , this is to avoid writing the same string again into the file 
    { file_put_contents("Usecase.csv", "$tempstr10\r\n",FILE_APPEND | LOCK_EX); } 
 
    if( $asd==0  && filesize("Usecase.csv")==0)
    { file_put_contents("Usecase.csv", "$tempstr10\r\n",FILE_APPEND | LOCK_EX); } 

and as I mentioned above , its working on the localhost but not on the cpanel , can someone point out if something is wrong with the code ? or if its something else ? thank you

1 Answers

The most likely problem is that your local version of PHP or your local configuration of PHP is different from what is on the server.

For example, fopen is a feature that can be disabled on some shared servers.

You can check this by creating a php file with the following conents:

<?php phpinfo();

Then visit that PHP file in your browser. Do this for both your local dev environment and your cPanel server to compare the configuration to identify the differences that may be contributing to the differing behavior.

You should also check the error logs. They can be found in multiple different places depending on how your hosting provider has things configured. If you can't find them, you'll need to ask your hosting provider to know for sure where the error logs are.

Typical locations are:

  • The "Errors" icon in cPanel
  • A file named "error_log" in one of the folders of your site. Via ssh or the Terminal icon in cPanel you can use this command to find those files: find $PWD -name error_log
  • If your server is configured to use PHP-FPM, the php error log is located at ~/logs/yourdomain_tld.php.error.log

You should also consider turning on error reporting for the script by putting this at the very top. Please note that this should only be used temporarily while you are actively debugging the application. Leaving this kind of debugging output on could expose details about your application that may invite additional security risks.

<?php
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

... Your code here ...
Related