PHP : image upload and display problem Help me out plzzz?

Discussion in 'PHP' started by rafi, May 15, 2010.

  1. #1
    This is my code
    <html>
    <body>
    <form action="uploading.php" method="post" enctype="multipart/form-data">
    <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
    <tr>
    <td width="246">
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
    <input name="userfile" type="file" id="userfile">
    </td>
    <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    <?php
    if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
    $con=mysql_connect("localhost" ,"root" ,"");
    mysql_select_db("rafi");
    {
    $fileName = $_FILES['userfile']['name'];
    $tmpName = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];

    $fp = fopen($tmpName, 'r');
    $content = fread($fp, filesize($tmpName));
    $content = addslashes($content);
    fclose($fp);

    if(!get_magic_quotes_gpc())
    {
    $fileName = addslashes($fileName);
    }


    $query = "INSERT INTO upload (name, size, type, content ) ".
    "VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

    mysql_query($query) or die('Error, query failed');

    $blah=mysql_query("Select * FROM upload");
    while($row=mysql_fetch_assoc($blah))
    echo $row['name'];
    }
    ?>
    when i upload any pic it did'nt show ?
    plz help me out!
     
    rafi, May 15, 2010 IP
  2. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #2
    rather use this
    or die(mysql_error());
    PHP:
    or use file_get_contents instead of fopen
     
    gapz101, May 15, 2010 IP
  3. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #3
    assuming that your upload script is working and you see the images in your upload directory [check]
    then the <image> path's your problem
     
    bartolay13, May 15, 2010 IP
  4. live.co.uk

    live.co.uk Banned

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    WHY PEOPLE STILL TRY TO USE NEW ONE
    YOU CAN ESAY USE ONE OF THESE
     
    live.co.uk, May 15, 2010 IP
  5. manko24

    manko24 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    data base is not used to store binary content , use move_uploaded_files($src,$destination);
    give $destination a unique name by using $serverName = uniqid();
    get the extension of file $ext = end(explode($filename,'.');
    in the database save the extension , filename , the unique file name , and mime type.
     
    manko24, May 16, 2010 IP
  6. largn81

    largn81 Peon

    Messages:
    237
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    use this code instaed:

    if(!get_magic_quotes_gpc())
    {
    $fileName = addslashes(file_get_contents($Userfile));
    }
    
    
    $query = "INSERT INTO upload (name, size, type, content ) ".
    "VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
    
    mysql_query($query) 
    mysql_close();
    
    $query="Select * FROM upload";
    $blah=mysql_query($query)
    
    while( list(*)=mysql_fetch_array($blah))
    echo "fileName";
    }
    ?>
    PHP:
     
    largn81, May 18, 2010 IP
  7. rafi

    rafi Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    <html>
    <body>
    <form action="uploading.php" method="post" enctype="multipart/form-data">
    <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
    <tr>
    <td width="246">
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
    <input name="userfile" type="file" id="userfile">
    </td>
    <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    <?php
    if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
    $con=mysql_connect("localhost" ,"root" ,"");
    mysql_select_db("rafi");
    {
    $fileName = $_FILES['userfile']['name'];
    $tmpName = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];


    if(!get_magic_quotes_gpc())
    {
    $fileName = addslashes(file_get_contents($userfile));
    }

    $query = "INSERT INTO upload (name, size, type, content ) "."VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

    mysql_query($query)
    mysql_close($con);
    $query=("Select * FROM upload");
    $blah=mysql_query($query)
    while(list(*)=mysql_fetch_array($blah))
    echo "FileName";
    ?>
    not workin
     
    rafi, May 19, 2010 IP
  8. largn81

    largn81 Peon

    Messages:
    237
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Just use this code below:

    <?php
    $maxfilesize = 2000000;
    
    if(isset($_POST['upload']) && $_FILES['userfile']['error'] === UPLOAD_ERR_OK && $_FILES['userfile']['size'] <= $maxfilesize)
    {
    $con = mysql_connect("localhost", "root", "") or die(mysql_error());
    
    mysql_select_db("rafi", $con) or die(mysql_error());
    
    $fileName = mysql_real_escape_string($_FILES['userfile']['name'], $con);
    $tmpName  = mysql_real_escape_string($_FILES['userfile']['tmp_name'], $con);
    $fileSize = mysql_real_escape_string($_FILES['userfile']['size'], $con);
    $fileType = mysql_real_escape_string($_FILES['userfile']['type'], $con);
    $content = mysql_real_escape_string(file_get_contents($_FILES['userfile']['tmp_name']), $con);
    
    $query = "INSERT INTO upload (name, size, type, content ) VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
    
    mysql_query($query) or die('Error, query failed:' . mysql_error());
    
    
    echo "<br>File $fileName uploaded<br>";
    }
    ?>
    
    
    <form method="post" action="uploading.php" enctype="multipart/form-data">
    <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
    <tr>
    <td width="246">
    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $maxfilesize; ?>">
    <input name="userfile" type="file" id="userfile">
    </td>
    <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
    </tr>
    </table>
    </form>
    
    PHP:
     
    Last edited: May 21, 2010
    largn81, May 21, 2010 IP