PHP Uploading Problem

Discussion in 'PHP' started by 7awaka, Oct 26, 2008.

  1. #1
    Hi

    I have been trying for some time to upload files either to a mysql database or to a directory.

    I have failed at both.
    in the case of uploading to a database i have successfully uploaded the size and the name of the files to the db tables but no content.

    in the case of uploading files to a directory, the code is fine but no files are copied to the directory location for some reason although i got a success message.

    heres the code:

    the first page is the upload page

    entry.php
    <?php
    echo'
    <form action="File.php" Method="post" enctype="multipart/form-data">
    <input type="hidden" name="upload" value="100000">
    <center> <input name="file" type="file" id="file" size="20">
    <input type="submit" value="upload">
    </form> ';
    ?>


    The second is ofcourse the page that takes the variables and actually uploads the file
    File.php:-
    <?php
    require($_SERVER["DOCUMENT_ROOT"]. "/config/cv_config.php");
    $connection = mysql_connect($db_host,$db_user,$db_password) or die ("error connecting") ;
    mysql_select_db($db_name, $connection);

    if(isset($_POST['upload']) && $_FILES['file']['size'] > 0)
    {
    $fileName = $_FILES['file']['name'];
    $tmpName = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];
    $fileType = $_FILES['file']['type'];
    $directory= '/upload';
    $path = "$directory/$fileName" ;

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

    fclose($fp);

    move_uploaded_file($tmp_name, $path);
    if(!move_uploaded_file($tmpName, $path))
    {
    echo 'Successful<BR/><a href='.$path.'> '.$path.' </a>';

    }
    else
    {
    echo 'error<a href='.$tmpName.'> '.$tmpName.' </a>';
    }



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

    include 'library/config.php';
    include 'library/opendb.php';

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

    mysql_query($query) or die('Error, query failed');
    include 'library/closedb.php';

    echo "<br>File $fileName uploaded<br>";
    }

    $query1 = "SELECT * FROM upload";
    $result = mysql_query($query1, $connection);


    echo '

    <table bgcolor="black" border="2" width="75%" cellspacing="1" cellpading="2" valign="top" >


    <tr>
    <td width="1%" valign="to[" nowrap bgcolor="Blue" >
    <b><font face="arial" size="2.5" color="white"><center>
    Name </td>
    <td width="10%" valign="to["wrap bgcolor="#EEEEEE"><b><font face="Comic Sans" size="2">'
    .$fileName.'
    </td>
    </tr>


    <tr>
    <td width="1%" valign="to[" nowrap bgcolor="Blue">
    <b><font face="arial" size="2.5" color="white"><center>
    Size </td>
    <td width="10%" valign="to["nowrap bgcolor="#EEEEEE"><b><font face="Comic Sans" size="2">'
    .$fileSize.'
    </td>
    </tr>



    <tr>
    <td width="1%" valign="to[" nowrap bgcolor="Blue">
    <b><font face="arial" size="2.5" color="white"><center>
    Type
    <td width="10%" valign="to["wrap bgcolor="#EEEEEE"><b><font face="Comic Sans" size="2">'
    .$fileType.'

    </td>

    </tr>

    <tr>
    <td width="1%" valign="to[" nowrap bgcolor="Blue">
    <b><font face="arial" size="2.5" color="white"><center>
    Content </td>
    <td width="10%" valign="to["nowrap bgcolor="#EEEEEE"><b><font face="Comic Sans" size="2">'
    .$content.'
    </td>
    </tr>

    </table>';
    ?>

    I get a successful message plus the directory path in hyperlink which i press on to get a 404 error page
    and below it is the table that states the name,type,size and content of the file that is uploaded...all present but the content.

    Why isnt it working....HELP!
     
    7awaka, Oct 26, 2008 IP
  2. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #2
    Beside syntax errors I don't see a problem.

    Did you try this on your localhost and it worked?

    Try using files of small sizes (1 KB or so).

    Are you the servermaster? If so, what are the max upload size for PHP in php.ini.

    Also try echo $content after you read the file and see if it actually has anything.

    Peace,
     
    Barti1987, Oct 26, 2008 IP
  3. 7awaka

    7awaka Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I did use small files and my max upload size in php.ini is 10M

    I have an echo for content already in the code if you examine it closely


    Any other suggestions?
     
    7awaka, Oct 26, 2008 IP
  4. 7awaka

    7awaka Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    actually i primarly worked on the localhost on my pc but when i tried the whole thing on my remote host it worked...wierd

    Its either the php or mysql version..right?!
     
    7awaka, Oct 26, 2008 IP
  5. smbasha

    smbasha Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    along this make sure your target directory has writer permissions.
     
    smbasha, Oct 29, 2008 IP