PHP

COMMON ERRORS IN PHP


Login .php

1              <html>
2              <body>
3              <form action="login.php" method="POST">
4              Username<input type="text" name="myname"><br>
5              Passsword<input type="password" name="password"><br>
6              <input type="submit" name="submit" value="click here"><br>
7              </form>
8              </body>
9              </html>
10           <?php
11           $name=$_POST['myname'];
12           $pass=$_POST['password'];
13           if($name&&$pass)
14           echo "username:=".$name."Password:=".$pass;
15           ?>


Parse error

It is because of the semicolon missing or bracket missing or not ending single
or double quotes correctly missing or extra brackets

I f we remove semicolon of the first php code it shows below error

$name=$_POST['myname']
$pass=$_POST['password'];
Solution :Put  a semicolon at the end of 11 th line



GET POST
Undefined variable and undefined index errors

In the above login page ‘myname’ and ‘password’ is unidentified  before post .So while page loading time
Below shown error comes.


Solution:We need to put an @ symbol at the begging or middle of that line
Or you can put  ‘error_reporting(0);’ on top of the php code.
<?php
@$name=$_POST['myname'];
@$pass=$_POST['password'];
if($name&&$pass)
echo "username:=".$name."Password:=".$pass;
?>
Or
<?php
error_reporting(0);
$name=$_POST['myname'];
?>

what does the @ operator do? It temporarily sets the error reporting level to 0 for that line. If that line triggers an error, the error handler will still be called, but it will be called with an error level of 0

error_reporting()
refer:
http://php.net/manual/en/function.error-reporting.php

                                                                                                                                                                   


Image and video uploading code (tested with PHP Version 5.4.17)

Before uploading video we need to change PHP the maximum upload file size

For doing that 

You need to set the value of upload_max_filesize and post_max_size in your php.ini :
; Maximum allowed size for uploaded files.
upload_max_filesize = 40M

; Must be greater than or equal to upload_max_filesize
post_max_size = 40M

PHP  the maximum upload file size

HTML form

<!DOCTYPE html>

<head>
<title></title>
</head>

<body>

<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file"><span>Filename:</span></label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

PHP handler (upload_file.php)

Change upload folder to preferred name. Presently saves to upload/
<?php

$allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma");
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

if ((($_FILES["file"]["type"] == "video/mp4")
|| ($_FILES["file"]["type"] == "audio/mp3")
|| ($_FILES["file"]["type"] == "audio/wma")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg"))

&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))

  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>
courtesy:
http://stackoverflow.com/questions/2184513/php-change-the-maximum-upload-file-size
http://stackoverflow.com/questions/18217964/upload-video-files-via-php-and-save-them-in-appropriate-folder-and-have-a-databa
                                                                                                                                                                    

rename uploaded file before saving it into a directory

Instead of
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
Use
$temp = explode(".",$_FILES["file"]["name"]);
$newfilename = rand(1,99999) . '.' .end($temp);
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newfilename;
Changed to reflect your question, will product a random number between 1-99999 and append the extension from the originally uploaded file.
rand(1,9999) cand be replaced with your id or name  while using in registration page.

courtesy:
http://stackoverflow.com/questions/18705639/how-to-rename-uploaded-file-before-saving-it-into-a-directory
                                                                                                                                                                 

Comments

Popular posts from this blog