Hi,

Can anyone please help me with this simple (i'm sure) problem.

I can fully create a system where I can upload images and manage them properly, however for my own personal use I wish to create a similar system where I can store .txt, .nfo and .doc files on my (remote) server using PHP and MySQL.

I have tried using my code and changing the MIME type from image to text, which I thought would work great. NO!!

Even a simple 3 field table (id, title, file) will not work. I cannot seem to store the file entry in my database. The full details store without a problem (e.g. title, author, date, rating and so on) but the field I have designated for the nfo/doc/txt file remains blank. I DO get a file stored on my server, but it is a bunch of jargon like "phpmhQyRg". I can view the file through a browser (e.g. going via mydomain.com/dbase/phpmhQyRg) and it is the correct file, just horribly named and unreachable via my PHP script (e.g. showfile.php?id=2).

Can anyone please help me?

    why not store the path in the db and place all the files in a folder(s), then when displaying / whatever else you want to do show the path as ahyperlink to open or download or whatever....

    hth

    bastien

      I am storing all information in the table, also the type and subtype. This works fine for me when I'm storing doc, xls and txt.

      For the upload:
      if ($sendfile) {
      If(is_uploaded_file($fcontent)) {
      $fsize = filesize($fcontent);
      $mysqlPicture = addslashes(fread(fopen($fcontent, "r"), $fsize));
      $ftype = $fcontent_type;
      $farrtype = split("/", $fcontent_type, 2);
      $ftype = $farrtype[0];
      $fsubtype = $farrtype[1];
      if ($ftype == "image") {
      $fimagesize = getimagesize($fcontent);
      $fimagesizetext = $fimagesize[0]."*".$fimagesize[1];
      }
      else $fimagesizetext = "";
      $sql = "INSERT INTO Files";
      $sql .= "(FType, FSubType, FSize, FImageSize, FName, FFileName, FDescription, FContent)";
      $sql .= " VALUES('$ftype', '$fsubtype', $fsize";
      $sql .= ", '$fimagesizetext', '$fname', '$fcontent_name', '$fdescription', '$mysqlPicture')";

      	mysql_query($sql) 
      		or die(ferror("$PHP_SELF Insert ($sql)")); 
      
      	unlink($fcontent);
      
      	$fname=$fdescription="";
      } 
      else { 
      	echo"You did not upload any picture"; 
      } 

      }

      And when for sending to the client:
      $sql = "SELECT *";
      $sql .= " FROM Files";
      $sql .= " WHERE FNo=$no";

      $result = mysql_query($sql)
      or die(ferror("$PHP_SELF ($sql)"));

      $row=mysql_fetch_object($result);

      if ($row->FType != "image") header("Content-Disposition: attachment; filename=".$row->FFileName);

      Header( "Content-type: ".$row->FType."/".$row->FSubType);

      // echo "<head><title>$FBeskrivelse</title></head>";

      echo $row->FContent;

      mysql_free_result($result);

      I hope this is what you where looking for, and to help four you?

        Hi bastien, thx for your answer 🙂

        Thats basically what I want to do... but I am having trouble getting it to work as I want. Since I cant store the filename in the dbase, how do I link the file to its relevant dbase entry...?

          Hi Knut 🙂

          I will certainly read that, and let you know what happens if I can amend my code. Looks promising if you can store links to xls, txt and doc files though!

          Thanks to all 🙂

            I think Knut you are storing your data IN your database? I dont want to do that, I have a directory on my server.

            This is my (test) table:

            ID (int)
            Author (varchar)
            Group (varchar)
            file_name (varchar)

            The code I am using (relevant selections from...):

            <?php

            if ($submit) {

            MYSQL_CONNECT("localhost","user","pass");
            mysql_select_db("dbase_name");
            
            $sql = "INSERT INTO dbase (author,group,file_name) VALUES ('$author','$group','$file_name')"; 
            $result = mysql_query($sql); 
            
            exec("cp $file /path/to/file/store/$file_name"); 
            
            echo "Author: $author<br>\n";
            echo "Group: $group<br>\n";
            echo "File_Name: $file_name<br>\n";

            }

            ?>

            <HEAD><TITLE>Adding a new record.</TITLE></HEAD>
            <BODY bgcolor="#FFFFFF" text="#000000">
            <font size="2">

            <form enctype="multipart/form-data" method="post" action="<?php echo $PHP_SELF ?>">
            <font face="Verdana, Arial, Helvetica, sans-serif" size="2">
            Pick Group:<br>
            <select name="group">
            options... etc etc
            </select><br><br>
            Author:<br>
            <input type="Text" name="author" size="25">
            <br>
            File:<br>
            <input type="File" name="file" size="25"><br>
            <br><br>
            <input type="submit" name="submit" value="Upload">
            </form>
            </BODY>

            Anyone see anything I am doing wrong there?

            Please bear in mind this works perfectly for images, but do I need to add information in the code to tell mysql/php I am sending text this time? Other than that, I really cannot see why it will not upload my file 🙁

            Any help appreciated!

              it was my fault, too blind to notice a variable wasn't named right !

                Write a Reply...