Training, Open Source Programming Languages

This is page http://www.wellho.net/resources/ex.php

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Uploading an image from a form to a MySQL database via PHP
Using MySQL Databases in PHP Pages example from a Well House Consultants training course
More on Using MySQL Databases in PHP Pages [link]

This example is described in the following article(s):
   • Hot answers in PHP - [link]
   • Using PHP to upload images / Store on MySQL database - security questions - [link]

If you're searching for a page where you can try this code, select here

Source code: pic_up.php Module: H113
<?php

// Connect to database

$errmsg "";
if (! @
mysql_connect("localhost","trainee","abc123")) {
        
$errmsg "Cannot connect to database";
        }
@
mysql_select_db("wellho");

// First run ONLY - need to create table by un commenting this
// Or with silent @ we can let it fail every subsequent time ;-)

$q = <<<CREATE
create table pix (
    pid int primary key not null auto_increment,
    title text,
    imgdata longblob)
CREATE;
@
mysql_query($q);

// Insert any new image into database

if ($_REQUEST[completed] == 1) {
        
// Need to add - check for large upload. Otherwise the code
        // will just duplicate old file ;-)
        // ALSO - note that latest.img must be public write and in a
        // live application should be in another (safe!) directory.
        
move_uploaded_file($_FILES['imagefile']['tmp_name'],"latest.img");
        
$instr fopen("latest.img","rb");
        
$image mysql_real_escape_string(fread($instr,filesize("latest.img")));
        if (
strlen($instr) < 149000) {
                
mysql_query ("insert into pix (title, imgdata) values (\"".
                
$_REQUEST[whatsit].
                
"\", \"".
                
$image.
                
"\")");
        } else {
                
$errmsg "Too large!";
        }
}

// Find out about latest image

$gotten = @mysql_query("select * from pix order by pid desc limit 1");
if (
$row = @mysql_fetch_assoc($gotten)) {
        
$title htmlspecialchars($row[title]);
        
$bytes $row[imgdata];
} else {
        
$errmsg "There is no image in the database yet";
        
$title "no database image available";
        
// Put up a picture of our training centre
        
$instr fopen("../wellimg/ctco.jpg","rb");
        
$bytes fread($instr,filesize("../wellimg/ctco.jpg"));
}

// If this is the image request, send out the image

if ($_REQUEST[gim] == 1) {
        
header("Content-type: image/jpeg");
        print 
$bytes;
        exit ();
        }
?>

<html><head>
<title>Upload an image to a database</title>
<body bgcolor=white><h2>Here's the latest picture</h2>
<font color=red><?= $errmsg ?></font>
<center><img src=?gim=1 width=144><br>
<b><?= $title ?></center>
<hr>
<h2>Please upload a new picture and title</h2>
<form enctype=multipart/form-data method=post>
<input type=hidden name=MAX_FILE_SIZE value=150000>
<input type=hidden name=completed value=1>
Please choose an image to upload: <input type=file name=imagefile><br>
Please enter the title of that picture: <input name=whatsit><br>
then: <input type=submit></form><br><br>
<a href=pic_alog.php4>Link - view images already uploaded</a><br>
<a href=/solutions/php-example-php-form-image-upload-store-in-mysql-database-retreive.html>Link - technical article</a> including source code<br><br>
Note</b> - by uploading an image to this site, you agree that you are the
copyright owner, that the picture is legally acceptable and that you take
full responsibility for this, and that the owners of this site are
henceforth able to make free limitless use of the picture if they so
wish. Your IP address and other details may be logged as you upload.
Sorry about this note - have to protect ourselves.<b>
<hr>
By Graham Ellis - graham@wellho.net</b>
</body>
</html>

Learn about this subject
This module and example are covered on the following public courses:
 * Learning to program in PHP
 * PHP Programming
 * MySQL
 * PHP Programming
 * Learning to program in PHP
Also available on on site courses for larger groups

Books covering this topic
Yes. We have over 700 books in our library. Books covering PHP are listed here and when you've selected a relevant book we'll link you on to Amazon to order.

Other Examples
This example comes from our "Using MySQL Databases in PHP Pages" training module. You'll find a description of the topic and some other closely related examples on the "Using MySQL Databases in PHP Pages" module index page.

Full description of the source code
You can learn more about this example on the training courses listed on this page, on which you'll be given a full set of training notes.

Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License.

Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre.
The Horse's mouth provides a daily tip or thought.
• Further resources are available via the resources centre.
• All of these resources can be searched through through our search engine
• And there's a global index here.

Purpose of this website
This is a sample program, class demonstration or answer from a training course. It's main purpose is to provide an after-course service to customers who have attended our public private or on site courses, but the examples are made generally available under conditions described below.

Web site author
This web site is written and maintained by Well House Consultants.

Conditions of use
Past attendees on our training courses are welcome to use individual examples in the course of their programming, but must check the examples they use to ensure that they are suitable for their job. Remember that some of our examples show you how not to do things - check in your notes. Well House Consultants take no responsibility for the suitability of these example programs to customer's needs.

This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details.

Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request.

© WELL HOUSE CONSULTANTS LTD., 2022: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb