iwato Posted March 31, 2010 Share Posted March 31, 2010 QUESTION: What is a good explanation for Experiment 2's failure to generate a statement?BACKGROUND: The following is modified code obtained from an example given on the PHP website. My goal was to understand the use of the flock() function and its parameters. Two experiments in two different PHP files were run. Their results are provided below.experiment1.php: I can run this experiment as often as I like, and the statement "Unable to obtain lock" is never echoed. <?php $file = "../copy/nonPHP_file.txt"; echo "The original file is " . filesize($file) . " bytes large."; clearstatcache(); $openedFile = fopen($file, 'r+'); if(!flock($openedFile, LOCK_EX | LOCK_NB)) { echo 'Unable to obtain lock'; exit(-1); } fclose($openedFile);?> experiment2.php: Although the PHP run is successful, the experiment fails to produce either of the two hoped for echoed strings. <?php $file = "../../filesystem/copy/nonPHP_file.txt"; $openedFile = fopen($file, "a+"); if (!$openedFile) { echo "The file " . $file . " failed to open. <br />"; } else { "The file " . $file . " was successfully opened. <br />"; }?> Roddy Link to comment Share on other sites More sharing options...
Synook Posted March 31, 2010 Share Posted March 31, 2010 You forgot the echo keyword. Link to comment Share on other sites More sharing options...
boen_robot Posted March 31, 2010 Share Posted March 31, 2010 You're missing an "echo" in your else. You always get to the false part, and there's just a string "in the wild" there.Note that PHP only locks a file while the script is running, so what happens is that you always get a lock, then by closing the file, you're essentially removing the lock too. After that, the second experiment gets a file without a lock, so it runs on the else part.To see the effect of the lock, you'll either have to find a way to run both scripts at once (I suggest a long sleep(), during which you run the second script), or (more easily) write those two in one file. Link to comment Share on other sites More sharing options...
iwato Posted March 31, 2010 Author Share Posted March 31, 2010 You forgot the echo keyword.Not really. It was just a typo. I copy and paste via a format neutralizer.Whether the echo command is present or not, it fails.Roddy Link to comment Share on other sites More sharing options...
iwato Posted March 31, 2010 Author Share Posted March 31, 2010 You're missing an "echo" in your else. You always get to the false part, and there's just a string "in the wild" there.Note that PHP only locks a file while the script is running, so what happens is that you always get a lock, then by closing the file, you're essentially removing the lock too. After that, the second experiment gets a file without a lock, so it runs on the else part.To see the effect of the lock, you'll either have to find a way to run both scripts at once (I suggest a long sleep(), during which you run the second script), or (more easily) write those two in one file.It is good to know that the lock disappears when the file is closed. I will have to explore the sleep() function.This said, whether the second echo statement is proper or not, the second experiment fails. Could it be a problem with permissions again. I thought I had resolved that problem.Roddy Link to comment Share on other sites More sharing options...
boen_robot Posted March 31, 2010 Share Posted March 31, 2010 Fails? The file opens... what do you expect? A failure due to the lock? Again, once the first script finishes (with or without fclose()), the lock is removed. If you call sleep() after obtaining the lock, and execute the second script before the sleep() is over, the file in the second script should not open, because the lock will still be in place. Link to comment Share on other sites More sharing options...
iwato Posted March 31, 2010 Author Share Posted March 31, 2010 Fails? The file opens... what do you expect? A failure due to the lock? Again, once the first script finishes (with or without fclose()), the lock is removed. If you call sleep() after obtaining the lock, and execute the second script before the sleep() is over, the file in the second script should not open, because the lock will still be in place.I am expecting an echoed message that tells me whether or not a resource was created for the file that I just opened.Roddy Link to comment Share on other sites More sharing options...
boen_robot Posted March 31, 2010 Share Posted March 31, 2010 I am expecting an echoed message that tells me whether or not a resource was created for the file that I just opened.RoddyDidn't the addition of the echo fix that part? i.e.else {echo "The file " . $file . " was successfully opened. <br />";} Link to comment Share on other sites More sharing options...
iwato Posted March 31, 2010 Author Share Posted March 31, 2010 Didn't the addition of the echo fix that part? i.e.Unfortunately, not.Roddy Link to comment Share on other sites More sharing options...
justsomeguy Posted March 31, 2010 Share Posted March 31, 2010 If neither clause of an if statement is running, then the entire code isn't running. Make sure you have error messages enabled:ini_set('display_errors', 1);error_reporting(E_ALL);If you make those changes, and still see no output, it really sounds like it's missing an echo statement. The basic fact is that this structure will always print something: if (...){ echo '1';}else{ echo '2';} In all situations that will print something, there is no possible condition for the if statement that will cause no output. If that in fact doesn't produce any output, then it's not being executed.Make sure to run the code until the timeout, if that file is in fact locked then the code may be waiting for the file to unlock, so it would just sit there until the PHP timeout. Link to comment Share on other sites More sharing options...
iwato Posted April 1, 2010 Author Share Posted April 1, 2010 "... there is no possible condition for the if statement that will cause no output. If that in fact doesn't produce any output, then it's not being executed."Could it be that my PHP initialization is controlled by my installation host? When I try to contact e-Living theThis what I found inside when I looked. Everything expression containing the word MAMP was highlighted in red, so it made it easy to find. error_reporting = MAMP_error_reporting_MAMPdisplay_errors = MAMP_display_errors_MAMPdisplay_startup_errors = MAMP_display_startup_errors_MAMPlog_errors = MAMP_php_log_errors_MAMPerror_log = "MAMP_php_error_log_MAMP"extension_dir = "MAMP_extension_dir_MAMP"; ExtensionsMAMP_apc_MAMPextension=imap.soextension=gettext.soextension=mcrypt.soextension=yaz.soextension=pgsql.soextension=pdo_pgsql.soextension=pdo_mysql.soMAMP_eAccelerator_MAMP eaccelerator.shm_size="16" eaccelerator.cache_dir="/Applications/MAMP/tmp/eaccelerator" eaccelerator.enable="1" eaccelerator.optimizer="1" eaccelerator.check_mtime="1" eaccelerator.debug="0" eaccelerator.filter="" eaccelerator.shm_max="0" eaccelerator.shm_ttl="0" eaccelerator.shm_prune_period="0" eaccelerator.shm_only="0" eaccelerator.compress="1" eaccelerator.compress_level="9" eaccelerator.allowed_admin_path="/Library/Application Support/living-e/MAMP PRO/mamp"[xcache-common]; install as zend extension (recommended), normally "$extension_dir/xcache.so"MAMP_XCache_MAMP[Zend]zend_optimizer.optimization_level=15MAMP_zend_optimizer_MAMP I have sent a request to MAMP Pro -- my installation host -- to explain the presence of this code, but they have not yet responded.Also, I have a slightly different, but related question: Is it possible for the unencoded HTML of a PHP file to display even though the PHP encoded portion of the code fails?Roddy Link to comment Share on other sites More sharing options...
iwato Posted April 1, 2010 Author Share Posted April 1, 2010 Make sure you have error messages enabled: . . .This is the error that I found in the MAMP-Pro generated error log.The file TMP3B0QR6NEX.php is test file produced by Safari when I run my test server in Dreamweaver.[01-Apr-2010 14:32:57] PHP Fatal error: Call to undefined function fopen() in /Applications/MAMP/htdocs/php-practice/functions/miscellaneous/exit/TMP3B0QR6NEX.php on line 38.Roddy Link to comment Share on other sites More sharing options...
Synook Posted April 1, 2010 Share Posted April 1, 2010 Well, that's it then - the Filesystem functions have been disabled/uninstalled. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.