0

I am using textpad to create php scripts. Now is there anything I can use with textpad, or is there a way to debug with textpad. I am a few of my code echo out and I am still not getting the results I am wanting my page to do. So I am thinking my code needs some debugging. I will post the code below and I am sure many of you will agree it needs debugging too. I know alot of people are probally saying I should not use what I am using but this is what I am being tought to use.

    <?php
function dbConnect(){
// Connect to the database
$hostname="localhost";
 $database="tblFile";
 $mysql_login="*****";
 $mysql_password="*****";

 if(!($db=mysql_connect($hostname, $mysql_login, $mysql_password))){
    echo"error on connect";
 }
 else{
    if(!(mysql_select_db($database,$db))){
        echo mysql_error();
        echo "<br />error on database connection. Check your settings.";
    }
    else{
                echo "I have successfully made a connection to my database and everything
 is working as it should.";
        }




}

$aryImages=array("image/jpeg","image/png");
$aryDocs=array("application/msword","application/pdf","video/x-msvideo");
$filename=filenameSafe($_FILES['upload']['name']);
$fileType=$_FILES["upload"]["type"];
if (in_array($_FILES["upload"]["type"],$aryImages)){
    createThumb($fileType,$_FILES['upload']['tmp_name'],$filename,100,100);
}
elseif (in_array($_FILES["upload"]["type"],$aryDocs)){
    move_uploaded_file($_FILES['upload']['tmp_name'],
"/home/valerie2/public_html/elinkswap/snorris/upload/".$filename);
    $aryColumns=array("sessionID"=>$curSess,"fileName"=>$filename,"fileType"=>$fileType,"thumbFileName"=>$thumbFilename,"dateCreated"=>date('Y-m-d H:i:s'));
    dbInsert($filename,$aryColumns,$_FILES["upload"]["type"]);
}


    else{

    echo "File Uploaded";
  }

function createThumb($type,$tmpname,$filename,$new_w,$new_h){
    $thumbFilename="tmb-".$filename;
    echo $type;
    echo "<br>".$tmpname;
    if (is_numeric(strpos($type,"jpeg"))){
        $src_img=imagecreatefromjpeg($tmpname);
    }
    if (is_numeric(strpos($type,"png"))){
        $src_img=imagecreatefrompng($tmpname);
    }
    $old_x=imageSX($src_img);
    $old_y=imageSY($src_img);
    if ($old_x > $old_y) {
        $thumb_w=$new_w;
        $thumb_h=$old_y*($new_h/$old_x);
    }
    if ($old_x < $old_y) {
        $thumb_w=$old_x*($new_w/$old_y);
        $thumb_h=$new_h;
    }
    if ($old_x == $old_y) {
        $thumb_w=$new_w;
        $thumb_h=$new_h;
    }
    $dst_img=imagecreatetruecolor($thumb_w,$thumb_h);
    imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
    if (is_numeric(strpos($type,"jpeg"))){
        imagejpeg($dst_img,"/home/valerie2/public_html/elinkswap/imageupload/upload/".$thumbFilename);
        imagejpeg($src_img,"/home/valerie2/public_html/elinkswap/imageupload/upload/".$filename);
    }
    if (is_numeric(strpos($type,"png"))){
        imagepng($dst_img,"/home/valerie2/public_html/elinkswap/imageupload/upload/".$thumbFilename);
        imagepng($src_img,"/home/valerie2/public_html/elinkswap/imageupload/upload/".$filename);
    }
    imagedestroy($dst_img);
    imagedestroy($src_img);
    dbInsert($filename,$thumbFilename,$type);
}
function filenameSafe($filename)
{
    // Lower case
    $filename = strtolower($filename);

    // get extension
    $ext = pathinfo($filename, PATHINFO_EXTENSION);

    // Replace spaces with a ’_’
    $filename = str_replace(" ", "_", $filename);

    // Replace non-alphanumerics (except underscores)
    $filename = preg_replace('/\W/', '', $filename);

    // append the timestamp
    $filename = $filename . time();

    // create an md5 hash
    $result = md5($filename);

    // ensure the string is safe for the db query
    $result = mysql_real_escape_string($result);

    dbConnect();

    $SQL="SELECT fileId FROM tblFile WHERE fileName='".$result.".$ext'";

    $rs = mysql_query($SQL);
    if (mysql_num_rows($rs) > 0) {
        $result = str_replace(".$ext", time(), $result);
        $result = "$result.$ext";
    }
    return $result;
}


function dbInsert($filename,$thumbFilename,$type){
    dbConnect();
    $SQL="INSERT Into tblFile (fileName,thumbFileName,fileType) values('".$filename."','".$thumbFilename."','".$type."')";
    //echo $SQL;
    mysql_query($SQL);


}


}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>File Upload</title>
<link href="styles.css" type="text/css" rel="stylesheet" />
</head>
<body>


<form enctype="multipart/form-data" action="upload.php" method="post">

Select File: <input type="file" name="upload">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000"/>
<input name="Submit" type="submit" value="Upload">

</form>
</html>

I have look up myself to see if there is anyway of debugging in textpad but I am getting nothing.

1
  • 3
    You are wide open to SQL injection. You really need to learn to do prepared queries with PDO, unless you want your server to be hacked within days of having this script running. Commented Dec 5, 2011 at 15:06

3 Answers 3

2

IMHO the easiest way to debug PHP with a non-PHP-IDE is establishing an external logfile.

In your .htaccess you can specify something like

php_value display_errors 1
php_value error_reporting 2147483647
php_value error_log /var/log/php/php_error.log

Make sure this file is writable for your webserver/php process. Withing your code you can simply use the method error_log to log stuff into your file.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm afraid you have to switch to a "bigger" IDE to be able to do some serius debugging. You must try Eclipse or NetBeans with Xdebug. If you do a Google search you will find lot of tutorial to setup a debugging environment with eclipse php xdebug or netbeans php xdebug.

If you need something very very simpler, but better than calling everytime an echo or a var_dump, you should try FirePHP with FirePHP extension for Firefox.

Comments

0

just by a simple copy paste in a editor with syntax highlight I saw that all the functions you wrote are in the dbConnect function and this doesn't look normal.

also, if you would indent your code it would be a lot easier to read it, so to spot problems. if your server has error reporting and error display on you should get some messages

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.