Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: PHP Lesson

  1. #1
    Join Date
    Jul 2006
    Posts
    12

    Default PHP Lesson

    PHP Lesson php source code PHP PHP PHP

  2. #2
    Join Date
    Jul 2006
    Posts
    12

    Default Creating A File Upload Script :

    Creating A File Upload Script :

    First we need to create the database table with the following SQL:
    Code:
    CREATE TABLE `uploads` (
    `id` int(10) unsigned NOT NULL auto_increment,
    `whenuploaded` datetime NOT NULL default '0000-00-00 00:00:00',
    `ipaddress` varchar(15) NOT NULL default 'unknown',
    `imageloc` varchar(255) NOT NULL default 'unknown',
    `imagesize` int(10) unsigned default NULL,
    `imagetype` varchar(30) default NULL,
    PRIMARY KEY (`id`)
    ) TYPE=MyISAM;

  3. #3
    Join Date
    Jul 2006
    Posts
    12

    Default contiune

    Code:
    Then here's the PHP code you need at the top of the page where you're going to have your upload form. You need to change the database access details at the top and the 'defines' to specify your domain and path names. Create the DEST_DIR directory on the server with 777 permissions.
    
    define('MAX_ALLOWED_FILE_SIZE', 1024000); // change 1024000 with the size U wish to allow people to upload
    define("DEST_DIR", '/upload/1/'); / / set your upload dir
    define('DEST_PATH', '/home/yourcpanelid/public_html' . DEST_DIR); //path to your dir
    define('DEST_URL', 'http://yourdomain.com' . DEST_DIR); 
    
    $allowed_types = array("image/gif", "image/pjpeg", "image/x-png", "image/bmp"); //set the allowed extensions
    
    $dbhost = "localhost";
    $dbname = "yourdb_name";
    $dbuser = "yourdb_user";
    $dbpass = "yourdb_password";
    
    $errormessage = "Please enter file to be uploaded."; //change this message to whatever U want

  4. #4
    Join Date
    Jul 2006
    Posts
    12

    Default contiune

    Code:
    if ((isset($_REQUEST['form_submit'])) && ('form_uploader' == $_REQUEST['form_submit']))
    {
    $picfile_name = $_FILES['picfile']['name'];
    $picfile_type = $_FILES['picfile']['type'];
    $picfile_size = $_FILES['picfile']['size'];
    $picfile_temp = $_FILES['picfile']['tmp_name'];

  5. #5
    Join Date
    Jul 2006
    Posts
    12

    Default contiune

    Code:
    if (MAX_ALLOWED_FILE_SIZE >= $picfile_size)
    {
    if (in_array($picfile_type, $allowed_types))
    {
    if (is_uploaded_file($_FILES['picfile']['tmp_name']))
    {
    
    if (file_exists(DEST_PATH . $picfile_name))
    {
    $unique_id = time();
    $picfile_name = $unique_id . '_' . $picfile_name;
    }

  6. #6
    Join Date
    Jul 2006
    Posts
    12

    Default contiune

    Code:
    if (move_uploaded_file($picfile_temp, DEST_PATH . $picfile_name))
    {
    $errormessage = "File uploaded as:
    " . DEST_URL . $picfile_name . "";
    if(mysql_connect($dbhost, $dbuser, $dbpass))
    {
    if(mysql_select_db($dbname))
    {

  7. #7
    Join Date
    Jul 2006
    Posts
    12

    Default

    Code:
    $sql1 = "INSERT1 INTO uploads (whenuploaded, ipaddress, imageloc, imagesize, imagetype) VALUES (";
    $sql1 .= "'" . date("Y-m-d H:i:s") . "',";
    $sql1 .= "'" . $_SERVER['REMOTE_ADDR'] . "',";
    $sql1 .= "'" . DEST_DIR . $picfile_name . "',";
    $sql1 .= "" . $picfile_size . ",";
    $sql1 .= "'" . $picfile_type . "')";

  8. #8
    Join Date
    Jul 2006
    Posts
    12

    Default contiune

    Code:
    if (!mysql_query($sql1))
    {
    $errormessage .= "
    Query failed [$sql1].";
    }
    }
    else
    {
    $errormessage .= "
    Could not select database.";
    }
    }
    else
    {
    $errormessage .= "
    Could not connect to database.";
    }
    }
    else
    {
    $errormessage = "File upload failed for obscure reasons (error code: " . $_FILES['picfile']['error'] . ").";
    }
    }
    else
    {
    $errormessage = "No file uploaded.";
    }
    }
    else
    {
    $errormessage = "Invalid file type.";
    }
    }
    else
    {
    $errormessage = "File too big (maximum size is " . MAX_ALLOWED_FILE_SIZE . ").";
    }
    $_REQUEST['form_submit'] = "";
    }
    
    ?>

  9. #9
    Join Date
    Jul 2006
    Posts
    12

    Default Multilingual pages

    Multilingual pages
    So you want to create a page (website) that is accessible on different languages ? In this tutorial we will make a page available for russian and english visitors.

    There are some several ways you can choose to implement this.
    1.) You can make a simple gateway ('splash') page and users will have to click on the link that represents the chosen language.
    2.) You can ask users to choose the language and store this information as a cookie on the browser so they only have to set the language once.
    3.) You can read HTTP request data sent by the browser to find the local language preference of the client machine.
    It is obvious the th
    ir
    d method is more convenient for the users because they don't need to do something, it is all done automatically.
    So here we go to the coding. When the web server receives the HTTP request it sets value for the $HTTP_ACCEPT_LANGUAGE predefined variable. This variable contains the language set by operation system or by the user in the browser configuration settings.


    All we have to do is to check the value of that variable and then output the page:
    Code:
    $pos = strpos($HTTP_ACCEPT_LANGUAGE, "ru");
    
    if ($pos === false) {
    
    include('russian_page.html" title="Phrase: html">html');
    
    }
    
    else{
    
    include('english_page.html" title="Phrase: html">html');
    
    }
    Take a look at '===' symbols. It is identical check which means two value should be equal and of the same type to generate TRUE. So the code shown above will show russian version of the page if there is 'ru' substring in the HTTP_ACCEPT_LANGUAGE and it will output english page otherwise.

    Unfortunately not all browsers correctly use language options but be sure that Internet Explorer (versions 4+) will do everything right. It is still recommended to include a link to switch language manually somewhere on your page. After user clicks on this link you should use cookies to store the chosen language on the client side.

  10. #10
    Join Date
    Jul 2006
    Posts
    12

    Default Date and time

    Date and time
    Here are special meanings of some symbols which you can use in format string (first parameter)

    * d - day of the month, numeric, 2 digits (with leading zeros)
    * D - day of the week, textual, 3 letters; i.e. "Fri"
    * F - month, textual, long; i.e. "January"
    * h - hour, numeric, 12 hour format
    * H - hour, numeric, 24 hour format
    * i - minutes, numeric
    * j - day of the month, numeric, without leading zeros
    * l (lowercase 'L') - day of the week, textual, long; i.e. "Friday"
    * m - month, numeric
    * M - month, textual, 3 letters; i.e. "Jan"
    * s - seconds, numeric
    * S - English ordinal suffix, textual, 2 characters; i.e. "th", "nd"
    * Y - year, numeric, 4 digits
    * w - day of the week, numeric, 0 represents Sunday
    * y - year, numeric, 2 digits
    * z - day of the year, numeric; i.e. "299"
    You can include any other symbols in format string and they should be outputted as they are. Only above symbols are replaced with their time and date values.
    Examples:
    print(date( "l dS of F Y h:i:s" ));
    Output:
    Friday 04th of January 1994 3:52:31
    print(date("Current time is: H (hours), i (minutes), s (seconds)"));
    output:
    Current time is: 12 (hours), 34 (minutes), 13 (seconds)
    mktime - get timestamp for date and time value
    Syntax: int mktime (int hour, int minute, int second, int month, int day, int year);

    Timestamp has been created to represent date / time value as a single integer number. So any logical date and time can be converted and used as special integer called timestamp. That's how you are able to compare two time moments as just two integer numbers. This timestamp contains the number of seconds between the Unix Epoch (January 1 1970) and the time specified. For example, January first of 1997th year will be less number than January second of that year.
    In databases the date type fields usually stored as timestamp integers. So to create query to get all records in specified time period you can use mktime() function to get the boundary timestamp integers. If you combine both date and mktime function, you will be able to perform any time operation that you can imagine (and you don't need time machine or something :) )

    Example:
    Let's print date that happened 7 days ago:
    $m=date('m');
    $d=date('d');
    $Y=date('Y');
    print(date('Y-m-d', mktime(0,0,0, $m, $d-7, $Y)));
    Note that PHP mktime()'s arguments sequence is different from standart UNIX mktime function. Most right arguments can be omitted and they will be set to current date time values. ьktime also would make all needed corrections if after ariphmetic calculations you would get incorrect date, for example 32nd of Febrary :).
    Example. All lines print "Jan-01-1998".
    print(date("M-d-Y", mktime(0,0,0,12,32,1997)));
    print(date("M-d-Y", mktime(0,0,0,13,1,1997)));
    print(date("M-d-Y", mktime(0,0,0,1,1,1998)));

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •