Tag Archives: PHP

A PHP Class for uploading files to server

A PHP class to upload files to server

The software includes the functions as below:
1, according to the date, produce a subfolder, like: 201010;
2, check if the subfolder exists; if it doesn’t exist, build the subfolder in the current path; and it will also build a subfolder named “small” in this subfolder, which will be used when a big image is uploaded;
3, upload the file on this subfolder; If the upload file is an image and the image is bigger then expected, a small one will be produced and put in the “small” subfolder. On the return string, the small one will be shown in <img> and linked to the orignal one.

Software download: upload.v1.1.rar    under license GNU GPL

How to check if a folder or a file exists and build a folder with PHP

This is an example to check if a folder exists and if not exist, build this folder:
//produce the string according to the date and the format is like this: 201010
$subfolder = gmdate(“Ym”, time());

//file_exists() check if the folder or a file exists; if it exists, reture true, otherwise false.
// dirname(__FILE__) returns the current folder path.
$dir = dirname(__FILE__) . $subfolder;
if (file_exists($dir)) {
echo “this folder” . $dir . “exits”;
} else {
mkdir($dir,0777);
}
?>

An example to check if a file exists under the folder:
$dir = dirname(__FILE__) . “\\201010”;
$file = $dir . “\\data.txt”;

if (file_exists($file)) {
echo “In the folder: ” . $dir . “, the file ” . $file . “exits”;
} else {
echo “In the folder” . $dir . “,” . $file . “doesn’t exist”;
}
?>

I found that the folders that are created by the script above on the web hosting space and the files in them can’t be deleted through the interface provided by cPanel.

The difference of CSS ID & CLASS

The ID is used for one HTML tag.

For example:

<style type=”text/css”>
<!– #aaa {color:#fff} –>
</style>

<p id=”aaa”>xxx</p>

One CLASS is used for many HTML tags generally.

For example:

<style type=”text/css”>
<!– .aaa {color:#fff} –>
</style>

<p>xxx</p>
<table>xxx</tabe>

Read and write file on the hard disk with PHP

Read the file:

<? Php
$ File_name = “data.txt”;
$ File_pointer = fopen ($ file_name, “r”);
$ File_read = fread ($ file_pointer, filesize ($ file_name));
fclose ($ file_pointer);
print_r (“to read the contents of the file:”. $ file_read);
?>

Write file:

<? Php
$ File_name = “data.txt”;
$ File_pointer = fopen ($ file_name, “w”);
fwrite ($ file_pointer, “to write the content”);
print_r (“Data successfully written to the file.”);
?>

My program:

Read the file and put it in textarea. When some changes are made and submitted, the changes would be written in the file, and then read, displayed in the textarea. All is in one file named board.php.

board.php

<? Php
 $ Board = trim ($ _POST [“board”]);
 $ File_name = “board.php”;
 
 if ($ board! = null) (
   $ File_pointer = fopen ($ file_name, “w”);
   fwrite ($ file_pointer, $ board);
   print_r (“Data successfully written to the file.”);
 )

    $ File_pointer = fopen ($ file_name, “r”);
    $ File_read = fread ($ file_pointer, filesize ($ file_name));
    fclose ($ file_pointer);
?>
<Table>
  <Tr bgcolor = “# CDDADC” height = “30px;”>
    <Td align = “right”>
      <Form method = “post” action = “board.php”>
        <Textarea name = “board” cols = “80” rows = “15”> <? Php if ($ board == null) echo $ file_read; else echo $ board;?> </ Textarea>
        <Input type = “submit” value = “submit” />
      </ Form>
    </ Td>
  </ Tr>
</ Table>