LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-15-2005, 09:26 AM   #1
ALInux
Member
 
Registered: Nov 2003
Location: Lebanon
Distribution: RHEL 5/CentOS 5/Debian Lenny/(K)Ubuntu Is Dead/Mandriva 10.1
Posts: 676
Blog Entries: 7

Rep: Reputation: 32
upload image to db "php + mysql"


Hi guys I want to create a backoffice for my site...I want to be able to upload banners to my mysql database. Ive got everything right...I only need the code to acually upload the image to the db like "fread" or so.........
 
Old 12-15-2005, 11:27 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
You basically need the following; please google for a more complete example, and/or the PHP manual pages (whichever you prefer):

1. HTML:
Code:
<form enctype="multipart/form-data" action="myupload.php" method="post">
  ...
  <input type="file" name="myfile">
  ...
2. PHP:
Code:
<?php
  // $userfile: where the file went on the web server
  $userfile = $HTTP_POST_FILES['myfile']['tmp_name'];

  // $userfile_name: the original file name
  $userfile_name = $HTTP_POST_FILES['myfile']['name'];

  // $userfile_size: the file size (bytes)
  $userfile_name = $HTTP_POST_FILES['myfile']['size'];
  ...
3. mySQL:
Code:
sub insert
{
  # Insert a new attachment into the database.

  # Escape characters in strings that will be used in SQL statements.
  my $filename = SqlQuote($::FILE{'data'}->{'filename'});
  my $description = SqlQuote($::FORM{'description'});
  my $contenttype = SqlQuote($::FORM{'contenttype'});
  my $thedata = SqlQuote($::FORM{'data'});

  # Insert the attachment into the database.
  SendSQL("INSERT INTO attachments (bug_id, filename, description, mimetype,
    ispatch, submitter_id, thedata)
    VALUES ($::FORM{'bugid'}, $filename, $description, $contenttype,
    $::FORM{'ispatch'}, $::userid, $thedata)");

  # Retrieve the ID of the newly created attachment record.
  SendSQL("SELECT LAST_INSERT_ID()");
  my $attachid = FetchOneColumn();
The above is (in part) from "PHP and MySQL Web Development", Welling/Thomson, New Riders - highly recommended, and in part from Bugzilla (an open-source database that uses Perl - not PHP - and mySQL) "attachments.cgi".

Last edited by paulsm4; 12-15-2005 at 11:29 AM.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL/PHP/Horde "Failed to write session data" Big Money Linux - Software 0 11-23-2004 01:33 PM
Php or Mysql upgrade on Fedora Core 2 "Uptodate" Program geninblaze Fedora 1 07-12-2004 09:56 PM
PHP "for.. loop" to insert into mysql Boby Programming 10 05-29-2004 10:07 AM
PHP and MYSQL ("mysql_connect") smaida Programming 4 05-18-2004 05:27 PM
php ide with "break" and "trace" Kayaker Programming 3 04-25-2003 02:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration