<?php $month = 9; // September (only has 30 days) $day = 31; // 31st $year = 2018; // 2018 if (checkdate($month, $day, $year)) echo "Date is valid"; else echo "Date is invalid"; ?>
<?php // testfile.php $fh = fopen("testfile.txt", 'w') or die("Failed to create file"); $text = <<<_END Line 1 Line 2 Line 3 _END; fwrite($fh, $text) or die("Could not write to file"); fclose($fh); echo "File 'testfile.txt' written successfully"; ?>
<?php $fh = fopen("testfile.txt", 'r') or die("File does not exist or you lack permission to open it"); $line = fgets($fh); fclose($fh); echo $line; ?>
<?php $fh = fopen("testfile.txt", 'r') or die("File does not exist or you lack permission to open it"); $text = fread($fh, 3); fclose($fh); echo $text; ?>
<?php // copyfile.php copy('testfile.txt', 'testfile2.txt') or die("Could not copy file"); echo "File successfully copied to 'testfile2.txt'"; ?>
<?php // copyfile2.php if (!copy('testfile.txt', 'testfile2.txt')) echo "Could not copy file"; else echo "File successfully copied to 'testfile2.txt'"; ?>
<?php // movefile.php if (!rename('testfile2.txt', 'testfile2.new')) echo "Could not rename file"; else echo "File successfully renamed to 'testfile2.new'"; ?>
<?php // deletefile.php if (!unlink('testfile2.new')) echo "Could not delete file"; else echo "File 'testfile2.new' successfully deleted"; ?>
<?php // update.php $fh = fopen("testfile.txt", 'r+') or die("Failed to open file"); $text = fgets($fh); fseek($fh, 0, SEEK_END); fwrite($fh, "$text") or die("Could not write to file"); fclose($fh); echo "File 'testfile.txt' successfully updated"; ?>
<?php $fh = fopen("testfile.txt", 'r+') or die("Failed to open file"); $text = fgets($fh); if (flock($fh, LOCK_EX)) { fseek($fh, 0, SEEK_END); fwrite($fh, "$text") or die("Could not write to file"); flock($fh, LOCK_UN); } fclose($fh); echo "File 'testfile.txt' successfully updated"; ?>
<?php echo "<pre>"; // Enables display of line feeds echo file_get_contents("testfile.txt"); echo "</pre>"; // Terminates pre tag ?>
<?php echo file_get_contents("http://oreilly.com"); ?>
<?php // upload.php echo <<<_END <html><head><title>PHP Form Upload</title></head><body> <form method='post' action='upload.php' enctype='multipart/form-data'> Select File: <input type='file' name='filename' size='10'> <input type='submit' value='Upload'> </form> _END; if ($_FILES) { $name = $_FILES['filename']['name']; move_uploaded_file($_FILES['filename']['tmp_name'], $name); echo "Uploaded image '$name'<br><img src='$name'>"; } echo "</body></html>"; ?>
<?php //upload2.php echo <<<_END <html><head><title>PHP Form Upload</title></head><body> <form method='post' action='upload2.php' enctype='multipart/form-data'> Select a JPG, GIF, PNG or TIF File: <input type='file' name='filename' size='10'> <input type='submit' value='Upload'></form> _END; if ($_FILES) { $name = $_FILES['filename']['name']; switch($_FILES['filename']['type']) { case 'image/jpeg': $ext = 'jpg'; break; case 'image/gif': $ext = 'gif'; break; case 'image/png': $ext = 'png'; break; case 'image/tiff': $ext = 'tif'; break; default: $ext = ''; break; } if ($ext) { $n = "image.$ext"; move_uploaded_file($_FILES['filename']['tmp_name'], $n); echo "Uploaded image '$name' as '$n':<br>"; echo "<img src='$n'>"; } else echo "'$name' is not an accepted image file"; } else echo "No image has been uploaded"; echo "</body></html>"; ?>
<?php // exec.php $cmd = "dir"; // Windows // $cmd = "ls"; // Linux, Unix & Mac exec(escapeshellcmd($cmd), $output, $status); if ($status) echo "Exec command failed"; else { echo "<pre>"; foreach($output as $line) echo htmlspecialchars("$line\n"); echo "</pre>"; } ?>
<?php echo "<pre>"; // Enables viewing of the spaces // Pad to 15 spaces printf("The result is $%15f\n", 123.42 / 12); // Pad to 15 spaces, fill with zeros printf("The result is $%015f\n", 123.42 / 12); // Pad to 15 spaces, 2 decimal places precision printf("The result is $%15.2f\n", 123.42 / 12); // Pad to 15 spaces, 2 decimal places precision, fill with zeros printf("The result is $%015.2f\n", 123.42 / 12); // Pad to 15 spaces, 2 decimal places precision, fill with # symbol printf("The result is $%'#15.2f\n", 123.42 / 12); ?>
<?php echo "<pre>"; // Enables viewing of the spaces $h = 'Rasmus'; printf("[%s]\n", $h); // Standard string output printf("[%12s]\n", $h); // Right justify with spaces printf("[%-12s]\n", $h); // Left justify with spaces printf("[%012s]\n", $h); // Zero padding printf("[%'#12s]\n\n", $h); // Use the custom padding character '#' $d = 'Rasmus Lerdorf'; printf("[%12.8s]\n", $d); // Right justify, cutoff of 8 characters printf("[%-12.12s]\n", $d); // Left justify, cutoff of 12 characters printf("[%-'@12.10s]\n", $d); // Left justify, pad '@', cutoff 10 chars ?>