need help to get this upload page to work

Discussion in 'PHP' started by dougvcd, Apr 30, 2009.

  1. #1
    have this page which is suppose to check upload is an jpg etc
    all i get at moment is a blank screen if any one can help
    cheers
    Doug
    
    <head>
      <title>Lost and Found Registration</title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     
    </head>
    <body>
     
     
    <?php 
     $correctAnswer = 4;
     if (isset($_POST['leg']) && $_POST['leg'] != $correctAnswer) 
    { 
        // send em away 
        header("Location: http://www.lostpetsplymouth.com/error-page.html"); 
        exit(); 
    }
     
    //This gets all the other information from the form
    $state=$_POST['state']; 
    $type=$_POST['type'];
    $area=$_POST['area'];
    $desc=$_POST['desc'];
    $name=$_POST['name'];
    $email=$_POST['email'];
    $tel=$_POST['tel'];
    $pnum=$_POST['pnum'];
    $date = date("d-m-Y");
    $pname=$pnum.".jpg";
    
    if ((($_FILES["pic"]["type"] == "image/gif")
    || ($_FILES["pic"]["type"] == "image/jpeg")
    || ($_FILES["pic"]["type"] == "image/pjpeg"))
    && ($_FILES["pic"]["size"] < 20000))
      {
      if ($_FILES["pic"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["pic"]["error"] . "<br />";
        }
      
      else
         move_uploaded_file($_FILES['pic']['tmp_name'], 'uploads/'.$pname); 
        }
      }
      else
      {
      echo "Invalid file";
      }
    
    // Connects to your Database 
     $dbh=mysql_connect("localhost", "car", "57") or die('I cannot connect to database because: ' .mysql_error()) ; 
    mysql_select_db("cpets"); 
    
    //Writes the information to the database 
    mysql_query("INSERT INTO `register` VALUES ('$id', '$state', '$type', '$area', '$desc', '$name', '$email', '$tel', '$pname', '$date')");
    
    //compose the mail message        
     $msg= "New Record\n Region: $region, Name: $name, Email: $email ";       
    //send the mail       
    mail('d@gmail.com','New Record',$msg)
    
    ?> 
    
    <?php
    echo "your information has been added to the directory You will be redirected in three seconds!><br /><br /> 
    
    		<div class='info'>If you don't wish to wait, <a href='index.html'>click here</a>";			
    		echo'<meta http-equiv="REFRESH" content="2;url=index.html">';
    ?>					
     
     
    </body>
    </html>
    
    PHP:

     
    dougvcd, Apr 30, 2009 IP
  2. Stylesofts

    Stylesofts Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hello,

    Plz replace

    move_uploaded_file($_FILES['pic']['tmp_name'], 'uploads/'.$pname)

    with this code

    if(is_uploaded_file($_FILES['pic']['tmp_name']))
    {
    copy($_FILES['file']['tmp_name'],$'uploads.'/'.$pname);
    }

    Hope this is what u need

    :)

    Regards,
    Stylesofts Developing Team.
     
    Stylesofts, Apr 30, 2009 IP
  3. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    no that didnt do any thing
    thanks any way
    Doug
     
    dougvcd, Apr 30, 2009 IP
  4. sunnyverma1984

    sunnyverma1984 Well-Known Member

    Messages:
    342
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    120
    #4
    replace
     if (isset($_POST['leg']) && $_POST['leg'] != $correctAnswer) 
    PHP:
    with

     if (isset($_POST['leg']) && $_POST['leg'] == $correctAnswer) 
    PHP:
     
    sunnyverma1984, Apr 30, 2009 IP
  5. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    sorry but that didnt work must be some thing bad
    cheers
    Doug
     
    dougvcd, Apr 30, 2009 IP
  6. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Keep move_uploaded_files, but change the 'uploads/' part to an absolute location => accountname/www/uploads/ -- whatever the path is.
     
    SHOwnsYou, Apr 30, 2009 IP
  7. sliver

    sliver Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    try to put a
    print_r($_FILES);
    and check if you see something strange (or post here the results).
    also be sure the

    uploads/

    directory is writable.
     
    sliver, Apr 30, 2009 IP
  8. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    ok lets start again here is my page to upload photo to server works fine
    what i want to do is only allow jpg file to upload
    cheers
    Doug

    <?php 
     $correctAnswer = 4;
     if($_POST['leg'] != $correctAnswer) {
     // send em away
     header("Location: http://www.lostpetsplymouth.com/error-page.html");
     exit();
     } 
    //This gets all the other information from the form
    $state=$_POST['state']; 
    $type=$_POST['type'];
    $area=$_POST['area'];
    $desc=$_POST['desc'];
    $name=$_POST['name'];
    $email=$_POST['email'];
    $tel=$_POST['tel'];
    $pnum=$_POST['pnum'];
    $date = date("d-m-Y");
    $pname=$pnum.".jpg";
    move_uploaded_file($_FILES['pic']['tmp_name'], 'uploads/'.$pname);
    // Connects to your Database 
     $dbh=mysql_connect("localhost", "vcd", "57") or die('I cannot connect to database because: ' .mysql_error()) ; 
    mysql_select_db("pets"); 
    
    //Writes the information to the database 
    mysql_query("INSERT INTO `register` VALUES ('$id', '$state', '$type', '$area', '$desc', '$name', '$email', '$tel', '$pname', '$date')");
    
    //compose the mail message        
     $msg= "New Record\n Region: $region, Name: $name, Email: $email ";       
    //send the mail       
    mail('d@gmail.com','New Record',$msg)
    
    ?> 
    
    <?php
    echo "your information has been added to the directory You will be redirected in three seconds!><br /><br /> 
    
    		<div class='info'>If you don't wish to wait, <a href='index.html'>click here</a>";			
    		echo'<meta http-equiv="REFRESH" content="2;url=index.html">';
    ?>					
    PHP:
    or should the code be on the form page?
     
    dougvcd, Apr 30, 2009 IP
  9. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Use this code:
    
    if (!($uploaded_type=="image/jpg")) {
    echo "You may only upload JPG files.<br>";
    $ok=0;
    }
    Code (markup):
     
    SHOwnsYou, Apr 30, 2009 IP
  10. Stylesofts

    Stylesofts Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hello,

    Okay so u want to upload only the jpeg images then do like this

    
    function getExtension($fileName)
    {
    	$ext_arr = explode('.',$fileName);
    	$cnt = count($ext_arr);
    	$extension = $ext_arr[$cnt-1];
    	return $extension;
    }
    
    
    $extension = getExtension($_FILES['pic']['tmp_name']);
    if($extension=="jpg" || $extension=="JPG" || $extension=="jpeg" || $extension=="JPEG")
    {
    if(is_uploaded_file($_FILES['pic']['tmp_name']))
    {
    copy($_FILES['file']['tmp_name'],$'uploads.'/'.$pname);
    }
    }
    
    
    PHP:
    Now this will work,

    Regards,
    Stylesofts Developing Team
     
    Stylesofts, Apr 30, 2009 IP
  11. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #11
    ok thanks for that but where do i put it
    cheers
    Doug
     
    dougvcd, Apr 30, 2009 IP
  12. Stylesofts

    Stylesofts Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Hello,

    Okay here is the code,plz use this one

    
    
    
    <?php
    $dbh=mysql_connect("localhost", "vcd", "57") or die('I cannot connect to database because: ' .mysql_error()) ; mysql_select_db("pets"); 
    function getExtension($fileName)
    {
    	$ext_arr = explode('.',$fileName);
    	$cnt = count($ext_arr);
    	$extension = $ext_arr[$cnt-1];
    	return $extension;
    }
    $correctAnswer = 4; if($_POST['leg'] != $correctAnswer) {
    	// send em away 
    header("Location: http://www.lostpetsplymouth.com/error-page.html");
    exit();
    }
    
    $extension = getExtension($_FILES['pic']['tmp_name']);
    if($extension=="jpg" || $extension=="JPG" || $extension=="jpeg" || $extension=="JPEG")
    {
    $state=$_POST['state'];
    $type=$_POST['type'];
    $area=$_POST['area'];
    $desc=$_POST['desc'];
    $name=$_POST['name'];
    $email=$_POST['email'];
    $tel=$_POST['tel'];
    $pnum=$_POST['pnum'];
    $date = date("d-m-Y");
    $pname=$pnum.".jpg";
    // Connects to your Database  
    
    //Writes the information to the database 
    $selectQuery = "INSERT INTO `register` VALUES ('$id', '$state', '$type', '$area', '$desc', '$name', '$email', '$tel', '$pname', '$date')";
    $resultSet = mysql_query($seelctQuery);
    if($resultSet == true)
    {
    $msg= "New Record\n Region: $region, Name: $name, Email: $email ";       
    //send the mail       
    mail('d@gmail.com','New Record',$msg)?>
    <?php echo "your information has been added to the directory You will be redirected in three seconds!><br /><br />         <div class='info'>If you don't wish to wait, <a href='index.html'>click here</a>";
    echo'<meta http-equiv="REFRESH" content="2;url=index.html">';
    
    if(is_uploaded_file($_FILES['pic']['tmp_name']))
    {
    copy($_FILES['file']['tmp_name'],'$uploads'.'/'.$pname);
    }
    }
    //compose the mail message         
    }
    else
    {
    echo("Invalid file format, Please upload the image with 'JPEG' type");
    }
    ?> 
    
    
    
    PHP:
    Now this will work,

    Regards,
    Stylesofts Developing Team
     
    Stylesofts, Apr 30, 2009 IP
  13. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #13
    sorry to say that i tried it out an gave me error saying not jpeg file when i know it was
    bug some where
    cheers
    Doug
     
    dougvcd, May 2, 2009 IP