Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

directory file uploader to a single file uploader 2

Status
Not open for further replies.

altctrldel

Technical User
Jul 26, 2003
30
heres the code I followed from spoono.com in uploading all image files in a certain directory into rows in mysql:

Code:
while ($file = readdir($dir_handle)) {
$filetyp = substr($file, -3);
if ($filetyp == 'gif' OR $filetyp == 'jpg') {
$handle = fopen($file,'r');
$file_content = fread($handle,filesize($file));
fclose($handle);
$encoded = chunk_split(base64_encode($file_content));
echo $encoded; 
$sql = "INSERT INTO images SET imagefield='$encoded'"; 
mysql_query($sql);
}
}

closedir($dir_handle);
echo("complete");
mysql_close($dbcnx);
?>

I like the idea of converting the image into an encrypted long text. That way the image is stored directly into the database in plain text.

What I can't figure out is how to modify the script in order to:

use a file browser instead of having it find all the gifs and jpegs in a defined directory.

my sad attempt:
Code:
<input type=\"file\" name=\"image\" size=\"40\" maxlength=\"1024\" />

[b]$image[/b] = $_POST['image'];

$filetyp = substr([b]$image[/b], -3);
if ($filetyp == 'gif' OR $filetyp == 'jpg') {
$handle = fopen($file,'r');
$file_content = fread($handle,filesize([b]$image[/b]));
fclose($handle);
$encoded = chunk_split(base64_encode($file_content));
echo $encoded; // to see if it works... 
}

I get errors in fopen, fclose.... :(

thanks in advance.
 
Code:
<form method="post" action="<?=$_SERVER['PHP_SELF']?>" enctype="application/x-[URL unfurl="true"]www-form-urlencoded">[/URL]
<input type="file" name="image" />
<br/>
<input type="submit" name="submit" value="Upload"/>
</form>

<?
if (isset($_FILES)):
$contents = readfile($_FILES['image']['tmp_name']);
$contents = mysql_escape_string($contents);
$sql = "insert into table set filecontents='$contents'";
endif;

you might also want to store the file name and the file mimetype (but i really don't recommend using the database as a filesystem). for more info on the $_FILES superglobal read at
 
As jpadie said, you should also store the file's name & type into your database. Once you have your images stored in a database, you can use a script like below to display them in another page. I made a similar document archiving system that I use in the office. Note however that I store my docs in binary format in a databse blob field.

getdoc.php
Code:
<?php
$id=$_GET['id'];

$query="select * from documents where docId='$id'";
$result=mysql_query($query);

$fileType=mysql_result($result,0,"fileType");
$fileName=mysql_result($result,0,"fileName");
$fileData=mysql_result($result,0,"fileData");

header('Content-type: '.$fileType);
header('Content-Disposition: attachment; filename="'.$fileName.'"');
header('Content-Transfer-Encoding: binary'); 
print $fileData;
?>

On your display page you can do this:
<img src="getdoc.php?id='[some doc id here]'">


--== Anything can go wrong. It's just a matter of how far wrong it will go till people think its right. ==--
 
weee thanks guys! finally got it working :D

here are my codes:

Code:
if ($_FILES) {

  $userfile  = addslashes (fread (fopen ($_FILES["userfile"]["tmp_name"], "r"), filesize ($_FILES["userfile"]["tmp_name"])));
  $file_name = $_FILES["userfile"]["name"];
  $file_type = $_FILES["userfile"]["type"];
  
}

inserting them later on along with the data into the db was easy. I also used and modified zeland's getdoc.php to fit mine. thanks.

Next problem is to only allow gifs and jpegs uploads. But i think a IF statement would solve that one. thanks again.
 
one last attempt ...:

relational databases are designed for the storage of relational information rather than flat information. a file (such as an image) is inherently flat. that is not to say that you cannot store flat information in an rdms: there's just not much value in doing so.

contrariwise - filesystems have millions of dollars poured into them to make sure that they are really good at handling file read/writes and storage activity.

until winFS comes along (which will blur the lines a bit), i really really recommend storing files in filesystems and relational information in relational databases. store the filename in the database and retrieve the binary from some sensible filesystem location.
 
"relational databases are designed for the storage of relational information rather than flat information"

From one prespective that may be true. In my case, document archival is a subset of our CMS. Every word document, excel file, powerpoint slides, PDFs, scaned invoices & quotations, catalogues, emails, are all related to either a customer or vendor. I initially started off storing files on the filesystem but later had problems managing the mass of files, especially files with same names and what files belonged to who.


--== Anything can go wrong. It's just a matter of how far wrong it will go till people think its right. ==--
 
i agree that things like blogs, and in some cases cms, blur the lines a bit and it can be said, as you point, that the document object is a related piece of the record object. but i still think the occasions where you should use a db blob field to hold binaries rather than ntfs or similar are few and far between!
 
@_@

ok I'd try to remember that

thanks for all these extra tips ^^
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top