PHP Applications
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Applications

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
You don't need a fax machine to get faxes. Get a fax-to-email fax number from CallWave. Try it free.
  #1  
Old August 16th, 2007, 01:25 PM
eyeshield21 eyeshield21 is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Posts: 12 eyeshield21 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 15 m 23 sec
Reputation Power: 0
Upload image file to MySQL as BLOB

hello,

i have this code below in which to upload image and store if in the database but it occurs an error upon running the program please help me im stack in here .. thankss

PHP Code:
<?
if (!isset($_REQUEST["submit"])) {

?>
<form method="POST" action="<?= $_SERVER["PHP_SELF"?>" enctype="application/x-www-form-
urlencoded">
<table>
<tr><td>Type</td><td><select name="imgtype"><option value="image/gif">GIF</option><option
value="image/jpeg">JPEG</option></select></td></tr>
<tr><td>File</td><td><input type="file" name="imgfile"></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="upload"><input type="reset"></td></tr>
</table>
</form>

<?
//-- save image to db --
} else {
  
/*
  the code below is a suggestion from California Strong...
  */
  
$hndl=fopen($_REQUEST["imgfile"],"r");
  
$isize=sizeof($_REQUEST["imgfile"]);

  
$imgdata="";
  while(!
feof($hndl)){
    
$imgdata=fread($hndl,$isize);
  }; 

  
/*
  my code was...
 
  $hndl=fopen($_REQUEST["imgfile"],"r");
  $imgdata=fread($hndl,filesize($_REQUEST["imgfile"]));
  */
  
   
$imgdata=addslashes($imgdata); 

  
$dbconn = @mysql_connect(localhost,simply22,msiatech8) or exit("SERVER Unavailable");
  @
mysql_select_db(simply22_simplyok,$dbconn) or exit("DB Unavailable");


  
$sql "INSERT INTO tblimage VALUES(NULL,'"$_REQUEST["imgtype"] ."','"$imgdata ."')";
 
  @
mysql_query($sql,$dbconn) or exit("QUERY FAILED!");

  
mysql_close($dbconn);

  
fclose($hndl);

  echo 
"<a href=\"test_imagedb_view.php\">view image</a>";

};
?>


errors:

Warning:fopen: failed to open stream: No such file or directory in
Warning: feof(): supplied argument is not a valid stream resource in on line 26

Reply With Quote
  #2  
Old August 16th, 2007, 02:11 PM
cwf's Avatar
cwf cwf is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 343 cwf User rank is Private First Class (20 - 50 Reputation Level)cwf User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 8 m 34 sec
Reputation Power: 2
To upload a file through a form in a browser, the enctype must be -
Code:
enctype="multipart/form-data"
Once you do this, the file will be uploaded, and will be accessible through the $_FILES array.

Reading this link will probably help - http://www.php.net/manual/en/features.file-upload.php

Reply With Quote
  #3  
Old August 16th, 2007, 08:45 PM
akong_mata akong_mata is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Location: makati city
Posts: 15 akong_mata User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 21 m 57 sec
Reputation Power: 0
cwf is right eyeshield21 and i haven't tried storing an IMAGE exactly in a database.. an easy way for me is that i store only the FILENAME of the FILE/IMAGE into the database.. i'm not sure if you're comfortable using this type of style but that's how I did it..

but we all know that there are so many ways of managing a FILE..

More reference:
http://www.php.net/features.file-upload
http://www.tizag.com/phpT/fileupload.php
http://www.w3schools.com/php/php_file_upload.asp

Reply With Quote
  #4  
Old August 16th, 2007, 09:31 PM
cwf's Avatar
cwf cwf is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 343 cwf User rank is Private First Class (20 - 50 Reputation Level)cwf User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 8 m 34 sec
Reputation Power: 2
Since the subject of storing images/files in a database has been brought up... my 2 cents -

A database is not a file storage engine. Using a database to store files is inefficient. It takes more processing CPU cycles, more memory, more time, and more storage space over just storing the files as a file.

Because binary data must be escaped before putting it into the database and un-escaped every time it is retrieved, the PHP program and the database engine use up additional resources over what is needed to just keep the file as a file in the file system.

I just did a quick test of the additional disk space need. It was about 2%-3% for the two test files I used -

Original .jpg file - 121,040 escaped size - 123,936 a 2.4% increase.
Original .jpg file - 57,902 escaped size - 59,577 a 2.9% increase.

If you have a lot of files or the file sizes are large, this extra storage space will add up.

Reply With Quote
  #5  
Old August 17th, 2007, 02:09 PM
hammer65 hammer65 is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Location: Nebraska
Posts: 55 hammer65 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 16 m 53 sec
Reputation Power: 1
The only reason to store file contents is if you absolutely need the security model that the database provides. If you were for instance storing image scans of patient x-ray examinations or other private medical information in pdf form, then it might be necessary to store them in the database, otherwise, it's just a performance and storage problem, that you aren't going to want to deal with.

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Applications > Upload image file to MySQL as BLOB


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five &quot;checkpoints&quot; for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway