Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Warning: fread(): supplied argument is not a valid stream resource

    Hi

    Iam new to PHP,while uploading the large pdf files im getting the following 2 warnings:

    Warning: fread(): supplied argument is not a valid stream resource

    Warning: fclose(): supplied argument is not a valid stream resource

    the folllowing is the code:

    <?php ob_start(); ?>
    <?php
    function login()
    { global $err;
    include("head.php");
    ?>


    <tr><td> <table border="0" cellpadding="0" cellspacing="0" height="150" width="200" align="center">
    <tr>
    <td height="20" align="center" bgcolor="#ffffff">
    <font color="#000000">Admin Login</font>
    </td>
    </tr>
    <tr>
    <td valign="top">
    <form action="" method="post"><br>
    <table border="0" cellpadding="3" cellspacing="0" align="center">
    <tr>
    <td width="64">
    <b>Username:</b></td>
    <td width="134"><input type="text" name="aname" value="<?php echo $name;?>" /></td>
    </tr>
    <tr>
    <td>
    <b>Password:</b></td>
    <td>
    <input type="password" name="apass" value="<?php echo $pass;?>"></td>
    </tr>
    <tr>
    <td colspan="2">
    <center><input type="hidden" name="maa" value="do_login"><input type="submit" name="submit" value="Submit"></center></td>
    </tr>
    </table>
    </form>

    </td></tr>
    </table> </td></tr>
    <tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td><?php echo $err; ?></td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr>
    <?php footer(); }

    function do_login()
    {

    global $err;
    $name="admin";
    $pass="admin";
    $adn=$_REQUEST['aname'];
    $adp=$_REQUEST['apass'];
    if(($name==$adn)&&($pass==$adp))
    {
    admin();
    }
    else
    {
    $err="<font color=\"#CC0033\"><div align=\"center\" class=\"style9\"><b>LOGIN_ERROR</b></div></font>";
    login();
    }

    }

    function admin()
    {
    include("head1.php"); ?>
    <tr><td align="center"><b><a href="index.php?maa=upload">Fileupload </a></b></td>></b></td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr>

    <? footer(); }
    function upload()
    {
    include("head1.php");
    echo "<title>Upload File To MySQL Database</title>";

    echo "<tr><td>";
    if(isset($_POST['upload']))
    {
    $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);
    $content = addslashes($content);

    fclose($fp);

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


    $con=mysql_connect("localhost","root","");
    mysql_select_db("a2z");
    $str_sql = "SELECT id,name,type,size,content FROM upload";
    $result = mysql_query($str_sql) or die(mysql_error());

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

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

    echo "<tr><td><br>File $fileName uploaded<br></td></tr>";
    } ?>


    <tr><td>&nbsp;</td></tr>
    <tr><td>
    <form action="" method="post" enctype="multipart/form-data" name="uploadform">
    <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
    <tr>
    <td width="246"><input type="hidden" name="MAX_FILE_SIZE" value="200000000"><input name="userfile" type="file" class="box" id="userfile">
    </td>
    <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
    </tr>
    </table>
    </form> </td></tr>

    <tr><td><a href="index.php?maa=download">Click here to check the uploaded files</a></td></tr>
    <tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr>


    <? footer(); }

    function download()
    {
    //footer();
    include("head1.php");
    if(isset($_GET['id']))
    {

    $con=mysql_connect("localhost","root","");
    mysql_select_db("a2z");

    $id = $_GET['id'];
    $query = "SELECT name, type, size, content FROM upload WHERE id = '$id'";
    $result = mysql_query($query) or die('Error, query failed');
    list($name, $type, $size, $content) = mysql_fetch_array($result);


    header("Content-Disposition: attachment; filename=$name");
    header("Content-length: $size");
    header("Content-type: $type");
    echo $content;
    mysql_close($con);

    exit;
    }
    $con=mysql_connect("localhost","root","");
    mysql_select_db("a2z");
    $query = "SELECT id, name FROM upload";
    $result = mysql_query($query) or die('Error, query failed');
    if(mysql_num_rows($result) == 0)
    {
    echo "Database is empty ";
    }
    else
    {
    while(list($id, $name) = mysql_fetch_array($result))
    {

    echo "<tr><td><a href=\"index.php?maa=download&id=$id\">$id&nbsp;&nbsp;$name</a><br></td></tr>";

    }

    footer();
    }
    mysql_close($con);
    }

    function logout()
    {
    include("head.php");
    ?>
    <form name="form1" method="post" action="">
    <tr bgcolor="#FFFFFF">
    <td colspan="2"><div align="center"><font color="#2A3F55" size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>LOG OUT </strong></font></div></td>
    </tr>
    <tr>
    <td><div align="center">Are you sure you want to log out ?<br><br>
    <input type="submit" name="submit" value="Yes">&nbsp;</div></td>
    </tr>
    <tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr>

    </form>
    <?php
    if(isset($_REQUEST['submit']))
    {
    header("Location: index.php");
    }
    footer();
    }





    $opt=$_REQUEST['maa'];
    switch($opt)
    {

    case "do_login":
    do_login();
    break;

    case "upload":
    upload();
    break;
    case "download" :
    download();
    break;

    case "logout" :
    logout();
    break;

    default:
    login();
    break;

    }






    function footer()
    { ?>

    </table>
    </td>
    </tr>


    <tr>
    <td height="63" valign="top" background="../chrometheme/bg.jpg">




    </td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    </tr>

    </table>

    <body>
    </body>
    </html>
    <?php } ?>
    <?php ob_end_flush(); ?>

    Any one know the solution,please reply.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Funny how you already posted this question and I already answered it.

  • #3
    New Coder
    Join Date
    Jan 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    hi

    i used the ob_start(); and ob_end_flush(); in my code,but if i tried to upload large pdf files like 7MB it will give an error,so what to do for that.

  • #4
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    You didn't say what your specific error was...
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.


  •  

    Posting Permissions

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