Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP - locking an exported file
    text
    copied!<p>I'm looking for an easiest way to lock a file. The following code export a .csv file from mysql via php code :</p> <pre><code>&lt;?php require_once('connect_db.php'); $fdate = $_POST['fdate']; $tdate = $_POST['tdate']; //Selecting Items based on Date $result = mysql_query("SELECT item_no, qty, discount_price, date FROM sold_items WHERE date BETWEEN '".$fdate."' AND '".$tdate."' ORDER BY date Desc"); //Check if File Name Exists $filename = $name = "C:/Sales-{$tdate}".".csv"; $index = 1; while(file_exists($filename)) { $filename = $name.'--'.$index.".csv"; $index++; } //Fputs the Table Headers fputcsv($f, array('Item No', 'Qty', 'Sell Price', 'Date')); //Fputcsv adds records to csv file while($row = mysql_fetch_array($result, MYSQL_NUM)) { fputcsv($f, $row); } //Close file fclose($f); ?&gt; </code></pre> <p>All i need know is a code that will do any of the following:</p> <ul> <li><p>Password protect .csv file when trying to open it</p></li> <li><p>locking fields which makes this file uneditable</p></li> <li><p>or convert this .csv file to zip file, and create password before extracting it.</p></li> </ul> <p>I'm not looking for high secured way to lock this file. However while googling around I found this code :</p> <pre><code>&lt;?php echo system('zip -P pass file.zip file.txt'); ?&gt; </code></pre> <p>I've tried it! but its not working.</p> <p>Thank You</p> <hr> <p>Edit: Using flock</p> <pre><code>$file = fopen("{$filename}", "w+"); // exclusive lock if (flock($file,LOCK_EX)){ //Fputs the Table Headers fputcsv($f, array('Item No', 'Qty', 'Sell Price', 'Date')); //Fputcsv adds records to csv file while($row = mysql_fetch_array($result, MYSQL_NUM)) { fputcsv($f, $row); } flock($file,LOCK_UN); } else{ echo "Could not lock file!"; } fclose($file); ?&gt; </code></pre> <p>It will export the csv file empty but when trying to write something inside any fields and save it; An error will pop up " filename " is read-only and forces me to save as file to another name. But after saving as the file, you can still edit fields and save it. How to disable save as?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload